Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / naming / directory / BasicAttribute.java
blob24c21e6b0479e79b81f95153ae404ea09ec01933
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)
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;
46 import javax.naming.OperationNotSupportedException;
48 /**
49 * @author Tom Tromey (tromey@redhat.com)
50 * @date June 20, 2001
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;
63 // Used by cloning.
64 private BasicAttribute ()
68 public BasicAttribute (String id)
70 this (id, false);
73 public BasicAttribute (String id, boolean ordered)
75 attrID = id;
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)
87 attrID = id;
88 this.ordered = ordered;
89 values = new Vector ();
90 values.add (value);
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);
107 public void clear ()
109 values.clear ();
112 public Object clone ()
114 BasicAttribute c = new BasicAttribute ();
115 c.attrID = attrID;
116 c.ordered = ordered;
117 c.values = (Vector) values.clone ();
118 return c;
121 public boolean contains (Object val)
123 for (int i = 0; i < values.size (); ++i)
125 if (equals (val, values.get (i)))
126 return true;
129 return false;
132 public boolean equals (Object obj)
134 if (! (obj instanceof BasicAttribute))
135 return false;
136 BasicAttribute b = (BasicAttribute) obj;
138 if (ordered != b.ordered
139 || ! attrID.equals (b.attrID)
140 || values.size () != b.values.size ())
141 return false;
143 for (int i = 0; i < values.size (); ++i)
145 boolean ok = false;
146 if (ordered)
147 ok = equals (values.get (i), b.values.get (i));
148 else
150 for (int j = 0; j < b.values.size (); ++j)
152 if (equals (values.get (i), b.values.get (j)))
154 ok = true;
155 break;
160 if (! ok)
161 return false;
164 return true;
167 public Object get ()
168 throws NamingException
170 if (values.size () == 0)
171 throw new NoSuchElementException ("no values");
172 return get (0);
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 ()
201 return attrID;
204 public int hashCode ()
206 int val = attrID.hashCode ();
207 for (int i = 0; i < values.size (); ++i)
209 Object o = values.get (i);
210 if (o == null)
212 // Nothing.
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 ();
220 else
221 val += o.hashCode ();
224 return val;
227 public boolean isOrdered ()
229 return ordered;
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)))
243 values.remove (i);
244 return true;
248 return false;
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);
258 public int size ()
260 return values.size ();
263 public String toString ()
265 String r = attrID;
266 for (int i = 0; i < values.size (); ++i)
267 r += ";" + values.get (i).toString ();
268 return r;
271 // This is used for testing equality of two Objects according to our
272 // local rules.
273 private boolean equals (Object one, Object two)
275 if (one == null)
276 return two == null;
278 if (one instanceof Object[])
280 if (! (two instanceof Object[]))
281 return false;
283 Object[] aone = (Object[]) one;
284 Object[] atwo = (Object[]) two;
286 if (aone.length != atwo.length)
287 return false;
289 for (int i = 0; i < aone.length; ++i)
291 if (! aone[i].equals (atwo[i]))
292 return false;
295 return true;
298 return one.equals (two);
301 // Used when enumerating this attribute.
302 private class BasicAttributeEnumeration implements NamingEnumeration
304 int where = -1;
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");
333 ++where;
334 return values.get (where);