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 javax.naming.*;
45 * @author Tom Tromey <tromey@redhat.com>
48 public class BasicAttributes implements Attributes
50 public BasicAttributes ()
55 public BasicAttributes (boolean ignoreCase)
57 this.ignoreCase = ignoreCase;
58 this.attributes = new Vector ();
61 public BasicAttributes (String attrID, Object val)
63 this (attrID, val, false);
66 public BasicAttributes (String attrID, Object val, boolean ignoreCase)
68 this.ignoreCase = ignoreCase;
69 attributes = new Vector ();
70 attributes.add (new BasicAttribute (attrID, val));
73 public Object clone ()
75 // Slightly inefficient as we make a garbage Vector here.
76 BasicAttributes ba = new BasicAttributes (ignoreCase);
77 ba.attributes = (Vector) attributes.clone ();
81 public boolean equals (Object obj)
83 if (! (obj instanceof BasicAttributes))
85 BasicAttributes b = (BasicAttributes) obj;
86 if (ignoreCase != b.ignoreCase
87 || attributes.size () != b.attributes.size ())
91 for (int i = 0; i < attributes.size (); ++i)
93 if (! attributes.get (i).equals (b.attributes.get (i)))
100 public Attribute get (String attrID)
102 for (int i = 0; i < attributes.size (); ++i)
104 Attribute at = (Attribute) attributes.get (i);
105 if ((ignoreCase && attrID.equalsIgnoreCase (at.getID ()))
106 || (! ignoreCase && attrID.equals (at.getID ())))
113 public NamingEnumeration getAll ()
115 return new BasicAttributesEnumeration (false);
118 public NamingEnumeration getIDs ()
120 return new BasicAttributesEnumeration (true);
123 public int hashCode ()
126 for (int i = 0; i < attributes.size (); ++i)
127 val += attributes.get (i).hashCode ();
131 public boolean isCaseIgnored ()
136 public Attribute put (Attribute attr)
138 Attribute r = remove (attr.getID ());
139 attributes.add (attr);
143 public Attribute put (String attrID, Object val)
145 return put (new BasicAttribute (attrID, val));
148 public Attribute remove (String attrID)
150 for (int i = 0; i < attributes.size (); ++i)
152 Attribute at = (Attribute) attributes.get (i);
153 if ((ignoreCase && attrID.equalsIgnoreCase (at.getID ()))
154 || (! ignoreCase && attrID.equals (at.getID ())))
156 attributes.remove (i);
166 return attributes.size ();
169 public String toString ()
172 for (int i = 0; i < attributes.size (); ++i)
176 r += attributes.get (i).toString ();
181 // This is set by the serialization spec.
182 private boolean ignoreCase;
183 private transient Vector attributes;
185 // Used when enumerating.
186 private class BasicAttributesEnumeration implements NamingEnumeration
191 public BasicAttributesEnumeration (boolean id)
196 public void close () throws NamingException
200 public boolean hasMore () throws NamingException
202 return hasMoreElements ();
205 public Object next () throws NamingException
207 return nextElement ();
210 public boolean hasMoreElements ()
212 return where < attributes.size ();
215 public Object nextElement () throws NoSuchElementException
217 if (where + 1 >= attributes.size ())
218 throw new NoSuchElementException ("no more elements");
220 Attribute at = (Attribute) attributes.get (where);
221 return id ? (Object) at.getID () : (Object) at;