1 /* BasicAttributes.java --
2 Copyright (C) 2000, 2001 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
;
43 import javax
.naming
.NamingEnumeration
;
44 import javax
.naming
.NamingException
;
47 * @author Tom Tromey <tromey@redhat.com>
50 public class BasicAttributes
implements Attributes
52 private static final long serialVersionUID
= 4980164073184639448L;
54 public BasicAttributes ()
59 public BasicAttributes (boolean ignoreCase
)
61 this.ignoreCase
= ignoreCase
;
62 this.attributes
= new Vector ();
65 public BasicAttributes (String attrID
, Object val
)
67 this (attrID
, val
, false);
70 public BasicAttributes (String attrID
, Object val
, boolean ignoreCase
)
72 this.ignoreCase
= ignoreCase
;
73 attributes
= new Vector ();
74 attributes
.add (new BasicAttribute (attrID
, val
));
77 public Object
clone ()
79 // Slightly inefficient as we make a garbage Vector here.
80 BasicAttributes ba
= new BasicAttributes (ignoreCase
);
81 ba
.attributes
= (Vector
) attributes
.clone ();
85 public boolean equals (Object obj
)
87 if (! (obj
instanceof BasicAttributes
))
89 BasicAttributes b
= (BasicAttributes
) obj
;
90 if (ignoreCase
!= b
.ignoreCase
91 || attributes
.size () != b
.attributes
.size ())
95 for (int i
= 0; i
< attributes
.size (); ++i
)
97 if (! attributes
.get (i
).equals (b
.attributes
.get (i
)))
104 public Attribute
get (String attrID
)
106 for (int i
= 0; i
< attributes
.size (); ++i
)
108 Attribute at
= (Attribute
) attributes
.get (i
);
109 if ((ignoreCase
&& attrID
.equalsIgnoreCase (at
.getID ()))
110 || (! ignoreCase
&& attrID
.equals (at
.getID ())))
117 public NamingEnumeration
getAll ()
119 return new BasicAttributesEnumeration (false);
122 public NamingEnumeration
getIDs ()
124 return new BasicAttributesEnumeration (true);
127 public int hashCode ()
130 for (int i
= 0; i
< attributes
.size (); ++i
)
131 val
+= attributes
.get (i
).hashCode ();
135 public boolean isCaseIgnored ()
140 public Attribute
put (Attribute attr
)
142 Attribute r
= remove (attr
.getID ());
143 attributes
.add (attr
);
147 public Attribute
put (String attrID
, Object val
)
149 return put (new BasicAttribute (attrID
, val
));
152 public Attribute
remove (String attrID
)
154 for (int i
= 0; i
< attributes
.size (); ++i
)
156 Attribute at
= (Attribute
) attributes
.get (i
);
157 if ((ignoreCase
&& attrID
.equalsIgnoreCase (at
.getID ()))
158 || (! ignoreCase
&& attrID
.equals (at
.getID ())))
160 attributes
.remove (i
);
170 return attributes
.size ();
173 public String
toString ()
176 for (int i
= 0; i
< attributes
.size (); ++i
)
180 r
+= attributes
.get (i
).toString ();
185 // This is set by the serialization spec.
186 private boolean ignoreCase
;
187 private transient Vector attributes
;
189 // Used when enumerating.
190 private class BasicAttributesEnumeration
implements NamingEnumeration
195 public BasicAttributesEnumeration (boolean id
)
200 public void close () throws NamingException
204 public boolean hasMore () throws NamingException
206 return hasMoreElements ();
209 public Object
next () throws NamingException
211 return nextElement ();
214 public boolean hasMoreElements ()
216 return where
< attributes
.size ();
219 public Object
nextElement () throws NoSuchElementException
221 if (where
+ 1 >= attributes
.size ())
222 throw new NoSuchElementException ("no more elements");
224 Attribute at
= (Attribute
) attributes
.get (where
);
225 return id ?
(Object
) at
.getID () : (Object
) at
;