AccessibleRelationSet.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* AccessibleRelationSet.java -- the combined relations of an accessible object
  2. Copyright (C) 2002, 2005 Free Software Foundation
  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.accessibility;
  32. import gnu.java.lang.CPStringBuilder;
  33. import java.util.Locale;
  34. import java.util.Vector;
  35. /**
  36. * Describes all relations of an accessible object. For example, an object
  37. * by labeled by one object and control another.
  38. *
  39. * @author Eric Blake (ebb9@email.byu.edu)
  40. * @see AccessibleRelation
  41. * @since 1.2
  42. * @status updated to 1.4
  43. */
  44. public class AccessibleRelationSet
  45. {
  46. /**
  47. * The list of relations, should be instances of AccessibleRelation. Don't
  48. * set this to null.
  49. *
  50. * @see #add(AccessibleRelation)
  51. * @see #addAll(AccessibleRelation[])
  52. * @see #remove(AccessibleRelation)
  53. * @see #contains(String)
  54. * @see #get(String)
  55. * @see #size()
  56. * @see #toArray()
  57. * @see #clear()
  58. */
  59. protected Vector<AccessibleRelation> relations
  60. = new Vector<AccessibleRelation>();
  61. /**
  62. * Create an empty relation set.
  63. */
  64. public AccessibleRelationSet()
  65. {
  66. }
  67. /**
  68. * Create a relation set initialized with the given relations, duplicates are
  69. * ignored.
  70. *
  71. * @param relations the relations to insert
  72. * @throws NullPointerException if relations is null
  73. */
  74. public AccessibleRelationSet(AccessibleRelation[] relations)
  75. {
  76. addAll(relations);
  77. }
  78. /**
  79. * Add a new relation to the current set. If the relation is already in
  80. * the set, the targets are merged with the existing relation, possibly
  81. * resulting in an object being in the target list more than once. Do not
  82. * add a relation with a null key, as it will cause problems later.
  83. *
  84. * @param relation the relation to add
  85. * @return true if the set was modified, which is always the case
  86. * @throws NullPointerException if relation is null
  87. */
  88. public boolean add(AccessibleRelation relation)
  89. {
  90. AccessibleRelation old = get(relation.key);
  91. if (old == null)
  92. return relations.add(relation);
  93. if (old.targets.length == 0)
  94. old.targets = relation.targets;
  95. else if (relation.targets.length != 0)
  96. {
  97. Object[] t = new Object[old.targets.length + relation.targets.length];
  98. System.arraycopy(old.targets, 0, t, 0, old.targets.length);
  99. System.arraycopy(relation.targets, 0, t, old.targets.length,
  100. relation.targets.length);
  101. old.targets = t;
  102. }
  103. return true;
  104. }
  105. /**
  106. * Add all of the relations to the current set. Duplicates are ignored.
  107. *
  108. * @param array the array of relations to add
  109. * @throws NullPointerException if array is null or has null entries
  110. */
  111. public void addAll(AccessibleRelation[] array)
  112. {
  113. int i = array.length;
  114. while (--i >= 0)
  115. add(array[i]);
  116. }
  117. /**
  118. * Remove a relation from the set. If a relation was removed, return true.
  119. * Note that this uses AccessibleRelation.equals, which defaults to ==, so a
  120. * relation with the same key may still exist in the set afterwords.
  121. *
  122. * @param relation the state to remove
  123. * @return true if the set changed
  124. */
  125. public boolean remove(AccessibleRelation relation)
  126. {
  127. return relations.remove(relation);
  128. }
  129. /**
  130. * Clear all relations in the set.
  131. */
  132. public void clear()
  133. {
  134. relations.clear();
  135. }
  136. /**
  137. * Return the number of relations in the set.
  138. *
  139. * @return the set size
  140. */
  141. public int size()
  142. {
  143. return relations.size();
  144. }
  145. /**
  146. * Check if the relation key is in the set.
  147. *
  148. * @param key the relation to locate
  149. * @return true if it is in the set
  150. */
  151. public boolean contains(String key)
  152. {
  153. int i = relations.size();
  154. while (--i >= 0)
  155. if ((relations.get(i)).key.equals(key))
  156. return true;
  157. return false;
  158. }
  159. /**
  160. * Get the relation that matches the key.
  161. *
  162. * @param key the relation to locate
  163. * @return the relation in the set, or null
  164. */
  165. public AccessibleRelation get(String key)
  166. {
  167. int i = relations.size();
  168. while (--i >= 0)
  169. {
  170. AccessibleRelation r = relations.get(i);
  171. if (r.key.equals(key))
  172. return r;
  173. }
  174. return null;
  175. }
  176. /**
  177. * Return the relation set as an array.
  178. *
  179. * @return an array of the current relations
  180. */
  181. public AccessibleRelation[] toArray()
  182. {
  183. AccessibleRelation[] result = new AccessibleRelation[relations.size()];
  184. relations.toArray(result);
  185. return result;
  186. }
  187. /**
  188. * Return a localized, comma-separated string representing all relations
  189. * in the set. This is in arbitrary order.
  190. *
  191. * @return the string representation
  192. * @see AccessibleBundle#toDisplayString(String, Locale)
  193. */
  194. public String toString()
  195. {
  196. int i = relations.size();
  197. if (i == 0)
  198. return "";
  199. // Pre-allocate an average of 10 chars per state.
  200. CPStringBuilder b = new CPStringBuilder(i * 10);
  201. while (--i >= 0)
  202. b.append(relations.get(i)).append(',');
  203. return b.substring(0, b.length() - 1);
  204. }
  205. } // class AccessibleRelationSet