01: package gnu.mapping;
02:
03: public abstract class PropertySet implements Named {
04: /** If non-null, a sequence of (key, value)-pairs. */
05: private Object[] properties;
06:
07: private static final String nameKey = "name";
08:
09: public String getName() {
10: Object symbol = getProperty(nameKey, null);
11: return symbol == null ? null
12: : symbol instanceof Symbol ? ((Symbol) symbol)
13: .getName() : symbol.toString();
14: }
15:
16: public Object getSymbol() {
17: return getProperty(nameKey, null);
18: }
19:
20: public final void setSymbol(Object name) {
21: setProperty(nameKey, name);
22: }
23:
24: public final void setName(String name) {
25: setProperty(nameKey, name);
26: }
27:
28: public Object getProperty(Object key, Object defaultValue) {
29: if (properties != null) {
30: for (int i = properties.length; (i -= 2) >= 0;) {
31: if (properties[i] == key)
32: return properties[i + 1];
33: }
34: }
35: return defaultValue;
36: }
37:
38: public synchronized void setProperty(Object key, Object value) {
39: properties = PropertySet.setProperty(properties, key, value);
40: }
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, Object key,
49: Object value) {
50: int avail;
51: Object[] props = properties;
52: if (props == null) {
53: properties = props = new Object[10];
54: avail = 0;
55: } else {
56: avail = -1;
57: for (int i = props.length; (i -= 2) >= 0;) {
58: Object k = props[i];
59: if (k == key) {
60: Object old = props[i + 1];
61: props[i + 1] = value;
62: return properties;
63: } else if (k == null)
64: avail = i;
65: }
66: if (avail < 0) {
67: avail = props.length;
68: properties = new Object[2 * avail];
69: System.arraycopy(props, 0, properties, 0, avail);
70: props = properties;
71: }
72: }
73: props[avail] = key;
74: props[avail + 1] = value;
75: return properties;
76: }
77:
78: public Object removeProperty(Object key) {
79: Object[] props = properties;
80: if (props == null)
81: return null;
82: for (int i = props.length; (i -= 2) >= 0;) {
83: Object k = props[i];
84: if (k == key) {
85: Object old = props[i + 1];
86: props[i] = null;
87: props[i + 1] = null;
88: return old;
89: }
90: }
91: return null;
92: }
93: }
|