001/* AccessibleRelationSet.java -- the combined relations of an accessible object
002   Copyright (C) 2002, 2005 Free Software Foundation
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038package javax.accessibility;
039
040import gnu.java.lang.CPStringBuilder;
041
042import java.util.Locale;
043import java.util.Vector;
044
045/**
046 * Describes all relations of an accessible object. For example, an object
047 * by labeled by one object and control another.
048 *
049 * @author Eric Blake (ebb9@email.byu.edu)
050 * @see AccessibleRelation
051 * @since 1.2
052 * @status updated to 1.4
053 */
054public class AccessibleRelationSet
055{
056  /**
057   * The list of relations, should be instances of AccessibleRelation. Don't
058   * set this to null.
059   *
060   * @see #add(AccessibleRelation)
061   * @see #addAll(AccessibleRelation[])
062   * @see #remove(AccessibleRelation)
063   * @see #contains(String)
064   * @see #get(String)
065   * @see #size()
066   * @see #toArray()
067   * @see #clear()
068   */
069  protected Vector<AccessibleRelation> relations
070    = new Vector<AccessibleRelation>();
071
072  /**
073   * Create an empty relation set.
074   */
075  public AccessibleRelationSet()
076  {
077  }
078
079  /**
080   * Create a relation set initialized with the given relations, duplicates are
081   * ignored.
082   *
083   * @param relations the relations to insert
084   * @throws NullPointerException if relations is null
085   */
086  public AccessibleRelationSet(AccessibleRelation[] relations)
087  {
088    addAll(relations);
089  }
090
091  /**
092   * Add a new relation to the current set. If the relation is already in
093   * the set, the targets are merged with the existing relation, possibly
094   * resulting in an object being in the target list more than once. Do not
095   * add a relation with a null key, as it will cause problems later.
096   *
097   * @param relation the relation to add
098   * @return true if the set was modified, which is always the case
099   * @throws NullPointerException if relation is null
100   */
101  public boolean add(AccessibleRelation relation)
102  {
103    AccessibleRelation old = get(relation.key);
104    if (old == null)
105      return relations.add(relation);
106    if (old.targets.length == 0)
107      old.targets = relation.targets;
108    else if (relation.targets.length != 0)
109      {
110        Object[] t = new Object[old.targets.length + relation.targets.length];
111        System.arraycopy(old.targets, 0, t, 0, old.targets.length);
112        System.arraycopy(relation.targets, 0, t, old.targets.length,
113                         relation.targets.length);
114        old.targets = t;
115      }
116    return true;
117  }
118
119  /**
120   * Add all of the relations to the current set. Duplicates are ignored.
121   *
122   * @param array the array of relations to add
123   * @throws NullPointerException if array is null or has null entries
124   */
125  public void addAll(AccessibleRelation[] array)
126  {
127    int i = array.length;
128    while (--i >= 0)
129      add(array[i]);
130  }
131
132  /**
133   * Remove a relation from the set. If a relation was removed, return true.
134   * Note that this uses AccessibleRelation.equals, which defaults to ==, so a
135   * relation with the same key may still exist in the set afterwords.
136   *
137   * @param relation the state to remove
138   * @return true if the set changed
139   */
140  public boolean remove(AccessibleRelation relation)
141  {
142    return relations.remove(relation);
143  }
144
145  /**
146   * Clear all relations in the set.
147   */
148  public void clear()
149  {
150    relations.clear();
151  }
152
153  /**
154   * Return the number of relations in the set.
155   *
156   * @return the set size
157   */
158  public int size()
159  {
160    return relations.size();
161  }
162
163  /**
164   * Check if the relation key is in the set.
165   *
166   * @param key the relation to locate
167   * @return true if it is in the set
168   */
169  public boolean contains(String key)
170  {
171    int i = relations.size();
172    while (--i >= 0)
173      if ((relations.get(i)).key.equals(key))
174        return true;
175    return false;
176  }
177
178  /**
179   * Get the relation that matches the key.
180   *
181   * @param key the relation to locate
182   * @return the relation in the set, or null
183   */
184  public AccessibleRelation get(String key)
185  {
186    int i = relations.size();
187    while (--i >= 0)
188      {
189        AccessibleRelation r = relations.get(i);
190        if (r.key.equals(key))
191          return r;
192      }
193    return null;
194  }
195
196  /**
197   * Return the relation set as an array.
198   *
199   * @return an array of the current relations
200   */
201  public AccessibleRelation[] toArray()
202  {
203    AccessibleRelation[] result = new AccessibleRelation[relations.size()];
204    relations.toArray(result);
205    return result;
206  }
207
208  /**
209   * Return a localized, comma-separated string representing all relations
210   * in the set. This is in arbitrary order.
211   *
212   * @return the string representation
213   * @see AccessibleBundle#toDisplayString(String, Locale)
214   */
215  public String toString()
216  {
217    int i = relations.size();
218    if (i == 0)
219      return "";
220    // Pre-allocate an average of 10 chars per state.
221    CPStringBuilder b = new CPStringBuilder(i * 10);
222    while (--i >= 0)
223      b.append(relations.get(i)).append(',');
224    return b.substring(0, b.length() - 1);
225  }
226} // class AccessibleRelationSet