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