BasicAttribute.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /* BasicAttribute.java --
  2. Copyright (C) 2000, 2001, 2004, 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.naming.directory;
  32. import java.io.IOException;
  33. import java.io.ObjectInputStream;
  34. import java.io.ObjectOutputStream;
  35. import java.util.NoSuchElementException;
  36. import java.util.Vector;
  37. import javax.naming.NamingEnumeration;
  38. import javax.naming.NamingException;
  39. import javax.naming.OperationNotSupportedException;
  40. /**
  41. * @author Tom Tromey (tromey@redhat.com)
  42. * @date June 20, 2001
  43. * @since 1.3
  44. */
  45. public class BasicAttribute implements Attribute
  46. {
  47. private static final long serialVersionUID = 6743528196119291326L;
  48. /** The ID of this attribute. */
  49. protected String attrID;
  50. /** True if this attribute's values are ordered. */
  51. protected boolean ordered;
  52. /** Values for this attribute. */
  53. protected transient Vector<Object> values;
  54. // Used by cloning.
  55. private BasicAttribute ()
  56. {
  57. }
  58. public BasicAttribute (String id)
  59. {
  60. this (id, false);
  61. }
  62. public BasicAttribute (String id, boolean ordered)
  63. {
  64. attrID = id;
  65. this.ordered = ordered;
  66. values = new Vector<Object> ();
  67. }
  68. public BasicAttribute (String id, Object value)
  69. {
  70. this (id, value, false);
  71. }
  72. public BasicAttribute (String id, Object value, boolean ordered)
  73. {
  74. attrID = id;
  75. this.ordered = ordered;
  76. values = new Vector<Object> ();
  77. values.add (value);
  78. }
  79. public void add (int index, Object val)
  80. {
  81. if (! ordered && contains (val))
  82. throw new IllegalStateException ("value already in attribute");
  83. values.add (index, val);
  84. }
  85. public boolean add (Object val)
  86. {
  87. if (! ordered && contains (val))
  88. throw new IllegalStateException ("value already in attribute");
  89. return values.add (val);
  90. }
  91. public void clear ()
  92. {
  93. values.clear ();
  94. }
  95. public Object clone ()
  96. {
  97. BasicAttribute c = new BasicAttribute ();
  98. c.attrID = attrID;
  99. c.ordered = ordered;
  100. c.values = (Vector<Object>) values.clone ();
  101. return c;
  102. }
  103. public boolean contains (Object val)
  104. {
  105. for (int i = 0; i < values.size (); ++i)
  106. {
  107. if (equals (val, values.get (i)))
  108. return true;
  109. }
  110. return false;
  111. }
  112. public boolean equals (Object obj)
  113. {
  114. if (! (obj instanceof BasicAttribute))
  115. return false;
  116. BasicAttribute b = (BasicAttribute) obj;
  117. if (ordered != b.ordered
  118. || ! attrID.equals (b.attrID)
  119. || values.size () != b.values.size ())
  120. return false;
  121. for (int i = 0; i < values.size (); ++i)
  122. {
  123. boolean ok = false;
  124. if (ordered)
  125. ok = equals (values.get (i), b.values.get (i));
  126. else
  127. {
  128. for (int j = 0; j < b.values.size (); ++j)
  129. {
  130. if (equals (values.get (i), b.values.get (j)))
  131. {
  132. ok = true;
  133. break;
  134. }
  135. }
  136. }
  137. if (! ok)
  138. return false;
  139. }
  140. return true;
  141. }
  142. public Object get ()
  143. throws NamingException
  144. {
  145. if (values.size () == 0)
  146. throw new NoSuchElementException ("no values");
  147. return get (0);
  148. }
  149. public Object get (int index)
  150. throws NamingException
  151. {
  152. return values.get (index);
  153. }
  154. public NamingEnumeration<?> getAll ()
  155. throws NamingException
  156. {
  157. return new BasicAttributeEnumeration ();
  158. }
  159. public DirContext getAttributeDefinition ()
  160. throws OperationNotSupportedException, NamingException
  161. {
  162. throw new OperationNotSupportedException ();
  163. }
  164. public DirContext getAttributeSyntaxDefinition ()
  165. throws OperationNotSupportedException, NamingException
  166. {
  167. throw new OperationNotSupportedException ();
  168. }
  169. public String getID ()
  170. {
  171. return attrID;
  172. }
  173. public int hashCode ()
  174. {
  175. int val = attrID.hashCode ();
  176. for (int i = 0; i < values.size (); ++i)
  177. {
  178. Object o = values.get (i);
  179. if (o == null)
  180. {
  181. // Nothing.
  182. }
  183. else if (o instanceof Object[])
  184. {
  185. Object[] a = (Object[]) o;
  186. for (int j = 0; j < a.length; ++j)
  187. val += a[j].hashCode ();
  188. }
  189. else
  190. val += o.hashCode ();
  191. }
  192. return val;
  193. }
  194. public boolean isOrdered ()
  195. {
  196. return ordered;
  197. }
  198. public Object remove (int index)
  199. {
  200. return values.remove (index);
  201. }
  202. public boolean remove (Object val)
  203. {
  204. for (int i = 0; i < values.size (); ++i)
  205. {
  206. if (equals (val, values.get (i)))
  207. {
  208. values.remove (i);
  209. return true;
  210. }
  211. }
  212. return false;
  213. }
  214. public Object set (int index, Object val)
  215. {
  216. if (! ordered && contains (val))
  217. throw new IllegalStateException ("value already in attribute");
  218. return values.set (index, val);
  219. }
  220. public int size ()
  221. {
  222. return values.size ();
  223. }
  224. public String toString ()
  225. {
  226. String r = attrID;
  227. for (int i = 0; i < values.size (); ++i)
  228. r += ";" + values.get (i).toString ();
  229. return r;
  230. }
  231. // This is used for testing equality of two Objects according to our
  232. // local rules.
  233. private boolean equals (Object one, Object two)
  234. {
  235. if (one == null)
  236. return two == null;
  237. if (one instanceof Object[])
  238. {
  239. if (! (two instanceof Object[]))
  240. return false;
  241. Object[] aone = (Object[]) one;
  242. Object[] atwo = (Object[]) two;
  243. if (aone.length != atwo.length)
  244. return false;
  245. for (int i = 0; i < aone.length; ++i)
  246. {
  247. if (! aone[i].equals (atwo[i]))
  248. return false;
  249. }
  250. return true;
  251. }
  252. return one.equals (two);
  253. }
  254. private void readObject(ObjectInputStream s)
  255. throws IOException, ClassNotFoundException
  256. {
  257. s.defaultReadObject();
  258. int size = s.readInt();
  259. values = new Vector<Object>(size);
  260. for (int i=0; i < size; i++)
  261. values.add(s.readObject());
  262. }
  263. private void writeObject(ObjectOutputStream s) throws IOException
  264. {
  265. s.defaultWriteObject();
  266. s.writeInt(values.size());
  267. for (int i=0; i < values.size(); i++)
  268. s.writeObject(values.get(i));
  269. }
  270. // Used when enumerating this attribute.
  271. private class BasicAttributeEnumeration implements NamingEnumeration
  272. {
  273. int where = 0;
  274. public BasicAttributeEnumeration ()
  275. {
  276. }
  277. public void close () throws NamingException
  278. {
  279. }
  280. public boolean hasMore () throws NamingException
  281. {
  282. return hasMoreElements ();
  283. }
  284. public Object next () throws NamingException
  285. {
  286. return nextElement ();
  287. }
  288. public boolean hasMoreElements ()
  289. {
  290. return where < values.size ();
  291. }
  292. public Object nextElement () throws NoSuchElementException
  293. {
  294. if (where == values.size ())
  295. throw new NoSuchElementException ("no more elements");
  296. return values.get (where++);
  297. }
  298. }
  299. }