ReadOnlyAssociative.vala 883 B

12345678910111213141516171819202122232425262728
  1. namespace Invercargill {
  2. [GenericAccessors]
  3. public interface ReadOnlyAssociative<TKey, TValue> : Lot<KeyValuePair<TKey, TValue>> {
  4. public abstract bool try_get(TKey key, out TValue value);
  5. public abstract bool has(TKey key);
  6. public abstract Enumerable<TKey> keys { owned get; }
  7. public abstract Enumerable<TValue> values { owned get; }
  8. public virtual TValue @get(TKey key) throws IndexError {
  9. TValue value;
  10. if(try_get(key, out value)) {
  11. return value;
  12. }
  13. throw new IndexError.KEY_NOT_FOUND(@"Key not found in the associative collection");
  14. }
  15. public virtual TValue? get_or_default(TKey key) {
  16. TValue value;
  17. if(try_get(key, out value)) {
  18. return value;
  19. }
  20. return null;
  21. }
  22. }
  23. }