1 /* BasicAttribute.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 BasicAttribute implements Attribute
50 /** The ID of this attribute. */
51 protected String attrID;
52 /** True if this attribute's values are ordered. */
53 protected boolean ordered;
54 /** Values for this attribute. */
55 protected transient Vector values;
58 private BasicAttribute ()
62 public BasicAttribute (String id)
67 public BasicAttribute (String id, boolean ordered)
70 this.ordered = ordered;
71 values = new Vector ();
74 public BasicAttribute (String id, Object value)
76 this (id, value, false);
79 public BasicAttribute (String id, Object value, boolean ordered)
82 this.ordered = ordered;
83 values = new Vector ();
87 public void add (int index, Object val)
89 if (! ordered && contains (val))
90 throw new IllegalStateException ("value already in attribute");
91 values.add (index, val);
94 public boolean add (Object val)
96 if (! ordered && contains (val))
97 throw new IllegalStateException ("value already in attribute");
98 return values.add (val);
106 public Object clone ()
108 BasicAttribute c = new BasicAttribute ();
111 c.values = (Vector) values.clone ();
115 public boolean contains (Object val)
117 for (int i = 0; i < values.size (); ++i)
119 if (equals (val, values.get (i)))
126 public boolean equals (Object obj)
128 if (! (obj instanceof BasicAttribute))
130 BasicAttribute b = (BasicAttribute) obj;
132 if (ordered != b.ordered
133 || ! attrID.equals (b.attrID)
134 || values.size () != b.values.size ())
137 for (int i = 0; i < values.size (); ++i)
141 ok = equals (values.get (i), b.values.get (i));
144 for (int j = 0; j < b.values.size (); ++j)
146 if (equals (values.get (i), b.values.get (j)))
163 if (values.size () == 0)
164 throw new NoSuchElementException ("no values");
168 public Object get (int index)
170 return values.get (index);
173 public NamingEnumeration getAll ()
175 return new BasicAttributeEnumeration ();
178 public DirContext getAttributeDefinition ()
179 throws OperationNotSupportedException, NamingException
181 throw new OperationNotSupportedException ();
184 public DirContext getAttributeSyntaxDefinition ()
185 throws OperationNotSupportedException, NamingException
187 throw new OperationNotSupportedException ();
190 public String getID ()
195 public int hashCode ()
197 int val = attrID.hashCode ();
198 for (int i = 0; i < values.size (); ++i)
200 Object o = values.get (i);
205 else if (o instanceof Object[])
207 Object[] a = (Object[]) o;
208 for (int j = 0; j < a.length; ++j)
209 val += a[j].hashCode ();
212 val += o.hashCode ();
218 public boolean isOrdered ()
223 public Object remove (int index)
225 return values.remove (index);
228 public boolean remove (Object val)
230 for (int i = 0; i < values.size (); ++i)
232 if (equals (val, values.get (i)))
242 public Object set (int index, Object val)
244 if (! ordered && contains (val))
245 throw new IllegalStateException ("value already in attribute");
246 return values.set (index, val);
251 return values.size ();
254 public String toString ()
257 for (int i = 0; i < values.size (); ++i)
258 r += ";" + values.get (i).toString ();
262 // This is used for testing equality of two Objects according to our
264 private boolean equals (Object one, Object two)
269 if (one instanceof Object[])
271 if (! (two instanceof Object[]))
274 Object[] aone = (Object[]) one;
275 Object[] atwo = (Object[]) two;
277 if (aone.length != atwo.length)
280 for (int i = 0; i < aone.length; ++i)
282 if (! aone[i].equals (atwo[i]))
289 return one.equals (two);
292 // Used when enumerating this attribute.
293 private class BasicAttributeEnumeration implements NamingEnumeration
297 public BasicAttributeEnumeration ()
301 public void close () throws NamingException
305 public boolean hasMore () throws NamingException
307 return hasMoreElements ();
310 public Object next () throws NamingException
312 return nextElement ();
315 public boolean hasMoreElements ()
317 return where < values.size ();
320 public Object nextElement () throws NoSuchElementException
322 if (where + 1 >= values.size ())
323 throw new NoSuchElementException ("no more elements");
325 return values.get (where);