ConstrainedLocation.java 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright (c) 2005 Per M.A. Bothner.
  2. // This is free software; for terms and warranty disclaimer see ./COPYING.
  3. package gnu.mapping;
  4. public class ConstrainedLocation<T> extends Location<T>
  5. {
  6. protected Location<T> base;
  7. protected Procedure converter;
  8. public static <T> ConstrainedLocation<T> make (Location<T> base,
  9. Procedure converter)
  10. {
  11. ConstrainedLocation<T> cloc = new ConstrainedLocation<T>();
  12. cloc.base = base;
  13. cloc.converter = converter;
  14. return cloc;
  15. }
  16. public Symbol getKeySymbol ()
  17. {
  18. return base == null ? null : base.getKeySymbol();
  19. }
  20. public Object getKeyProperty ()
  21. {
  22. return base.getKeyProperty();
  23. }
  24. public boolean isConstant ()
  25. {
  26. return base.isConstant();
  27. }
  28. public final T get ()
  29. {
  30. return base.get();
  31. }
  32. public final T get (T defaultValue)
  33. {
  34. return base.get(defaultValue);
  35. }
  36. public boolean isBound ()
  37. {
  38. return base.isBound();
  39. }
  40. protected T coerce (T newValue)
  41. {
  42. try
  43. {
  44. return (T) converter.apply1(newValue);
  45. }
  46. catch (Throwable ex)
  47. {
  48. throw WrappedException.rethrow(ex);
  49. }
  50. }
  51. public final void set (T newValue)
  52. {
  53. base.set(coerce(newValue));
  54. }
  55. public void undefine ()
  56. {
  57. base.undefine();
  58. }
  59. public Object setWithSave (T newValue)
  60. {
  61. return base.setWithSave(coerce(newValue));
  62. }
  63. public void setRestore (Object oldValue)
  64. {
  65. base.setRestore(oldValue);
  66. }
  67. }