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 AccessibleStateSet {
027:
028: protected Vector<AccessibleState> states;
029:
030: public AccessibleStateSet() {
031: super ();
032: }
033:
034: public AccessibleStateSet(AccessibleState[] states) {
035: super ();
036: if (states.length == 0) {
037: return;
038: }
039: this .states = new Vector<AccessibleState>(states.length);
040: for (AccessibleState element : states) {
041: if (!this .states.contains(element)) {
042: this .states.addElement(element);
043: }
044: }
045: }
046:
047: public boolean add(final AccessibleState state) {
048: initStorage();
049: if (states.contains(state)) {
050: return false;
051: }
052: states.add(state);
053: return true;
054: }
055:
056: public void addAll(final AccessibleState[] states) {
057: if (states.length == 0) {
058: return;
059: }
060: initStorage(states.length);
061: for (AccessibleState element : states) {
062: if (!this .states.contains(element)) {
063: this .states.addElement(element);
064: }
065: }
066: }
067:
068: public boolean contains(AccessibleState state) {
069: return states == null ? false : states.contains(state);
070: }
071:
072: public boolean remove(AccessibleState state) {
073: return states == null ? false : states.remove(state);
074: }
075:
076: public void clear() {
077: if (states != null) {
078: states.clear();
079: }
080: }
081:
082: public AccessibleState[] toArray() {
083: return states == null ? new AccessibleState[0] : states
084: .toArray(new AccessibleState[states.size()]);
085: }
086:
087: @Override
088: public String toString() {
089: if (states == null) {
090: return null;
091: }
092: StringBuffer str = new StringBuffer();
093: for (Iterator<AccessibleState> it = states.iterator(); it
094: .hasNext();) {
095: str.append(it.next().toString());
096: if (it.hasNext()) {
097: str.append(","); //$NON-NLS-1$
098: }
099: }
100: return str.toString();
101: }
102:
103: private void initStorage(final int capacity) {
104: if (states == null) {
105: states = new Vector<AccessibleState>(capacity);
106: }
107: }
108:
109: private void initStorage() {
110: if (states == null) {
111: states = new Vector<AccessibleState>();
112: }
113: }
114: }
|