1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43:
44: import ;
45: import ;
46:
47:
51: public class BasicAttributes implements Attributes
52: {
53: private static final long serialVersionUID = 4980164073184639448L;
54:
55: public BasicAttributes ()
56: {
57: this (false);
58: }
59:
60: public BasicAttributes (boolean ignoreCase)
61: {
62: this.ignoreCase = ignoreCase;
63: this.attributes = new Vector ();
64: }
65:
66: public BasicAttributes (String attrID, Object val)
67: {
68: this (attrID, val, false);
69: }
70:
71: public BasicAttributes (String attrID, Object val, boolean ignoreCase)
72: {
73: this.ignoreCase = ignoreCase;
74: attributes = new Vector ();
75: attributes.add (new BasicAttribute (attrID, val));
76: }
77:
78: public Object clone ()
79: {
80:
81: BasicAttributes ba = new BasicAttributes (ignoreCase);
82: ba.attributes = (Vector) attributes.clone ();
83: return ba;
84: }
85:
86:
91: public boolean equals (Object obj)
92: {
93: if (! (obj instanceof Attributes))
94: return false;
95:
96: Attributes bs = (Attributes) obj;
97: if (ignoreCase != bs.isCaseIgnored()
98: || attributes.size () != bs.size ())
99: return false;
100:
101: NamingEnumeration bas = bs.getAll();
102: while (bas.hasMoreElements())
103: {
104: Attribute a = (Attribute) bas.nextElement();
105: Attribute b = get(a.getID ());
106: if (! a.equals(b))
107: return false;
108: }
109:
110: return true;
111: }
112:
113: public Attribute get (String attrID)
114: {
115: for (int i = 0; i < attributes.size (); ++i)
116: {
117: Attribute at = (Attribute) attributes.get (i);
118: if ((ignoreCase && attrID.equalsIgnoreCase (at.getID ()))
119: || (! ignoreCase && attrID.equals (at.getID ())))
120: return at;
121: }
122:
123: return null;
124: }
125:
126: public NamingEnumeration getAll ()
127: {
128: return new BasicAttributesEnumeration (false);
129: }
130:
131: public NamingEnumeration getIDs ()
132: {
133: return new BasicAttributesEnumeration (true);
134: }
135:
136: public int hashCode ()
137: {
138: int val = 0;
139: for (int i = 0; i < attributes.size (); ++i)
140: val += attributes.get (i).hashCode ();
141: return val;
142: }
143:
144: public boolean isCaseIgnored ()
145: {
146: return ignoreCase;
147: }
148:
149: public Attribute put (Attribute attr)
150: {
151: Attribute r = remove (attr.getID ());
152: attributes.add (attr);
153: return r;
154: }
155:
156: public Attribute put (String attrID, Object val)
157: {
158: return put (new BasicAttribute (attrID, val));
159: }
160:
161: public Attribute remove (String attrID)
162: {
163: for (int i = 0; i < attributes.size (); ++i)
164: {
165: Attribute at = (Attribute) attributes.get (i);
166: if ((ignoreCase && attrID.equalsIgnoreCase (at.getID ()))
167: || (! ignoreCase && attrID.equals (at.getID ())))
168: {
169: attributes.remove (i);
170: return at;
171: }
172: }
173:
174: return null;
175: }
176:
177: public int size ()
178: {
179: return attributes.size ();
180: }
181:
182: public String toString ()
183: {
184: String r = "";
185: for (int i = 0; i < attributes.size (); ++i)
186: {
187: if (i > 0)
188: r += "; ";
189: r += attributes.get (i).toString ();
190: }
191: return r;
192: }
193:
194:
195: private boolean ignoreCase;
196:
197: transient Vector attributes;
198:
199:
200: private class BasicAttributesEnumeration implements NamingEnumeration
201: {
202: int where = 0;
203: boolean id;
204:
205: public BasicAttributesEnumeration (boolean id)
206: {
207: this.id = id;
208: }
209:
210: public void close () throws NamingException
211: {
212: }
213:
214: public boolean hasMore () throws NamingException
215: {
216: return hasMoreElements ();
217: }
218:
219: public Object next () throws NamingException
220: {
221: return nextElement ();
222: }
223:
224: public boolean hasMoreElements ()
225: {
226: return where < attributes.size ();
227: }
228:
229: public Object nextElement () throws NoSuchElementException
230: {
231: if (where >= attributes.size ())
232: throw new NoSuchElementException ("no more elements");
233: Attribute at = (Attribute) attributes.get (where);
234: ++where;
235: return id ? (Object) at.getID () : (Object) at;
236: }
237: }
238: }