KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > naming > directory > BasicAttributes


1 /*
2  * @(#)BasicAttributes.java 1.12 04/05/05
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8
9 package javax.naming.directory;
10
11 import java.util.Hashtable JavaDoc;
12 import java.util.Enumeration JavaDoc;
13
14 import javax.naming.NamingException JavaDoc;
15 import javax.naming.NamingEnumeration JavaDoc;
16
17 /**
18   * This class provides a basic implementation
19   * of the Attributes interface.
20   *<p>
21   * BasicAttributes is either case-sensitive or case-insensitive (case-ignore).
22   * This property is determined at the time the BasicAttributes constructor
23   * is called.
24   * In a case-insensitive BasicAttributes, the case of its attribute identifiers
25   * is ignored when searching for an attribute, or adding attributes.
26   * In a case-sensitive BasicAttributes, the case is significant.
27   *<p>
28   * When the BasicAttributes class needs to create an Attribute, it
29   * uses BasicAttribute. There is no other dependency on BasicAttribute.
30   *<p>
31   * Note that updates to BasicAttributes (such as adding or removing an attribute)
32   * does not affect the corresponding representation in the directory.
33   * Updates to the directory can only be effected
34   * using operations in the DirContext interface.
35   *<p>
36   * A BasicAttributes instance is not synchronized against concurrent
37   * multithreaded access. Multiple threads trying to access and modify
38   * a single BasicAttributes instance should lock the object.
39   *
40   * @author Rosanna Lee
41   * @author Scott Seligman
42   * @version 1.12 04/05/05
43   *
44   * @see DirContext#getAttributes
45   * @see DirContext#modifyAttributes
46   * @see DirContext#bind
47   * @see DirContext#rebind
48   * @see DirContext#createSubcontext
49   * @see DirContext#search
50   * @since 1.3
51   */

52
53 public class BasicAttributes implements Attributes JavaDoc {
54     /**
55      * Indicates whether case of attribute ids is ignored.
56      * @serial
57      */

58     private boolean ignoreCase = false;
59
60     // The 'key' in attrs is stored in the 'right case'.
61
// If ignoreCase is true, key is aways lowercase.
62
// If ignoreCase is false, key is stored as supplied by put().
63
// %%% Not declared "private" due to bug 4064984.
64
transient Hashtable JavaDoc attrs = new Hashtable JavaDoc(11);
65
66     /**
67       * Constructs a new instance of Attributes.
68       * The character case of attribute identifiers
69       * is significant when subsequently retrieving or adding attributes.
70       */

71     public BasicAttributes() {
72     }
73
74     /**
75       * Constructs a new instance of Attributes.
76       * If <code>ignoreCase</code> is true, the character case of attribute
77       * identifiers is ignored; otherwise the case is significant.
78       * @param ignoreCase true means this attribute set will ignore
79       * the case of its attribute identifiers
80       * when retrieving or adding attributes;
81       * false means case is respected.
82       */

83     public BasicAttributes(boolean ignoreCase) {
84     this.ignoreCase = ignoreCase;
85     }
86
87     /**
88       * Constructs a new instance of Attributes with one attribute.
89       * The attribute specified by attrID and val are added to the newly
90       * created attribute.
91       * The character case of attribute identifiers
92       * is significant when subsequently retrieving or adding attributes.
93       * @param attrID non-null The id of the attribute to add.
94       * @param val The value of the attribute to add. If null, a null
95       * value is added to the attribute.
96       */

97     public BasicAttributes(String JavaDoc attrID, Object JavaDoc val) {
98     this();
99     this.put(new BasicAttribute JavaDoc(attrID, val));
100     }
101
102     /**
103       * Constructs a new instance of Attributes with one attribute.
104       * The attribute specified by attrID and val are added to the newly
105       * created attribute.
106       * If <code>ignoreCase</code> is true, the character case of attribute
107       * identifiers is ignored; otherwise the case is significant.
108       * @param attrID non-null The id of the attribute to add.
109       * If this attribute set ignores the character
110       * case of its attribute ids, the case of attrID
111       * is ignored.
112       * @param val The value of the attribute to add. If null, a null
113       * value is added to the attribute.
114       * @param ignoreCase true means this attribute set will ignore
115       * the case of its attribute identifiers
116       * when retrieving or adding attributes;
117       * false means case is respected.
118       */

119     public BasicAttributes(String JavaDoc attrID, Object JavaDoc val, boolean ignoreCase) {
120     this(ignoreCase);
121     this.put(new BasicAttribute JavaDoc(attrID, val));
122     }
123
124     public Object JavaDoc clone() {
125     BasicAttributes JavaDoc attrset;
126     try {
127         attrset = (BasicAttributes JavaDoc)super.clone();
128     } catch (CloneNotSupportedException JavaDoc e) {
129         attrset = new BasicAttributes JavaDoc(ignoreCase);
130     }
131     attrset.attrs = (Hashtable JavaDoc)attrs.clone();
132     return attrset;
133     }
134
135     public boolean isCaseIgnored() {
136     return ignoreCase;
137     }
138
139     public int size() {
140     return attrs.size();
141     }
142
143     public Attribute JavaDoc get(String JavaDoc attrID) {
144     Attribute JavaDoc attr = (Attribute JavaDoc) attrs.get(
145         ignoreCase ? attrID.toLowerCase() : attrID);
146     return (attr);
147     }
148
149     public NamingEnumeration JavaDoc<Attribute JavaDoc> getAll() {
150     return new AttrEnumImpl();
151     }
152
153     public NamingEnumeration JavaDoc<String JavaDoc> getIDs() {
154     return new IDEnumImpl();
155     }
156
157     public Attribute JavaDoc put(String JavaDoc attrID, Object JavaDoc val) {
158     return (Attribute JavaDoc)this.put(new BasicAttribute JavaDoc(attrID, val));
159     }
160
161     public Attribute JavaDoc put(Attribute JavaDoc attr) {
162     String JavaDoc id = attr.getID();
163     if (ignoreCase) {
164         id = id.toLowerCase();
165     }
166     return (Attribute JavaDoc)attrs.put(id, attr);
167     }
168
169     public Attribute JavaDoc remove(String JavaDoc attrID) {
170     String JavaDoc id = (ignoreCase ? attrID.toLowerCase() : attrID);
171     return (Attribute JavaDoc)attrs.remove(id);
172     }
173
174     /**
175      * Generates the string representation of this attribute set.
176      * The string consists of each attribute identifier and the contents
177      * of each attribute. The contents of this string is useful
178      * for debugging and is not meant to be interpreted programmatically.
179      *
180      * @return A non-null string listing the contents of this attribute set.
181      */

182     public String JavaDoc toString() {
183     if (attrs.size() == 0) {
184         return("No attributes");
185     } else {
186         return attrs.toString();
187     }
188     }
189
190     /**
191      * Determines whether this <tt>BasicAttributes</tt> is equal to another
192      * <tt>Attributes</tt>
193      * Two <tt>Attributes</tt> are equal if they are both instances of
194      * <tt>Attributes</tt>,
195      * treat the case of attribute IDs the same way, and contain the
196      * same attributes. Each <tt>Attribute</tt> in this <tt>BasicAttributes</tt>
197      * is checked for equality using <tt>Object.equals()</tt>, which may have
198      * be overridden by implementations of <tt>Attribute</tt>).
199      * If a subclass overrides <tt>equals()</tt>,
200      * it should override <tt>hashCode()</tt>
201      * as well so that two <tt>Attributes</tt> instances that are equal
202      * have the same hash code.
203      * @param obj the possibly null object to compare against.
204      *
205      * @return true If obj is equal to this BasicAttributes.
206      * @see #hashCode
207      */

208     public boolean equals(Object JavaDoc obj) {
209     if ((obj != null) && (obj instanceof Attributes JavaDoc)) {
210         Attributes JavaDoc target = (Attributes JavaDoc)obj;
211         
212         // Check case first
213
if (ignoreCase != target.isCaseIgnored()) {
214         return false;
215         }
216
217         if (size() == target.size()) {
218         Attribute JavaDoc their, mine;
219         try {
220             NamingEnumeration JavaDoc theirs = target.getAll();
221             while (theirs.hasMore()) {
222             their = (Attribute JavaDoc)theirs.next();
223             mine = get(their.getID());
224             if (!their.equals(mine)) {
225                 return false;
226             }
227             }
228         } catch (NamingException JavaDoc e) {
229             return false;
230         }
231         return true;
232         }
233     }
234     return false;
235     }
236
237     /**
238      * Calculates the hash code of this BasicAttributes.
239      *<p>
240      * The hash code is computed by adding the hash code of
241      * the attributes of this object. If this BasicAttributes
242      * ignores case of its attribute IDs, one is added to the hash code.
243      * If a subclass overrides <tt>hashCode()</tt>,
244      * it should override <tt>equals()</tt>
245      * as well so that two <tt>Attributes</tt> instances that are equal
246      * have the same hash code.
247      *
248      * @return an int representing the hash code of this BasicAttributes instance.
249      * @see #equals
250      */

251     public int hashCode() {
252     int hash = (ignoreCase ? 1 : 0);
253     try {
254         NamingEnumeration JavaDoc all = getAll();
255         while (all.hasMore()) {
256         hash += all.next().hashCode();
257         }
258     } catch (NamingException JavaDoc e) {}
259     return hash;
260     }
261
262     /**
263      * Overridden to avoid exposing implementation details.
264      * @serialData Default field (ignoreCase flag -- a boolean), followed by
265      * the number of attributes in the set
266      * (an int), and then the individual Attribute objects.
267      */

268     private void writeObject(java.io.ObjectOutputStream JavaDoc s)
269         throws java.io.IOException JavaDoc {
270     s.defaultWriteObject(); // write out the ignoreCase flag
271
s.writeInt(attrs.size());
272     Enumeration JavaDoc attrEnum = attrs.elements();
273     while (attrEnum.hasMoreElements()) {
274         s.writeObject(attrEnum.nextElement());
275     }
276     }
277
278     /**
279      * Overridden to avoid exposing implementation details.
280      */

281     private void readObject(java.io.ObjectInputStream JavaDoc s)
282         throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
283         s.defaultReadObject(); // read in the ignoreCase flag
284
int n = s.readInt(); // number of attributes
285
attrs = (n >= 1)
286         ? new Hashtable JavaDoc(n * 2)
287         : new Hashtable JavaDoc(2); // can't have initial size of 0 (grrr...)
288
while (--n >= 0) {
289         put((Attribute JavaDoc)s.readObject());
290     }
291     }
292
293
294 class AttrEnumImpl implements NamingEnumeration JavaDoc<Attribute JavaDoc> {
295
296     Enumeration JavaDoc<Attribute JavaDoc> elements;
297
298     public AttrEnumImpl() {
299     this.elements = attrs.elements();
300     }
301
302     public boolean hasMoreElements() {
303     return elements.hasMoreElements();
304     }
305
306     public Attribute JavaDoc nextElement() {
307     return elements.nextElement();
308     }
309
310     public boolean hasMore() throws NamingException JavaDoc {
311     return hasMoreElements();
312     }
313
314     public Attribute JavaDoc next() throws NamingException JavaDoc {
315     return nextElement();
316     }
317
318     public void close() throws NamingException JavaDoc {
319     elements = null;
320     }
321 }
322
323 class IDEnumImpl implements NamingEnumeration JavaDoc<String JavaDoc> {
324
325     Enumeration JavaDoc<Attribute JavaDoc> elements;
326
327     public IDEnumImpl() {
328     // Walking through the elements, rather than the keys, gives
329
// us attribute IDs that have not been converted to lowercase.
330
this.elements = attrs.elements();
331     }
332
333     public boolean hasMoreElements() {
334     return elements.hasMoreElements();
335     }
336
337     public String JavaDoc nextElement() {
338     Attribute JavaDoc attr = (Attribute JavaDoc)elements.nextElement();
339     return attr.getID();
340     }
341
342     public boolean hasMore() throws NamingException JavaDoc {
343     return hasMoreElements();
344     }
345
346     public String JavaDoc next() throws NamingException JavaDoc {
347     return nextElement();
348     }
349
350     public void close() throws NamingException JavaDoc {
351     elements = null;
352     }
353 }
354
355     /**
356      * Use serialVersionUID from JNDI 1.1.1 for interoperability.
357      */

358     private static final long serialVersionUID = 4980164073184639448L;
359 }
360
Popular Tags