LocationProc.java 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. /** A Procedure that evaluates to the value of a Location.
  5. * Calling it with one argument sets the value, for compatibility
  6. * with the "parameter objects" of SRFI-39.
  7. */
  8. public class LocationProc<T> extends Procedure0or1 implements HasSetter
  9. {
  10. Location<T> loc;
  11. public LocationProc (Location loc)
  12. {
  13. this.loc = loc;
  14. }
  15. public static LocationProc makeNamed (Symbol name, Location loc)
  16. {
  17. LocationProc lproc = new LocationProc(loc);
  18. lproc.setSymbol(name);
  19. return lproc;
  20. }
  21. public LocationProc (Location loc, Procedure converter)
  22. {
  23. this.loc = loc;
  24. if (converter != null)
  25. pushConverter(converter);
  26. }
  27. public void pushConverter (Procedure converter)
  28. {
  29. loc = ConstrainedLocation.make(loc, converter);
  30. }
  31. public final T getValue() throws Throwable {
  32. return loc.get();
  33. }
  34. public T apply0 () throws Throwable
  35. {
  36. return loc.get();
  37. }
  38. public Object apply1 (Object value) throws Throwable
  39. {
  40. set0((T) value);
  41. return Values.empty;
  42. }
  43. public void set0 (Object value) throws Throwable
  44. {
  45. loc.set((T) value);
  46. }
  47. public Procedure getSetter()
  48. {
  49. return new Setter0(this);
  50. }
  51. public final Location getLocation ()
  52. {
  53. return loc;
  54. }
  55. public String toString ()
  56. {
  57. Object n = getSymbol();
  58. if (n != null)
  59. return super.toString();
  60. return "#<location-proc "+loc+">";
  61. }
  62. }