PlainLocation.java 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright (c) 2004 Per M.A. Bothner.
  2. // This is free software; for terms and warranty disclaimer see ./COPYING.
  3. package gnu.mapping;
  4. public class PlainLocation<T> extends NamedLocation<T>
  5. {
  6. public PlainLocation (Symbol symbol, Object property)
  7. {
  8. super(symbol, property);
  9. }
  10. public PlainLocation (Symbol symbol, Object property, T value)
  11. {
  12. super(symbol, property);
  13. this.value = value;
  14. }
  15. public final T get ()
  16. {
  17. if (base != null) return base.get();
  18. if (value == Location.UNBOUND) throw new UnboundLocationException(this);
  19. return (T) value;
  20. }
  21. public final T get (T defaultValue)
  22. {
  23. return base != null ? base.get(defaultValue)
  24. : value == Location.UNBOUND ? defaultValue : (T) value;
  25. }
  26. public boolean isBound ()
  27. {
  28. return base != null ? base.isBound() : value != Location.UNBOUND;
  29. }
  30. public final void set (T newValue)
  31. {
  32. if (base == null)
  33. value = newValue;
  34. else if (value == DIRECT_ON_SET)
  35. {
  36. base = null;
  37. value = newValue;
  38. }
  39. else if (base.isConstant())
  40. getEnvironment().put(getKeySymbol(), getKeyProperty(), newValue);
  41. else
  42. base.set(newValue);
  43. }
  44. }