1 /* 2 * Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.awt.X11; 27 28 import java.util.*; 29 30 /** 31 * Helper class to ease the work with the lists of atoms. 32 */ 33 class XAtomList { 34 Set<XAtom> atoms = new HashSet<XAtom>(); 35 36 /** 37 * Creates empty list. 38 */ 39 public XAtomList() { 40 } 41 42 /** 43 * Creates instance of XAtomList and initializes it with 44 * the contents pointer by <code>data</code>. 45 * Uses default display to initialize atoms. 46 */ 47 public XAtomList(long data, int count) { 48 init(data, count); 49 } 50 private void init(long data, int count) { 51 for (int i = 0; i < count; i++) { 52 add(new XAtom(XToolkit.getDisplay(), XAtom.getAtom(data+count*XAtom.getAtomSize()))); 53 } 54 } 55 56 /** 57 * Creates instance of XAtomList and initializes it with 58 * the arrays of atoms. Array can contain null atoms. 59 */ 60 public XAtomList(XAtom[] atoms) { 61 init(atoms); 62 } 63 private void init(XAtom[] atoms) { 64 for (int i = 0; i < atoms.length; i++) { 65 add(atoms[i]); 66 } 67 } 68 69 /** 70 * Returns contents of the list as array of atoms. 71 */ 72 public XAtom[] getAtoms() { 73 XAtom[] res = new XAtom[size()]; 74 Iterator<XAtom> iter = atoms.iterator(); 75 int i = 0; 76 while (iter.hasNext()) { 77 res[i++] = iter.next(); 78 } 79 return res; 80 } 81 82 /** 83 * Returns contents of the list as pointer to native data 84 * The size of the native data is size of the list multiplied by 85 * size of the Atom type on the platform. Caller is responsible for 86 * freeing the data by Unsafe.freeMemory when it is no longer needed. 87 */ 88 public long getAtomsData() { 89 return XAtom.toData(getAtoms()); 90 } 91 92 /** 93 * Returns true if this list contains the atom <code>atom</code> 94 */ 95 public boolean contains(XAtom atom) { 96 return atoms.contains(atom); 97 } 98 99 /** 100 * Add atom to the list. Does nothing if list already contains this atom. 101 */ 102 public void add(XAtom atom) { 103 atoms.add(atom); 104 } 105 106 /** 107 * Removes atom from the list. Does nothing if arrays doesn't conaint this atom. 108 */ 109 public void remove(XAtom atom) { 110 atoms.remove(atom); 111 } 112 113 114 /** 115 * Returns size of the list 116 */ 117 public int size() { 118 return atoms.size(); 119 } 120 121 /** 122 * Returns a subset of a list which is intersection of this set and set build by mapping <code>mask</code> in 123 * <code>mapping</code>. 124 */ 125 public XAtomList subset(int mask, Map<Integer, XAtom> mapping) { 126 XAtomList res = new XAtomList(); 127 Iterator<Integer> iter = mapping.keySet().iterator(); 128 while (iter.hasNext()) { 129 Integer bits = iter.next(); 130 if ( (mask & bits.intValue()) == bits.intValue() ) { 131 XAtom atom = mapping.get(bits); 132 if (contains(atom)) { 133 res.add(atom); 134 } 135 } 136 } 137 return res; 138 } 139 140 /** 141 * Returns iterator for items. 142 */ 143 public Iterator<XAtom> iterator() { 144 return atoms.iterator(); 145 } 146 147 /** 148 * Merges without duplicates all the atoms from another list 149 */ 150 public void addAll(XAtomList atoms) { 151 Iterator<XAtom> iter = atoms.iterator(); 152 while(iter.hasNext()) { 153 add(iter.next()); 154 } 155 } 156 157 public String toString() { 158 StringBuffer buf = new StringBuffer(); 159 buf.append("["); 160 Iterator<XAtom> iter = atoms.iterator(); 161 while (iter.hasNext()) { 162 buf.append(iter.next().toString()); 163 if (iter.hasNext()) { 164 buf.append(", "); 165 } 166 } 167 buf.append("]"); 168 return buf.toString(); 169 } 170 }