API Overview API Index Package Overview Direct link to this page
JDK 1.6
  javax.naming.directory. BasicAttribute View Javadoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539

/*
 * @(#)BasicAttribute.java	1.14 05/11/17
 *
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package javax.naming.directory;

import java.util.Vector;
import java.util.Enumeration;
import java.util.NoSuchElementException;
import java.lang.reflect.Array;

import javax.naming.NamingException;
import javax.naming.NamingEnumeration;
import javax.naming.OperationNotSupportedException;

/**
  * This class provides a basic implementation of the <tt>Attribute</tt> interface.
  *<p>
  * This implementation does not support the schema methods
  * <tt>getAttributeDefinition()</tt> and <tt>getAttributeSyntaxDefinition()</tt>.
  * They simply throw <tt>OperationNotSupportedException</tt>.
  * Subclasses of <tt>BasicAttribute</tt> should override these methods if they 
  * support them.
  *<p>
  * The <tt>BasicAttribute</tt> class by default uses <tt>Object.equals()</tt> to 
  * determine equality of attribute values when testing for equality or 
  * when searching for values, <em>except</em> when the value is an array.
  * For an array, each element of the array is checked using <tt>Object.equals()</tt>.
  * Subclasses of <tt>BasicAttribute</tt> can make use of schema information
  * when doing similar equality checks by overriding methods
  * in which such use of schema is meaningful.
  * Similarly, the <tt>BasicAttribute</tt> class by default returns the values passed to its
  * constructor and/or manipulated using the add/remove methods.
  * Subclasses of <tt>BasicAttribute</tt> can override <tt>get()</tt> and <tt>getAll()</tt>
  * to get the values dynamically from the directory (or implement 
  * the <tt>Attribute</tt> interface directly instead of subclassing <tt>BasicAttribute</tt>).
  *<p>
  * Note that updates to <tt>BasicAttribute</tt> (such as adding or removing a value)
  * does not affect the corresponding representation of the attribute
  * in the directory.  Updates to the directory can only be effected
  * using operations in the <tt>DirContext</tt> interface.
  *<p>
  * A <tt>BasicAttribute</tt> instance is not synchronized against concurrent
  * multithreaded access. Multiple threads trying to access and modify a
  * <tt>BasicAttribute</tt> should lock the object.
  *
  * @author Rosanna Lee
  * @author Scott Seligman
  * @version 1.14 05/11/17
  * @since 1.3
  */
public class BasicAttribute implements Attribute {
    /**
     * Holds the attribute's id. It is initialized by the public constructor and 
     * cannot be null unless methods in BasicAttribute that use attrID
     * have been overridden.
     * @serial
     */
    protected String attrID;

    /**
     * Holds the attribute's values. Initialized by public constructors.
     * Cannot be null unless methods in BasicAttribute that use
     * values have been overridden.
     */
    protected transient Vector<Object> values;

    /**
     * A flag for recording whether this attribute's values are ordered.
     * @serial
     */
    protected boolean ordered = false;

    public Object clone() {
	BasicAttribute attr;
	try {
	    attr = (BasicAttribute)super.clone();
	} catch (CloneNotSupportedException e) {
	    attr = new BasicAttribute(attrID, ordered);
	}
	attr.values = (Vector)values.clone();
	return attr;
    }

    /**
      * Determines whether obj is equal to this attribute.
      * Two attributes are equal if their attribute-ids, syntaxes
      * and values are equal. 
      * If the attribute values are unordered, the order that the values were added
      * are irrelevant. If the attribute values are ordered, then the
      * order the values must match.
      * If obj is null or not an Attribute, false is returned.
      *<p>
      * By default <tt>Object.equals()</tt> is used when comparing the attribute
      * id and its values except when a value is an array. For an array, 
      * each element of the array is checked using <tt>Object.equals()</tt>.
      * A subclass may override this to make
      * use of schema syntax information and matching rules, 
      * which define what it means for two attributes to be equal. 
      * How and whether a subclass makes
      * use of the schema information is determined by the subclass.
      * If a subclass overrides <tt>equals()</tt>, it should also override 
      * <tt>hashCode()</tt>
      * such that two attributes that are equal have the same hash code.
      *
      * @param obj	The possibly null object to check.
      * @return true if obj is equal to this attribute; false otherwise.
      * @see #hashCode
      * @see #contains
      */
    public boolean equals(Object obj) {
	if ((obj != null) && (obj instanceof Attribute)) {
	    Attribute target = (Attribute)obj;
	    
	    // Check order first
	    if (isOrdered() != target.isOrdered()) {
		return false;
	    }
	    int len;
	    if (attrID.equals(target.getID()) &&
	        (len=size()) == target.size()) {
		try {
		    if (isOrdered()) {
			// Go through both list of values
			for (int i = 0; i < len; i++) {
			    if (!valueEquals(get(i), target.get(i))) {
				return false;
			    }
			}
		    } else {
			// order is not relevant; check for existence
			Enumeration theirs = target.getAll();
			while (theirs.hasMoreElements()) {
			    if (find(theirs.nextElement()) < 0)
				return false;
			}
		    }
		} catch (NamingException e) {
		    return false;
		}
		return true;
	    }
	}
	return false;
    }

    /**
      * Calculates the hash code of this attribute.
      *<p>
      * The hash code is computed by adding the hash code of
      * the attribute's id and that of all of its values except for
      * values that are arrays.
      * For an array, the hash code of each element of the array is summed.
      * If a subclass overrides <tt>hashCode()</tt>, it should override 
      * <tt>equals()</tt>
      * as well so that two attributes that are equal have the same hash code.
      *
      * @return an int representing the hash code of this attribute.
      * @see #equals
      */
    public int hashCode() {
	int hash = attrID.hashCode();
	int num = values.size();
	Object val;
	for (int i = 0; i < num; i ++) {
	    val = values.elementAt(i);
	    if (val != null) {
		if (val.getClass().isArray()) {
		    Object it;
		    int len = Array.getLength(val);
		    for (int j = 0 ; j < len ; j++) {
			it = Array.get(val, j);
			if (it != null) {
			    hash += it.hashCode();
			}
		    }
		} else {
		    hash += val.hashCode();
		}
	    }
	}
	return hash;
    }

    /**
      * Generates the string representation of this attribute.
      * The string consists of the attribute's id and its values.
      * This string is meant for debugging and not meant to be 
      * interpreted programmatically.
      * @return The non-null string representation of this attribute.
      */
    public String toString() {
	StringBuffer answer = new StringBuffer(attrID + ": ");
	if (values.size() == 0) {
	    answer.append("No values");
	} else {
	    boolean start = true;
	    for (Enumeration e = values.elements(); e.hasMoreElements(); ) {
		if (!start)
		    answer.append(", ");
		answer.append(e.nextElement());
		start = false;
	    }
	}
	return answer.toString();
    }

    /**
      * Constructs a new instance of an unordered attribute with no value.
      *
      * @param id The attribute's id. It cannot be null.
      */
    public BasicAttribute(String id) {
	this(id, false);
    }

    /**
      * Constructs a new instance of an unordered attribute with a single value.
      *
      * @param id The attribute's id. It cannot be null.
      * @param value The attribute's value. If null, a null
      *        value is added to the attribute.
      */
    public BasicAttribute(String id, Object value) {
	this(id, value, false);
    }

    /**
      * Constructs a new instance of a possibly ordered attribute with no value.
      *
      * @param id The attribute's id. It cannot be null.
      * @param ordered true means the attribute's values will be ordered; 
      * false otherwise.
      */
    public BasicAttribute(String id, boolean ordered) {
	attrID = id;
	values = new Vector();
	this.ordered = ordered;
    }

    /**
      * Constructs a new instance of a possibly ordered attribute with a 
      * single value.
      *
      * @param id The attribute's id. It cannot be null.
      * @param value The attribute's value. If null, a null
      *        value is added to the attribute.
      * @param ordered true means the attribute's values will be ordered; 
      * false otherwise.
      */
    public BasicAttribute(String id, Object value, boolean ordered) {
	this(id, ordered);
	values.addElement(value);
    }

    /**
      * Retrieves an enumeration of this attribute's values.
      *<p>
      * By default, the values returned are those passed to the
      * constructor and/or manipulated using the add/replace/remove methods.
      * A subclass may override this to retrieve the values dynamically
      * from the directory.
      */
    public NamingEnumeration<?> getAll() throws NamingException {
      return new ValuesEnumImpl();
    }

    /**
      * Retrieves one of this attribute's values.
      *<p>
      * By default, the value returned is one of those passed to the
      * constructor and/or manipulated using the add/replace/remove methods.
      * A subclass may override this to retrieve the value dynamically
      * from the directory.
      */
    public Object get() throws NamingException {
	if (values.size() == 0) {
	    throw new 
	NoSuchElementException("Attribute " + getID() + " has no value");
	} else {
	    return values.elementAt(0);
	}
    }

    public int size() {
      return values.size();
    }

    public String getID() {
	return attrID;
    }

    /**
      * Determines whether a value is in this attribute.
      *<p>
      * By default, 
      * <tt>Object.equals()</tt> is used when comparing <tt>attrVal</tt>
      * with this attribute's values except when <tt>attrVal</tt> is an array.
      * For an array, each element of the array is checked using 
      * <tt>Object.equals()</tt>.
      * A subclass may use schema information to determine equality.
      */
    public boolean contains(Object attrVal) {
	return (find(attrVal) >= 0);
    }

    // For finding first element that has a null in JDK1.1 Vector.
    // In the Java 2 platform, can just replace this with Vector.indexOf(target);
    private int find(Object target) {
	Class cl;
	if (target == null) {
	    int ct = values.size();
	    for (int i = 0 ; i < ct ; i++) {
		if (values.elementAt(i) == null)
		    return i; 
	    }
	} else if ((cl=target.getClass()).isArray()) {
	    int ct = values.size();
	    Object it;
	    for (int i = 0 ; i < ct ; i++) {
		it = values.elementAt(i);
		if (it != null && cl == it.getClass() 
		    && arrayEquals(target, it))
		    return i;
	    }
	} else {
	    return values.indexOf(target, 0);
	}
	return -1;  // not found
    }

    /**
     * Determines whether two attribute values are equal.
     * Use arrayEquals for arrays and <tt>Object.equals()</tt> otherwise.
     */
    private static boolean valueEquals(Object obj1, Object obj2) {
	if (obj1 == obj2) {
	    return true; // object references are equal
	}
	if (obj1 == null) {
	    return false; // obj2 was not false
	}
	if (obj1.getClass().isArray() &&
	    obj2.getClass().isArray()) {
	    return arrayEquals(obj1, obj2);
	}
	return (obj1.equals(obj2));
    }

    /**
     * Determines whether two arrays are equal by comparing each of their
     * elements using <tt>Object.equals()</tt>.
     */
    private static boolean arrayEquals(Object a1, Object a2) {
	int len;
	if ((len = Array.getLength(a1)) != Array.getLength(a2))
	    return false;

	for (int j = 0; j < len; j++) {
	    Object i1 = Array.get(a1, j);
	    Object i2 = Array.get(a2, j);
	    if (i1 == null || i2 == null) {
		if (i1 != i2)
		    return false;
	    } else if (!i1.equals(i2)) {
		return false;
	    }
	}
	return true;
    }

    /**
      * Adds a new value to this attribute. 
      *<p>
      * By default, <tt>Object.equals()</tt> is used when comparing <tt>attrVal</tt>
      * with this attribute's values except when <tt>attrVal</tt> is an array.
      * For an array, each element of the array is checked using 
      * <tt>Object.equals()</tt>. 
      * A subclass may use schema information to determine equality.
      */
    public boolean add(Object attrVal) {
	if (isOrdered() || (find(attrVal) < 0)) {
	    values.addElement(attrVal);
	    return true;
	} else {
	    return false;
	}
    }

    /**
      * Removes a specified value from this attribute.
      *<p>
      * By default, <tt>Object.equals()</tt> is used when comparing <tt>attrVal</tt>
      * with this attribute's values except when <tt>attrVal</tt> is an array.
      * For an array, each element of the array is checked using 
      * <tt>Object.equals()</tt>. 
      * A subclass may use schema information to determine equality.
      */
    public boolean remove(Object attrval) {
	// For the Java 2 platform, can just use "return removeElement(attrval);"
	// Need to do the following to handle null case

	int i = find(attrval);
	if (i >= 0) {
	    values.removeElementAt(i);
	    return true;
	}
	return false;
    }

    public void clear() {
	values.setSize(0);
    }

//  ---- ordering methods

    public boolean isOrdered() {
	return ordered;
    }

    public Object get(int ix) throws NamingException {
	return values.elementAt(ix);
    }

    public Object remove(int ix) {
	Object answer = values.elementAt(ix);
	values.removeElementAt(ix);
	return answer;
    }

    public void add(int ix, Object attrVal) {
	if (!isOrdered() && contains(attrVal)) {
	    throw new IllegalStateException(
		"Cannot add duplicate to unordered attribute");
	}
	values.insertElementAt(attrVal, ix);
    }

    public Object set(int ix, Object attrVal) {
	if (!isOrdered() && contains(attrVal)) {
	    throw new IllegalStateException(
		"Cannot add duplicate to unordered attribute");
	}

	Object answer = values.elementAt(ix);
	values.setElementAt(attrVal, ix);
	return answer;
    }

// ----------------- Schema methods

    /**
      * Retrieves the syntax definition associated with this attribute.
      *<p>
      * This method by default throws OperationNotSupportedException. A subclass
      * should override this method if it supports schema.
      */
    public DirContext getAttributeSyntaxDefinition() throws NamingException {
	    throw new OperationNotSupportedException("attribute syntax");
    }

    /**
      * Retrieves this attribute's schema definition.
      *<p>
      * This method by default throws OperationNotSupportedException. A subclass
      * should override this method if it supports schema.
      */ 
    public DirContext getAttributeDefinition() throws NamingException {
	throw new OperationNotSupportedException("attribute definition");
    }


//  ---- serialization methods

    /**
     * Overridden to avoid exposing implementation details
     * @serialData Default field (the attribute ID -- a String), 
     * followed by the number of values (an int), and the
     * individual values.
     */
    private void writeObject(java.io.ObjectOutputStream s)
	    throws java.io.IOException {
	s.defaultWriteObject();	// write out the attrID
	s.writeInt(values.size());
	for (int i = 0; i < values.size(); i++) {
	    s.writeObject(values.elementAt(i));
	}
    }

    /**
     * Overridden to avoid exposing implementation details.
     */
    private void readObject(java.io.ObjectInputStream s)
	    throws java.io.IOException, ClassNotFoundException {
        s.defaultReadObject();	// read in the attrID
	int n = s.readInt();	// number of values
	values = new Vector(n);
	while (--n >= 0) {
	    values.addElement(s.readObject());
	}
    }


    class ValuesEnumImpl implements NamingEnumeration<Object> {
    Enumeration list;

    ValuesEnumImpl() {
	list = values.elements();
    }

    public boolean hasMoreElements() {
	return list.hasMoreElements();
    }

    public Object nextElement() {
	return(list.nextElement());
    }

    public Object next() throws NamingException {
	return list.nextElement();
    }

    public boolean hasMore() throws NamingException {
	return list.hasMoreElements();
    }

    public void close() throws NamingException {
	list = null;
    }
    }

    /**
     * Use serialVersionUID from JNDI 1.1.1 for interoperability.
     */
    private static final long serialVersionUID = 6743528196119291326L;
}

Generated By: JavaOnTracks Doclet 0.1.4     ©Thibaut Colar