SharedLocation.java 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. /** A Location suitable when Environment or Location can be access by
  5. * multiple threads. Accesses are synchronized. */
  6. public class SharedLocation<T> extends NamedLocation<T>
  7. {
  8. int timestamp;
  9. public SharedLocation (Symbol symbol, Object property, int timestamp)
  10. {
  11. super(symbol, property);
  12. this.timestamp = timestamp;
  13. }
  14. public synchronized final T get ()
  15. {
  16. if (base != null) return base.get();
  17. if (value == Location.UNBOUND) throw new UnboundLocationException(this);
  18. return (T) value;
  19. }
  20. public synchronized final T get (T defaultValue)
  21. {
  22. return base != null ? base.get(defaultValue)
  23. : value == Location.UNBOUND ? defaultValue : (T) value;
  24. }
  25. public synchronized boolean isBound ()
  26. {
  27. return base != null ? base.isBound() : value != Location.UNBOUND;
  28. }
  29. public synchronized final void set (T newValue)
  30. {
  31. if (base == null)
  32. value = newValue;
  33. else if (value == DIRECT_ON_SET)
  34. {
  35. base = null;
  36. value = newValue;
  37. }
  38. else if (base.isConstant())
  39. getEnvironment().put(getKeySymbol(), getKeyProperty(), newValue);
  40. else
  41. base.set(newValue);
  42. }
  43. }