001: /*
002: * Copyright 2003-2005 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.awt.X11;
027:
028: import java.util.*;
029:
030: /**
031: * Helper class to ease the work with the lists of atoms.
032: */
033: class XAtomList {
034: Set<XAtom> atoms = new HashSet<XAtom>();
035:
036: /**
037: * Creates empty list.
038: */
039: public XAtomList() {
040: }
041:
042: /**
043: * Creates instance of XAtomList and initializes it with
044: * the contents pointer by <code>data</code>.
045: * Uses default display to initialize atoms.
046: */
047: public XAtomList(long data, int count) {
048: init(data, count);
049: }
050:
051: private void init(long data, int count) {
052: for (int i = 0; i < count; i++) {
053: add(new XAtom(XToolkit.getDisplay(), XAtom.getAtom(data
054: + count * XAtom.getAtomSize())));
055: }
056: }
057:
058: /**
059: * Creates instance of XAtomList and initializes it with
060: * the arrays of atoms. Array can contain null atoms.
061: */
062: public XAtomList(XAtom[] atoms) {
063: init(atoms);
064: }
065:
066: private void init(XAtom[] atoms) {
067: for (int i = 0; i < atoms.length; i++) {
068: add(atoms[i]);
069: }
070: }
071:
072: /**
073: * Returns contents of the list as array of atoms.
074: */
075: public XAtom[] getAtoms() {
076: XAtom[] res = new XAtom[size()];
077: Iterator<XAtom> iter = atoms.iterator();
078: int i = 0;
079: while (iter.hasNext()) {
080: res[i++] = iter.next();
081: }
082: return res;
083: }
084:
085: /**
086: * Returns contents of the list as pointer to native data
087: * The size of the native data is size of the list multiplied by
088: * size of the Atom type on the platform. Caller is responsible for
089: * freeing the data by Unsafe.freeMemory when it is no longer needed.
090: */
091: public long getAtomsData() {
092: return XAtom.toData(getAtoms());
093: }
094:
095: /**
096: * Returns true if this list contains the atom <code>atom</code>
097: */
098: public boolean contains(XAtom atom) {
099: return atoms.contains(atom);
100: }
101:
102: /**
103: * Add atom to the list. Does nothing if list already contains this atom.
104: */
105: public void add(XAtom atom) {
106: atoms.add(atom);
107: }
108:
109: /**
110: * Removes atom from the list. Does nothing if arrays doesn't conaint this atom.
111: */
112: public void remove(XAtom atom) {
113: atoms.remove(atom);
114: }
115:
116: /**
117: * Returns size of the list
118: */
119: public int size() {
120: return atoms.size();
121: }
122:
123: /**
124: * Returns a subset of a list which is intersection of this set and set build by mapping <code>mask</code> in
125: * <code>mapping</code>.
126: */
127: public XAtomList subset(int mask, Map<Integer, XAtom> mapping) {
128: XAtomList res = new XAtomList();
129: Iterator<Integer> iter = mapping.keySet().iterator();
130: while (iter.hasNext()) {
131: Integer bits = iter.next();
132: if ((mask & bits.intValue()) == bits.intValue()) {
133: XAtom atom = mapping.get(bits);
134: if (contains(atom)) {
135: res.add(atom);
136: }
137: }
138: }
139: return res;
140: }
141:
142: /**
143: * Returns iterator for items.
144: */
145: public Iterator<XAtom> iterator() {
146: return atoms.iterator();
147: }
148:
149: /**
150: * Merges without duplicates all the atoms from another list
151: */
152: public void addAll(XAtomList atoms) {
153: Iterator<XAtom> iter = atoms.iterator();
154: while (iter.hasNext()) {
155: add(iter.next());
156: }
157: }
158:
159: public String toString() {
160: StringBuffer buf = new StringBuffer();
161: buf.append("[");
162: Iterator iter = atoms.iterator();
163: while (iter.hasNext()) {
164: buf.append(iter.next().toString());
165: if (iter.hasNext()) {
166: buf.append(", ");
167: }
168: }
169: buf.append("]");
170: return buf.toString();
171: }
172: }
|