1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46:
47: import ;
48: import ;
49: import ;
50:
51:
56: public class BasicAttribute implements Attribute
57: {
58: private static final long serialVersionUID = 6743528196119291326L;
59:
60:
61: protected String attrID;
62:
63: protected boolean ordered;
64:
65: protected transient Vector<Object> values;
66:
67:
68: private BasicAttribute ()
69: {
70: }
71:
72: public BasicAttribute (String id)
73: {
74: this (id, false);
75: }
76:
77: public BasicAttribute (String id, boolean ordered)
78: {
79: attrID = id;
80: this.ordered = ordered;
81: values = new Vector<Object> ();
82: }
83:
84: public BasicAttribute (String id, Object value)
85: {
86: this (id, value, false);
87: }
88:
89: public BasicAttribute (String id, Object value, boolean ordered)
90: {
91: attrID = id;
92: this.ordered = ordered;
93: values = new Vector<Object> ();
94: values.add (value);
95: }
96:
97: public void add (int index, Object val)
98: {
99: if (! ordered && contains (val))
100: throw new IllegalStateException ("value already in attribute");
101: values.add (index, val);
102: }
103:
104: public boolean add (Object val)
105: {
106: if (! ordered && contains (val))
107: throw new IllegalStateException ("value already in attribute");
108: return values.add (val);
109: }
110:
111: public void clear ()
112: {
113: values.clear ();
114: }
115:
116: public Object clone ()
117: {
118: BasicAttribute c = new BasicAttribute ();
119: c.attrID = attrID;
120: c.ordered = ordered;
121: c.values = (Vector<Object>) values.clone ();
122: return c;
123: }
124:
125: public boolean contains (Object val)
126: {
127: for (int i = 0; i < values.size (); ++i)
128: {
129: if (equals (val, values.get (i)))
130: return true;
131: }
132:
133: return false;
134: }
135:
136: public boolean equals (Object obj)
137: {
138: if (! (obj instanceof BasicAttribute))
139: return false;
140: BasicAttribute b = (BasicAttribute) obj;
141:
142: if (ordered != b.ordered
143: || ! attrID.equals (b.attrID)
144: || values.size () != b.values.size ())
145: return false;
146:
147: for (int i = 0; i < values.size (); ++i)
148: {
149: boolean ok = false;
150: if (ordered)
151: ok = equals (values.get (i), b.values.get (i));
152: else
153: {
154: for (int j = 0; j < b.values.size (); ++j)
155: {
156: if (equals (values.get (i), b.values.get (j)))
157: {
158: ok = true;
159: break;
160: }
161: }
162: }
163:
164: if (! ok)
165: return false;
166: }
167:
168: return true;
169: }
170:
171: public Object get ()
172: throws NamingException
173: {
174: if (values.size () == 0)
175: throw new NoSuchElementException ("no values");
176: return get (0);
177: }
178:
179: public Object get (int index)
180: throws NamingException
181: {
182: return values.get (index);
183: }
184:
185: public NamingEnumeration<?> getAll ()
186: throws NamingException
187: {
188: return new BasicAttributeEnumeration ();
189: }
190:
191: public DirContext getAttributeDefinition ()
192: throws OperationNotSupportedException, NamingException
193: {
194: throw new OperationNotSupportedException ();
195: }
196:
197: public DirContext getAttributeSyntaxDefinition ()
198: throws OperationNotSupportedException, NamingException
199: {
200: throw new OperationNotSupportedException ();
201: }
202:
203: public String getID ()
204: {
205: return attrID;
206: }
207:
208: public int hashCode ()
209: {
210: int val = attrID.hashCode ();
211: for (int i = 0; i < values.size (); ++i)
212: {
213: Object o = values.get (i);
214: if (o == null)
215: {
216:
217: }
218: else if (o instanceof Object[])
219: {
220: Object[] a = (Object[]) o;
221: for (int j = 0; j < a.length; ++j)
222: val += a[j].hashCode ();
223: }
224: else
225: val += o.hashCode ();
226: }
227:
228: return val;
229: }
230:
231: public boolean isOrdered ()
232: {
233: return ordered;
234: }
235:
236: public Object remove (int index)
237: {
238: return values.remove (index);
239: }
240:
241: public boolean remove (Object val)
242: {
243: for (int i = 0; i < values.size (); ++i)
244: {
245: if (equals (val, values.get (i)))
246: {
247: values.remove (i);
248: return true;
249: }
250: }
251:
252: return false;
253: }
254:
255: public Object set (int index, Object val)
256: {
257: if (! ordered && contains (val))
258: throw new IllegalStateException ("value already in attribute");
259: return values.set (index, val);
260: }
261:
262: public int size ()
263: {
264: return values.size ();
265: }
266:
267: public String toString ()
268: {
269: String r = attrID;
270: for (int i = 0; i < values.size (); ++i)
271: r += ";" + values.get (i).toString ();
272: return r;
273: }
274:
275:
276:
277: private boolean equals (Object one, Object two)
278: {
279: if (one == null)
280: return two == null;
281:
282: if (one instanceof Object[])
283: {
284: if (! (two instanceof Object[]))
285: return false;
286:
287: Object[] aone = (Object[]) one;
288: Object[] atwo = (Object[]) two;
289:
290: if (aone.length != atwo.length)
291: return false;
292:
293: for (int i = 0; i < aone.length; ++i)
294: {
295: if (! aone[i].equals (atwo[i]))
296: return false;
297: }
298:
299: return true;
300: }
301:
302: return one.equals (two);
303: }
304:
305: private void readObject(ObjectInputStream s)
306: throws IOException, ClassNotFoundException
307: {
308: s.defaultReadObject();
309: int size = s.readInt();
310: values = new Vector<Object>(size);
311: for (int i=0; i < size; i++)
312: values.add(s.readObject());
313: }
314:
315: private void writeObject(ObjectOutputStream s) throws IOException
316: {
317: s.defaultWriteObject();
318: s.writeInt(values.size());
319: for (int i=0; i < values.size(); i++)
320: s.writeObject(values.get(i));
321: }
322:
323:
324: private class BasicAttributeEnumeration implements NamingEnumeration
325: {
326: int where = 0;
327:
328: public BasicAttributeEnumeration ()
329: {
330: }
331:
332: public void close () throws NamingException
333: {
334: }
335:
336: public boolean hasMore () throws NamingException
337: {
338: return hasMoreElements ();
339: }
340:
341: public Object next () throws NamingException
342: {
343: return nextElement ();
344: }
345:
346: public boolean hasMoreElements ()
347: {
348: return where < values.size ();
349: }
350:
351: public Object nextElement () throws NoSuchElementException
352: {
353: if (where == values.size ())
354: throw new NoSuchElementException ("no more elements");
355: return values.get (where++);
356: }
357: }
358: }