KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > mapping > PropertySet


1 package gnu.mapping;
2
3 public abstract class PropertySet implements Named
4 {
5   /** If non-null, a sequence of (key, value)-pairs. */
6   private Object JavaDoc[] properties;
7
8   private static final String JavaDoc nameKey = "name";
9
10   public String JavaDoc getName()
11   {
12     Object JavaDoc symbol = getProperty(nameKey, null);
13     return symbol == null ? null
14       : symbol instanceof Symbol ? ((Symbol) symbol).getName()
15       : symbol.toString();
16   }
17
18   public Object JavaDoc getSymbol()
19   {
20     return getProperty(nameKey, null);
21   }
22
23   public final void setSymbol (Object JavaDoc name)
24   {
25     setProperty(nameKey, name);
26   }
27
28   public final void setName (String JavaDoc name)
29   {
30     setProperty(nameKey, name);
31   }
32
33   public Object JavaDoc getProperty(Object JavaDoc key, Object JavaDoc 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 JavaDoc key, Object JavaDoc value)
47   {
48     properties = PropertySet.setProperty(properties, key, value);
49   }
50
51   /** Given a property list, update it.
52    * @param properties the input property list
53    * @param key
54    * @param value associate this with key in result
55    * @return updated property list (maybe the same as the input)
56    */

57   public static Object JavaDoc[] setProperty(Object JavaDoc[] properties,
58                      Object JavaDoc key, Object JavaDoc value)
59   {
60     int avail;
61     Object JavaDoc[] props = properties;
62     if (props == null)
63       {
64     properties = props = new Object JavaDoc[10];
65     avail = 0;
66       }
67     else
68       {
69     avail = -1;
70     for (int i = props.length; (i -= 2) >= 0; )
71       {
72         Object JavaDoc k = props[i];
73         if (k == key)
74           {
75         Object JavaDoc 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 JavaDoc[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 JavaDoc removeProperty(Object JavaDoc key)
96   {
97     Object JavaDoc[] props = properties;
98     if (props == null)
99       return null;
100     for (int i = props.length; (i -= 2) >= 0; )
101       {
102     Object JavaDoc k = props[i];
103     if (k == key)
104       {
105         Object JavaDoc 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