001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Dennis Ushakov
020: * @version $Revision$
021: */package javax.accessibility;
022:
023: import java.util.Iterator;
024: import java.util.Vector;
025:
026: public class AccessibleRelationSet {
027:
028: protected Vector<AccessibleRelation> relations;
029:
030: public AccessibleRelationSet() {
031: super ();
032: }
033:
034: public AccessibleRelationSet(final AccessibleRelation[] relations) {
035: if (relations.length != 0) {
036: this .relations = new Vector<AccessibleRelation>(
037: relations.length);
038: for (AccessibleRelation relation : relations) {
039: add(relation);
040: }
041: }
042: }
043:
044: public boolean add(AccessibleRelation relation) {
045: initStorage();
046:
047: AccessibleRelation currentRelation = get(relation.getKey());
048: if (currentRelation == null) {
049: relations.addElement(relation);
050: return true;
051: }
052:
053: Object[] currentTarget = currentRelation.getTarget();
054: int combinedLength = currentTarget.length
055: + relation.getTarget().length;
056: Object[] combinedTarget = new Object[combinedLength];
057:
058: for (int i = 0; i < currentTarget.length; i++) {
059: combinedTarget[i] = currentTarget[i];
060: }
061: int index = 0;
062: for (int i = currentTarget.length; i < combinedLength; i++) {
063: combinedTarget[i] = relation.getTarget()[index++];
064: }
065:
066: currentRelation.setTarget(combinedTarget);
067:
068: return true;
069: }
070:
071: public void addAll(AccessibleRelation[] relations) {
072: if (relations.length != 0) {
073: initStorage();
074: for (AccessibleRelation relation : relations) {
075: add(relation);
076: }
077: }
078: }
079:
080: public boolean remove(AccessibleRelation relation) {
081: return relations != null && relations.remove(relation);
082: }
083:
084: public void clear() {
085: if (relations != null) {
086: relations.clear();
087: }
088: }
089:
090: public int size() {
091: return relations == null ? 0 : relations.size();
092: }
093:
094: public boolean contains(String key) {
095: return get(key) != null;
096: }
097:
098: public AccessibleRelation get(String key) {
099: if (relations == null) {
100: return null;
101: }
102: for (AccessibleRelation rel : relations) {
103: if (key.equals(rel.key)) {
104: return rel;
105: }
106: }
107: return null;
108: }
109:
110: public AccessibleRelation[] toArray() {
111: return relations == null ? new AccessibleRelation[0]
112: : relations.toArray(new AccessibleRelation[relations
113: .size()]);
114: }
115:
116: @Override
117: public String toString() {
118: if (relations == null) {
119: return ""; //$NON-NLS-1$
120: }
121: StringBuffer result = new StringBuffer();
122: for (Iterator<AccessibleRelation> it = relations.iterator(); it
123: .hasNext();) {
124: result.append(it.next());
125: if (it.hasNext()) {
126: result.append(","); //$NON-NLS-1$
127: }
128: }
129: return result.toString();
130: }
131:
132: private void initStorage() {
133: if (relations == null) {
134: relations = new Vector<AccessibleRelation>();
135: }
136: }
137: }
|