1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46:
47: import ;
48: import ;
49:
50:
54: public class BasicAttributes implements Attributes
55: {
56: private static final long serialVersionUID = 4980164073184639448L;
57:
58: public BasicAttributes ()
59: {
60: this (false);
61: }
62:
63: public BasicAttributes (boolean ignoreCase)
64: {
65: this.ignoreCase = ignoreCase;
66: this.attributes = new Vector<Attribute>();
67: }
68:
69: public BasicAttributes (String attrID, Object val)
70: {
71: this (attrID, val, false);
72: }
73:
74: public BasicAttributes (String attrID, Object val, boolean ignoreCase)
75: {
76: this.ignoreCase = ignoreCase;
77: attributes = new Vector<Attribute>();
78: attributes.add (new BasicAttribute (attrID, val));
79: }
80:
81: public Object clone ()
82: {
83:
84: BasicAttributes ba = new BasicAttributes (ignoreCase);
85: ba.attributes = (Vector<Attribute>) attributes.clone ();
86: return ba;
87: }
88:
89:
94: public boolean equals (Object obj)
95: {
96: if (! (obj instanceof Attributes))
97: return false;
98:
99: Attributes bs = (Attributes) obj;
100: if (ignoreCase != bs.isCaseIgnored()
101: || attributes.size () != bs.size ())
102: return false;
103:
104: NamingEnumeration bas = bs.getAll();
105: while (bas.hasMoreElements())
106: {
107: Attribute a = (Attribute) bas.nextElement();
108: Attribute b = get(a.getID ());
109: if (! a.equals(b))
110: return false;
111: }
112:
113: return true;
114: }
115:
116: public Attribute get (String attrID)
117: {
118: for (int i = 0; i < attributes.size (); ++i)
119: {
120: Attribute at = attributes.get (i);
121: if ((ignoreCase && attrID.equalsIgnoreCase (at.getID ()))
122: || (! ignoreCase && attrID.equals (at.getID ())))
123: return at;
124: }
125:
126: return null;
127: }
128:
129: public NamingEnumeration<Attribute> getAll ()
130: {
131: return new BasicAttributesEnumeration();
132: }
133:
134: public NamingEnumeration<String> getIDs ()
135: {
136: final NamingEnumeration<Attribute> attrs = getAll();
137: return new NamingEnumeration<String>() {
138: public boolean hasMore() throws NamingException
139: {
140: return attrs.hasMore();
141: }
142:
143: public boolean hasMoreElements()
144: {
145: return attrs.hasMoreElements();
146: }
147:
148: public String next() throws NamingException
149: {
150: return attrs.next().getID();
151: }
152:
153: public String nextElement()
154: {
155: return attrs.nextElement().getID();
156: }
157:
158: public void close() throws NamingException
159: {
160: attrs.close();
161: }
162: };
163: }
164:
165: public int hashCode ()
166: {
167: int val = 0;
168: for (int i = 0; i < attributes.size (); ++i)
169: val += attributes.get (i).hashCode ();
170: return val;
171: }
172:
173: public boolean isCaseIgnored ()
174: {
175: return ignoreCase;
176: }
177:
178: public Attribute put (Attribute attr)
179: {
180: Attribute r = remove (attr.getID ());
181: attributes.add (attr);
182: return r;
183: }
184:
185: public Attribute put (String attrID, Object val)
186: {
187: return put (new BasicAttribute (attrID, val));
188: }
189:
190: public Attribute remove (String attrID)
191: {
192: for (int i = 0; i < attributes.size (); ++i)
193: {
194: Attribute at = (Attribute) attributes.get (i);
195: if ((ignoreCase && attrID.equalsIgnoreCase (at.getID ()))
196: || (! ignoreCase && attrID.equals (at.getID ())))
197: {
198: attributes.remove (i);
199: return at;
200: }
201: }
202:
203: return null;
204: }
205:
206: public int size ()
207: {
208: return attributes.size ();
209: }
210:
211: public String toString ()
212: {
213: String r = "";
214: for (int i = 0; i < attributes.size (); ++i)
215: {
216: if (i > 0)
217: r += "; ";
218: r += attributes.get (i).toString ();
219: }
220: return r;
221: }
222:
223:
224: private boolean ignoreCase;
225:
226: transient Vector<Attribute> attributes;
227:
228: private void readObject(ObjectInputStream s) throws IOException,
229: ClassNotFoundException
230: {
231: s.defaultReadObject();
232: int size = s.readInt();
233: attributes = new Vector<Attribute>(size);
234: for (int i = 0; i < size; i++)
235: attributes.add((Attribute) s.readObject());
236: }
237:
238: private void writeObject(ObjectOutputStream s) throws IOException
239: {
240: s.defaultWriteObject();
241: s.writeInt(attributes.size());
242: for (int i = 0; i < attributes.size(); i++)
243: s.writeObject(attributes.get(i));
244: }
245:
246:
247: private class BasicAttributesEnumeration
248: implements NamingEnumeration<Attribute>
249: {
250: int where = 0;
251:
252: public BasicAttributesEnumeration ()
253: {
254: }
255:
256: public void close () throws NamingException
257: {
258: }
259:
260: public boolean hasMore () throws NamingException
261: {
262: return hasMoreElements ();
263: }
264:
265: public Attribute next () throws NamingException
266: {
267: return nextElement ();
268: }
269:
270: public boolean hasMoreElements ()
271: {
272: return where < attributes.size ();
273: }
274:
275: public Attribute nextElement () throws NoSuchElementException
276: {
277: if (where >= attributes.size ())
278: throw new NoSuchElementException ("no more elements");
279: Attribute at = attributes.get (where);
280: ++where;
281: return at;
282: }
283: }
284: }