source: trunk/gcc/libjava/javax/naming/directory/BasicAttributes.java@ 2446

Last change on this file since 2446 was 1392, checked in by bird, 22 years ago

This commit was generated by cvs2svn to compensate for changes in r1391,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 5.5 KB
Line 
1/* BasicAttributes.java --
2 Copyright (C) 2000, 2001 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package javax.naming.directory;
40
41import javax.naming.*;
42import java.util.*;
43
44/**
45 * @author Tom Tromey <[email protected]>
46 * @date June 22, 2001
47 */
48public class BasicAttributes implements Attributes
49{
50 public BasicAttributes ()
51 {
52 this (false);
53 }
54
55 public BasicAttributes (boolean ignoreCase)
56 {
57 this.ignoreCase = ignoreCase;
58 this.attributes = new Vector ();
59 }
60
61 public BasicAttributes (String attrID, Object val)
62 {
63 this (attrID, val, false);
64 }
65
66 public BasicAttributes (String attrID, Object val, boolean ignoreCase)
67 {
68 this.ignoreCase = ignoreCase;
69 attributes = new Vector ();
70 attributes.add (new BasicAttribute (attrID, val));
71 }
72
73 public Object clone ()
74 {
75 // Slightly inefficient as we make a garbage Vector here.
76 BasicAttributes ba = new BasicAttributes (ignoreCase);
77 ba.attributes = (Vector) attributes.clone ();
78 return ba;
79 }
80
81 public boolean equals (Object obj)
82 {
83 if (! (obj instanceof BasicAttributes))
84 return false;
85 BasicAttributes b = (BasicAttributes) obj;
86 if (ignoreCase != b.ignoreCase
87 || attributes.size () != b.attributes.size ())
88 return false;
89
90 // Does order matter?
91 for (int i = 0; i < attributes.size (); ++i)
92 {
93 if (! attributes.get (i).equals (b.attributes.get (i)))
94 return false;
95 }
96
97 return true;
98 }
99
100 public Attribute get (String attrID)
101 {
102 for (int i = 0; i < attributes.size (); ++i)
103 {
104 Attribute at = (Attribute) attributes.get (i);
105 if ((ignoreCase && attrID.equalsIgnoreCase (at.getID ()))
106 || (! ignoreCase && attrID.equals (at.getID ())))
107 return at;
108 }
109
110 return null;
111 }
112
113 public NamingEnumeration getAll ()
114 {
115 return new BasicAttributesEnumeration (false);
116 }
117
118 public NamingEnumeration getIDs ()
119 {
120 return new BasicAttributesEnumeration (true);
121 }
122
123 public int hashCode ()
124 {
125 int val = 0;
126 for (int i = 0; i < attributes.size (); ++i)
127 val += attributes.get (i).hashCode ();
128 return val;
129 }
130
131 public boolean isCaseIgnored ()
132 {
133 return ignoreCase;
134 }
135
136 public Attribute put (Attribute attr)
137 {
138 Attribute r = remove (attr.getID ());
139 attributes.add (attr);
140 return r;
141 }
142
143 public Attribute put (String attrID, Object val)
144 {
145 return put (new BasicAttribute (attrID, val));
146 }
147
148 public Attribute remove (String attrID)
149 {
150 for (int i = 0; i < attributes.size (); ++i)
151 {
152 Attribute at = (Attribute) attributes.get (i);
153 if ((ignoreCase && attrID.equalsIgnoreCase (at.getID ()))
154 || (! ignoreCase && attrID.equals (at.getID ())))
155 {
156 attributes.remove (i);
157 return at;
158 }
159 }
160
161 return null;
162 }
163
164 public int size ()
165 {
166 return attributes.size ();
167 }
168
169 public String toString ()
170 {
171 String r = "";
172 for (int i = 0; i < attributes.size (); ++i)
173 {
174 if (i > 0)
175 r += "; ";
176 r += attributes.get (i).toString ();
177 }
178 return r;
179 }
180
181 // This is set by the serialization spec.
182 private boolean ignoreCase;
183 private transient Vector attributes;
184
185 // Used when enumerating.
186 private class BasicAttributesEnumeration implements NamingEnumeration
187 {
188 int where = -1;
189 boolean id;
190
191 public BasicAttributesEnumeration (boolean id)
192 {
193 this.id = id;
194 }
195
196 public void close () throws NamingException
197 {
198 }
199
200 public boolean hasMore () throws NamingException
201 {
202 return hasMoreElements ();
203 }
204
205 public Object next () throws NamingException
206 {
207 return nextElement ();
208 }
209
210 public boolean hasMoreElements ()
211 {
212 return where < attributes.size ();
213 }
214
215 public Object nextElement () throws NoSuchElementException
216 {
217 if (where + 1 >= attributes.size ())
218 throw new NoSuchElementException ("no more elements");
219 ++where;
220 Attribute at = (Attribute) attributes.get (where);
221 return id ? (Object) at.getID () : (Object) at;
222 }
223 }
224}
Note: See TracBrowser for help on using the repository browser.