Merge from the pain train
[official-gcc.git] / libjava / javax / accessibility / AccessibleRelationSet.java
blob767aa0647bb2e075f92ba28130b46bb27d42a3df
1 /* AccessibleRelationSet.java -- the combined relations of an accessible object
2 Copyright (C) 2002 Free Software Foundation
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. */
38 package javax.accessibility;
40 import java.util.Vector;
42 /**
43 * Describes all relations of an accessible object. For example, an object
44 * by labeled by one object and control another.
46 * @author Eric Blake (ebb9@email.byu.edu)
47 * @see AccessibleRelation
48 * @since 1.2
49 * @status updated to 1.4
51 public class AccessibleRelationSet
53 /**
54 * The list of relations, should be instances of AccessibleRelation. Don't
55 * set this to null.
57 * @see #add(AccessibleRelation)
58 * @see #addAll(AccessibleRelation[])
59 * @see #remove(AccessibleRelation)
60 * @see #contains(String)
61 * @see #get(String)
62 * @see #size()
63 * @see #toArray()
64 * @see #clear()
66 protected Vector relations = new Vector();
68 /**
69 * Create an empty relation set.
71 public AccessibleRelationSet()
75 /**
76 * Create a relation set initialized with the given relations, duplicates are
77 * ignored.
79 * @param relations the relations to insert
80 * @throws NullPointerException if relations is null
82 public AccessibleRelationSet(AccessibleRelation[] relations)
84 addAll(relations);
87 /**
88 * Add a new relation to the current set. If the relation is already in
89 * the set, the targets are merged with the existing relation, possibly
90 * resulting in an object being in the target list more than once. Do not
91 * add a relation with a null key, as it will cause problems later.
93 * @param relation the relation to add
94 * @return true if the set was modified, which is always the case
95 * @throws NullPointerException if relation is null
97 public boolean add(AccessibleRelation relation)
99 AccessibleRelation old = get(relation.key);
100 if (old == null)
101 return relations.add(relation);
102 if (old.targets.length == 0)
103 old.targets = relation.targets;
104 else if (relation.targets.length != 0)
106 Object[] t = new Object[old.targets.length + relation.targets.length];
107 System.arraycopy(old.targets, 0, t, 0, old.targets.length);
108 System.arraycopy(relation.targets, 0, t, old.targets.length,
109 relation.targets.length);
110 old.targets = t;
112 return true;
116 * Add all of the relations to the current set. Duplicates are ignored.
118 * @param array the array of relations to add
119 * @throws NullPointerException if array is null or has null entries
121 public void addAll(AccessibleRelation[] array)
123 int i = array.length;
124 while (--i >= 0)
125 add(array[i]);
129 * Remove a relation from the set. If a relation was removed, return true.
130 * Note that this uses AccessibleRelation.equals, which defaults to ==, so a
131 * relation with the same key may still exist in the set afterwords.
133 * @param relation the state to remove
134 * @return true if the set changed
136 public boolean remove(AccessibleRelation relation)
138 return relations.remove(relation);
142 * Clear all relations in the set.
144 public void clear()
146 relations.clear();
150 * Return the number of relations in the set.
152 * @return the set size
154 public int size()
156 return relations.size();
160 * Check if the relation key is in the set.
162 * @param relation the relation to locate
163 * @return true if it is in the set
165 public boolean contains(String key)
167 int i = relations.size();
168 while (--i >= 0)
169 if (((AccessibleRelation) relations.get(i)).key.equals(key))
170 return true;
171 return false;
175 * Get the relation that matches the key.
177 * @param relation the relation to locate
178 * @return the relation in the set, or null
180 public AccessibleRelation get(String key)
182 int i = relations.size();
183 while (--i >= 0)
185 AccessibleRelation r = (AccessibleRelation) relations.get(i);
186 if (r.key.equals(key))
187 return r;
189 return null;
193 * Return the relation set as an array.
195 * @return an array of the current relations
197 public AccessibleRelation[] toArray()
199 AccessibleRelation[] result = new AccessibleRelation[relations.size()];
200 relations.toArray(result);
201 return result;
205 * Return a localized, comma-separated string representing all relations
206 * in the set. This is in arbitrary order.
208 * @return the string representation
209 * @see AccessibleBundle#toDisplayString(String, Locale)
211 public String toString()
213 int i = relations.size();
214 if (i == 0)
215 return "";
216 // Pre-allocate an average of 10 chars per state.
217 StringBuffer b = new StringBuffer(i * 10);
218 while (--i >= 0)
219 b.append(relations.get(i)).append(',');
220 return b.substring(0, b.length() - 1);
222 } // class AccessibleRelationSet