1 package gnu.mapping; 2 3 public abstract class PropertySet implements Named 4 { 5 6 private Object [] properties; 7 8 private static final String nameKey = "name"; 9 10 public String getName() 11 { 12 Object symbol = getProperty(nameKey, null); 13 return symbol == null ? null 14 : symbol instanceof Symbol ? ((Symbol) symbol).getName() 15 : symbol.toString(); 16 } 17 18 public Object getSymbol() 19 { 20 return getProperty(nameKey, null); 21 } 22 23 public final void setSymbol (Object name) 24 { 25 setProperty(nameKey, name); 26 } 27 28 public final void setName (String name) 29 { 30 setProperty(nameKey, name); 31 } 32 33 public Object getProperty(Object key, Object defaultValue) 34 { 35 if (properties != null) 36 { 37 for (int i = properties.length; (i -= 2) >= 0; ) 38 { 39 if (properties[i] == key) 40 return properties[i + 1]; 41 } 42 } 43 return defaultValue; 44 } 45 46 public synchronized void setProperty(Object key, Object value) 47 { 48 properties = PropertySet.setProperty(properties, key, value); 49 } 50 51 57 public static Object [] setProperty(Object [] properties, 58 Object key, Object value) 59 { 60 int avail; 61 Object [] props = properties; 62 if (props == null) 63 { 64 properties = props = new Object [10]; 65 avail = 0; 66 } 67 else 68 { 69 avail = -1; 70 for (int i = props.length; (i -= 2) >= 0; ) 71 { 72 Object k = props[i]; 73 if (k == key) 74 { 75 Object old = props[i + 1]; 76 props[i + 1] = value; 77 return properties; 78 } 79 else if (k == null) 80 avail = i; 81 } 82 if (avail < 0) 83 { 84 avail = props.length; 85 properties = new Object [2 * avail]; 86 System.arraycopy(props, 0, properties, 0, avail); 87 props = properties; 88 } 89 } 90 props[avail] = key; 91 props[avail+1] = value; 92 return properties; 93 } 94 95 public Object removeProperty(Object key) 96 { 97 Object [] props = properties; 98 if (props == null) 99 return null; 100 for (int i = props.length; (i -= 2) >= 0; ) 101 { 102 Object k = props[i]; 103 if (k == key) 104 { 105 Object old = props[i + 1]; 106 props[i] = null; 107 props[i + 1] = null; 108 return old; 109 } 110 } 111 return null; 112 } 113 } 114 | Popular Tags |