Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / naming / directory / BasicAttributes.java
blob9a9a80019a452186285cbeb029126ca3dab04df6
1 /* BasicAttributes.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)
9 any later version.
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
19 02111-1307 USA.
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
24 combination.
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;
47 /**
48 * @author Tom Tromey (tromey@redhat.com)
49 * @date June 22, 2001
51 public class BasicAttributes implements Attributes
53 private static final long serialVersionUID = 4980164073184639448L;
55 public BasicAttributes ()
57 this (false);
60 public BasicAttributes (boolean ignoreCase)
62 this.ignoreCase = ignoreCase;
63 this.attributes = new Vector ();
66 public BasicAttributes (String attrID, Object val)
68 this (attrID, val, false);
71 public BasicAttributes (String attrID, Object val, boolean ignoreCase)
73 this.ignoreCase = ignoreCase;
74 attributes = new Vector ();
75 attributes.add (new BasicAttribute (attrID, val));
78 public Object clone ()
80 // Slightly inefficient as we make a garbage Vector here.
81 BasicAttributes ba = new BasicAttributes (ignoreCase);
82 ba.attributes = (Vector) attributes.clone ();
83 return ba;
86 public boolean equals (Object obj)
88 if (! (obj instanceof BasicAttributes))
89 return false;
90 BasicAttributes b = (BasicAttributes) obj;
91 if (ignoreCase != b.ignoreCase
92 || attributes.size () != b.attributes.size ())
93 return false;
95 // Does order matter?
96 for (int i = 0; i < attributes.size (); ++i)
98 if (! attributes.get (i).equals (b.attributes.get (i)))
99 return false;
102 return true;
105 public Attribute get (String attrID)
107 for (int i = 0; i < attributes.size (); ++i)
109 Attribute at = (Attribute) attributes.get (i);
110 if ((ignoreCase && attrID.equalsIgnoreCase (at.getID ()))
111 || (! ignoreCase && attrID.equals (at.getID ())))
112 return at;
115 return null;
118 public NamingEnumeration getAll ()
120 return new BasicAttributesEnumeration (false);
123 public NamingEnumeration getIDs ()
125 return new BasicAttributesEnumeration (true);
128 public int hashCode ()
130 int val = 0;
131 for (int i = 0; i < attributes.size (); ++i)
132 val += attributes.get (i).hashCode ();
133 return val;
136 public boolean isCaseIgnored ()
138 return ignoreCase;
141 public Attribute put (Attribute attr)
143 Attribute r = remove (attr.getID ());
144 attributes.add (attr);
145 return r;
148 public Attribute put (String attrID, Object val)
150 return put (new BasicAttribute (attrID, val));
153 public Attribute remove (String attrID)
155 for (int i = 0; i < attributes.size (); ++i)
157 Attribute at = (Attribute) attributes.get (i);
158 if ((ignoreCase && attrID.equalsIgnoreCase (at.getID ()))
159 || (! ignoreCase && attrID.equals (at.getID ())))
161 attributes.remove (i);
162 return at;
166 return null;
169 public int size ()
171 return attributes.size ();
174 public String toString ()
176 String r = "";
177 for (int i = 0; i < attributes.size (); ++i)
179 if (i > 0)
180 r += "; ";
181 r += attributes.get (i).toString ();
183 return r;
186 // This is set by the serialization spec.
187 private boolean ignoreCase;
188 // Package-private to avoid a trampoline.
189 transient Vector attributes;
191 // Used when enumerating.
192 private class BasicAttributesEnumeration implements NamingEnumeration
194 int where = -1;
195 boolean id;
197 public BasicAttributesEnumeration (boolean id)
199 this.id = id;
202 public void close () throws NamingException
206 public boolean hasMore () throws NamingException
208 return hasMoreElements ();
211 public Object next () throws NamingException
213 return nextElement ();
216 public boolean hasMoreElements ()
218 return where < attributes.size ();
221 public Object nextElement () throws NoSuchElementException
223 if (where + 1 >= attributes.size ())
224 throw new NoSuchElementException ("no more elements");
225 ++where;
226 Attribute at = (Attribute) attributes.get (where);
227 return id ? (Object) at.getID () : (Object) at;