PropertySet.java 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package gnu.mapping;
  2. public abstract class PropertySet implements Named
  3. {
  4. /** If non-null, a sequence of (key, value)-pairs. */
  5. private Object[] properties;
  6. public static final Symbol nameKey = Namespace.EmptyNamespace.getSymbol("name");
  7. public String getName()
  8. {
  9. Object symbol = getProperty(nameKey, null);
  10. return symbol == null ? null
  11. : symbol instanceof Symbol ? ((Symbol) symbol).getName()
  12. : symbol.toString();
  13. }
  14. public Object getSymbol()
  15. {
  16. return getProperty(nameKey, null);
  17. }
  18. public final void setSymbol (Object name)
  19. {
  20. setProperty(nameKey, name);
  21. }
  22. public final void setName (String name)
  23. {
  24. setProperty(nameKey, name);
  25. }
  26. public Object getProperty(Object key, Object defaultValue)
  27. {
  28. if (properties != null)
  29. {
  30. for (int i = properties.length; (i -= 2) >= 0; )
  31. {
  32. if (properties[i] == key)
  33. return properties[i + 1];
  34. }
  35. }
  36. return defaultValue;
  37. }
  38. public synchronized void setProperty(Object key, Object value)
  39. {
  40. properties = PropertySet.setProperty(properties, key, value);
  41. }
  42. /** Given a property list, update it.
  43. * @param properties the input property list
  44. * @param key
  45. * @param value associate this with key in result
  46. * @return updated property list (maybe the same as the input)
  47. */
  48. public static Object[] setProperty(Object[] properties,
  49. Object key, Object value)
  50. {
  51. int avail;
  52. Object[] props = properties;
  53. if (props == null)
  54. {
  55. properties = props = new Object[10];
  56. avail = 0;
  57. }
  58. else
  59. {
  60. avail = -1;
  61. for (int i = props.length; (i -= 2) >= 0; )
  62. {
  63. Object k = props[i];
  64. if (k == key)
  65. {
  66. Object old = props[i + 1];
  67. props[i + 1] = value;
  68. return properties;
  69. }
  70. else if (k == null)
  71. avail = i;
  72. }
  73. if (avail < 0)
  74. {
  75. avail = props.length;
  76. properties = new Object[2 * avail];
  77. System.arraycopy(props, 0, properties, 0, avail);
  78. props = properties;
  79. }
  80. }
  81. props[avail] = key;
  82. props[avail+1] = value;
  83. return properties;
  84. }
  85. public Object removeProperty(Object key)
  86. {
  87. Object[] props = properties;
  88. if (props == null)
  89. return null;
  90. for (int i = props.length; (i -= 2) >= 0; )
  91. {
  92. Object k = props[i];
  93. if (k == key)
  94. {
  95. Object old = props[i + 1];
  96. props[i] = null;
  97. props[i + 1] = null;
  98. return old;
  99. }
  100. }
  101. return null;
  102. }
  103. }