Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/accessibility/AccessibleRelationSet.java
38829 views
/*1* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.accessibility;2627import java.util.Vector;28import java.util.Locale;29import java.util.MissingResourceException;30import java.util.ResourceBundle;3132/**33* Class AccessibleRelationSet determines a component's relation set. The34* relation set of a component is a set of AccessibleRelation objects that35* describe the component's relationships with other components.36*37* @see AccessibleRelation38*39* @author Lynn Monsanto40* @since 1.341*/42public class AccessibleRelationSet {4344/**45* Each entry in the Vector represents an AccessibleRelation.46* @see #add47* @see #addAll48* @see #remove49* @see #contains50* @see #get51* @see #size52* @see #toArray53* @see #clear54*/55protected Vector<AccessibleRelation> relations = null;5657/**58* Creates a new empty relation set.59*/60public AccessibleRelationSet() {61relations = null;62}6364/**65* Creates a new relation with the initial set of relations contained in66* the array of relations passed in. Duplicate entries are ignored.67*68* @param relations an array of AccessibleRelation describing the69* relation set.70*/71public AccessibleRelationSet(AccessibleRelation[] relations) {72if (relations.length != 0) {73this.relations = new Vector(relations.length);74for (int i = 0; i < relations.length; i++) {75add(relations[i]);76}77}78}7980/**81* Adds a new relation to the current relation set. If the relation82* is already in the relation set, the target(s) of the specified83* relation is merged with the target(s) of the existing relation.84* Otherwise, the new relation is added to the relation set.85*86* @param relation the relation to add to the relation set87* @return true if relation is added to the relation set; false if the88* relation set is unchanged89*/90public boolean add(AccessibleRelation relation) {91if (relations == null) {92relations = new Vector();93}9495// Merge the relation targets if the key exists96AccessibleRelation existingRelation = get(relation.getKey());97if (existingRelation == null) {98relations.addElement(relation);99return true;100} else {101Object [] existingTarget = existingRelation.getTarget();102Object [] newTarget = relation.getTarget();103int mergedLength = existingTarget.length + newTarget.length;104Object [] mergedTarget = new Object[mergedLength];105for (int i = 0; i < existingTarget.length; i++) {106mergedTarget[i] = existingTarget[i];107}108for (int i = existingTarget.length, j = 0;109i < mergedLength;110i++, j++) {111mergedTarget[i] = newTarget[j];112}113existingRelation.setTarget(mergedTarget);114}115return true;116}117118/**119* Adds all of the relations to the existing relation set. Duplicate120* entries are ignored.121*122* @param relations AccessibleRelation array describing the relation set.123*/124public void addAll(AccessibleRelation[] relations) {125if (relations.length != 0) {126if (this.relations == null) {127this.relations = new Vector(relations.length);128}129for (int i = 0; i < relations.length; i++) {130add(relations[i]);131}132}133}134135/**136* Removes a relation from the current relation set. If the relation137* is not in the set, the relation set will be unchanged and the138* return value will be false. If the relation is in the relation139* set, it will be removed from the set and the return value will be140* true.141*142* @param relation the relation to remove from the relation set143* @return true if the relation is in the relation set; false if the144* relation set is unchanged145*/146public boolean remove(AccessibleRelation relation) {147if (relations == null) {148return false;149} else {150return relations.removeElement(relation);151}152}153154/**155* Removes all the relations from the current relation set.156*/157public void clear() {158if (relations != null) {159relations.removeAllElements();160}161}162163/**164* Returns the number of relations in the relation set.165* @return the number of relations in the relation set166*/167public int size() {168if (relations == null) {169return 0;170} else {171return relations.size();172}173}174175/**176* Returns whether the relation set contains a relation177* that matches the specified key.178* @param key the AccessibleRelation key179* @return true if the relation is in the relation set; otherwise false180*/181public boolean contains(String key) {182return get(key) != null;183}184185/**186* Returns the relation that matches the specified key.187* @param key the AccessibleRelation key188* @return the relation, if one exists, that matches the specified key.189* Otherwise, null is returned.190*/191public AccessibleRelation get(String key) {192if (relations == null) {193return null;194} else {195int len = relations.size();196for (int i = 0; i < len; i++) {197AccessibleRelation relation =198(AccessibleRelation)relations.elementAt(i);199if (relation != null && relation.getKey().equals(key)) {200return relation;201}202}203return null;204}205}206207/**208* Returns the current relation set as an array of AccessibleRelation209* @return AccessibleRelation array contacting the current relation.210*/211public AccessibleRelation[] toArray() {212if (relations == null) {213return new AccessibleRelation[0];214} else {215AccessibleRelation[] relationArray216= new AccessibleRelation[relations.size()];217for (int i = 0; i < relationArray.length; i++) {218relationArray[i] = (AccessibleRelation) relations.elementAt(i);219}220return relationArray;221}222}223224/**225* Creates a localized String representing all the relations in the set226* using the default locale.227*228* @return comma separated localized String229* @see AccessibleBundle#toDisplayString230*/231public String toString() {232String ret = "";233if ((relations != null) && (relations.size() > 0)) {234ret = ((AccessibleRelation) (relations.elementAt(0))).toDisplayString();235for (int i = 1; i < relations.size(); i++) {236ret = ret + ","237+ ((AccessibleRelation) (relations.elementAt(i))).238toDisplayString();239}240}241return ret;242}243}244245246