1 /* BasicAttribute.java --
2 Copyright (C) 2000, 2001, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax
.naming
.directory
;
41 import java
.util
.NoSuchElementException
;
42 import java
.util
.Vector
;
44 import javax
.naming
.NamingEnumeration
;
45 import javax
.naming
.NamingException
;
46 import javax
.naming
.OperationNotSupportedException
;
49 * @author Tom Tromey (tromey@redhat.com)
52 public class BasicAttribute
implements Attribute
54 private static final long serialVersionUID
= 6743528196119291326L;
56 /** The ID of this attribute. */
57 protected String attrID
;
58 /** True if this attribute's values are ordered. */
59 protected boolean ordered
;
60 /** Values for this attribute. */
61 protected transient Vector values
;
64 private BasicAttribute ()
68 public BasicAttribute (String id
)
73 public BasicAttribute (String id
, boolean ordered
)
76 this.ordered
= ordered
;
77 values
= new Vector ();
80 public BasicAttribute (String id
, Object value
)
82 this (id
, value
, false);
85 public BasicAttribute (String id
, Object value
, boolean ordered
)
88 this.ordered
= ordered
;
89 values
= new Vector ();
93 public void add (int index
, Object val
)
95 if (! ordered
&& contains (val
))
96 throw new IllegalStateException ("value already in attribute");
97 values
.add (index
, val
);
100 public boolean add (Object val
)
102 if (! ordered
&& contains (val
))
103 throw new IllegalStateException ("value already in attribute");
104 return values
.add (val
);
112 public Object
clone ()
114 BasicAttribute c
= new BasicAttribute ();
117 c
.values
= (Vector
) values
.clone ();
121 public boolean contains (Object val
)
123 for (int i
= 0; i
< values
.size (); ++i
)
125 if (equals (val
, values
.get (i
)))
132 public boolean equals (Object obj
)
134 if (! (obj
instanceof BasicAttribute
))
136 BasicAttribute b
= (BasicAttribute
) obj
;
138 if (ordered
!= b
.ordered
139 || ! attrID
.equals (b
.attrID
)
140 || values
.size () != b
.values
.size ())
143 for (int i
= 0; i
< values
.size (); ++i
)
147 ok
= equals (values
.get (i
), b
.values
.get (i
));
150 for (int j
= 0; j
< b
.values
.size (); ++j
)
152 if (equals (values
.get (i
), b
.values
.get (j
)))
168 throws NamingException
170 if (values
.size () == 0)
171 throw new NoSuchElementException ("no values");
175 public Object
get (int index
)
176 throws NamingException
178 return values
.get (index
);
181 public NamingEnumeration
getAll ()
182 throws NamingException
184 return new BasicAttributeEnumeration ();
187 public DirContext
getAttributeDefinition ()
188 throws OperationNotSupportedException
, NamingException
190 throw new OperationNotSupportedException ();
193 public DirContext
getAttributeSyntaxDefinition ()
194 throws OperationNotSupportedException
, NamingException
196 throw new OperationNotSupportedException ();
199 public String
getID ()
204 public int hashCode ()
206 int val
= attrID
.hashCode ();
207 for (int i
= 0; i
< values
.size (); ++i
)
209 Object o
= values
.get (i
);
214 else if (o
instanceof Object
[])
216 Object
[] a
= (Object
[]) o
;
217 for (int j
= 0; j
< a
.length
; ++j
)
218 val
+= a
[j
].hashCode ();
221 val
+= o
.hashCode ();
227 public boolean isOrdered ()
232 public Object
remove (int index
)
234 return values
.remove (index
);
237 public boolean remove (Object val
)
239 for (int i
= 0; i
< values
.size (); ++i
)
241 if (equals (val
, values
.get (i
)))
251 public Object
set (int index
, Object val
)
253 if (! ordered
&& contains (val
))
254 throw new IllegalStateException ("value already in attribute");
255 return values
.set (index
, val
);
260 return values
.size ();
263 public String
toString ()
266 for (int i
= 0; i
< values
.size (); ++i
)
267 r
+= ";" + values
.get (i
).toString ();
271 // This is used for testing equality of two Objects according to our
273 private boolean equals (Object one
, Object two
)
278 if (one
instanceof Object
[])
280 if (! (two
instanceof Object
[]))
283 Object
[] aone
= (Object
[]) one
;
284 Object
[] atwo
= (Object
[]) two
;
286 if (aone
.length
!= atwo
.length
)
289 for (int i
= 0; i
< aone
.length
; ++i
)
291 if (! aone
[i
].equals (atwo
[i
]))
298 return one
.equals (two
);
301 // Used when enumerating this attribute.
302 private class BasicAttributeEnumeration
implements NamingEnumeration
306 public BasicAttributeEnumeration ()
310 public void close () throws NamingException
314 public boolean hasMore () throws NamingException
316 return hasMoreElements ();
319 public Object
next () throws NamingException
321 return nextElement ();
324 public boolean hasMoreElements ()
326 return where
< values
.size ();
329 public Object
nextElement () throws NoSuchElementException
331 if (where
+ 1 >= values
.size ())
332 throw new NoSuchElementException ("no more elements");
334 return values
.get (where
);