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 ();
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 ();
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) 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 = (Attribute) 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 getAll ()
130: {
131: return new BasicAttributesEnumeration (false);
132: }
133:
134: public NamingEnumeration getIDs ()
135: {
136: return new BasicAttributesEnumeration (true);
137: }
138:
139: public int hashCode ()
140: {
141: int val = 0;
142: for (int i = 0; i < attributes.size (); ++i)
143: val += attributes.get (i).hashCode ();
144: return val;
145: }
146:
147: public boolean isCaseIgnored ()
148: {
149: return ignoreCase;
150: }
151:
152: public Attribute put (Attribute attr)
153: {
154: Attribute r = remove (attr.getID ());
155: attributes.add (attr);
156: return r;
157: }
158:
159: public Attribute put (String attrID, Object val)
160: {
161: return put (new BasicAttribute (attrID, val));
162: }
163:
164: public Attribute remove (String attrID)
165: {
166: for (int i = 0; i < attributes.size (); ++i)
167: {
168: Attribute at = (Attribute) attributes.get (i);
169: if ((ignoreCase && attrID.equalsIgnoreCase (at.getID ()))
170: || (! ignoreCase && attrID.equals (at.getID ())))
171: {
172: attributes.remove (i);
173: return at;
174: }
175: }
176:
177: return null;
178: }
179:
180: public int size ()
181: {
182: return attributes.size ();
183: }
184:
185: public String toString ()
186: {
187: String r = "";
188: for (int i = 0; i < attributes.size (); ++i)
189: {
190: if (i > 0)
191: r += "; ";
192: r += attributes.get (i).toString ();
193: }
194: return r;
195: }
196:
197:
198: private boolean ignoreCase;
199:
200: transient Vector attributes;
201:
202: private void readObject(ObjectInputStream s) throws IOException,
203: ClassNotFoundException
204: {
205: s.defaultReadObject();
206: int size = s.readInt();
207: attributes = new Vector(size);
208: for (int i = 0; i < size; i++)
209: attributes.add(s.readObject());
210: }
211:
212: private void writeObject(ObjectOutputStream s) throws IOException
213: {
214: s.defaultWriteObject();
215: s.writeInt(attributes.size());
216: for (int i = 0; i < attributes.size(); i++)
217: s.writeObject(attributes.get(i));
218: }
219:
220:
221: private class BasicAttributesEnumeration implements NamingEnumeration
222: {
223: int where = 0;
224: boolean id;
225:
226: public BasicAttributesEnumeration (boolean id)
227: {
228: this.id = id;
229: }
230:
231: public void close () throws NamingException
232: {
233: }
234:
235: public boolean hasMore () throws NamingException
236: {
237: return hasMoreElements ();
238: }
239:
240: public Object next () throws NamingException
241: {
242: return nextElement ();
243: }
244:
245: public boolean hasMoreElements ()
246: {
247: return where < attributes.size ();
248: }
249:
250: public Object nextElement () throws NoSuchElementException
251: {
252: if (where >= attributes.size ())
253: throw new NoSuchElementException ("no more elements");
254: Attribute at = (Attribute) attributes.get (where);
255: ++where;
256: return id ? (Object) at.getID () : (Object) at;
257: }
258: }
259: }