1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.db.impl.query;
\r
14 import gnu.trove.procedure.TIntProcedure;
\r
16 import java.util.Arrays;
\r
17 import java.util.Collection;
\r
18 import java.util.Iterator;
\r
19 import java.util.Set;
\r
21 import org.simantics.db.Resource;
\r
22 import org.simantics.db.ResourceSet;
\r
23 import org.simantics.db.impl.ResourceImpl;
\r
24 import org.simantics.db.impl.support.ResourceSupport;
\r
27 final public class IntSet implements ResourceSet {
\r
29 final private ResourceSupport support;
\r
33 /** the index after the last entry in the list */
\r
34 public int sizeOrData;
\r
36 /** the default capacity for new lists */
\r
37 protected static final int DEFAULT_CAPACITY = 3;
\r
39 public static final int NO_DATA = -1;
\r
44 sizeOrData = NO_DATA;
\r
47 public IntSet(QuerySupport support) {
\r
48 this.support = support.getSupport();
\r
50 sizeOrData = NO_DATA;
\r
53 public IntSet(QuerySupport support, int value) {
\r
54 this.support = support.getSupport();
\r
60 public int hashCode() {
\r
61 return 31 * sizeOrData + 41 * Arrays.hashCode(data);
\r
65 public boolean equals(Object object) {
\r
68 else if (object == null)
\r
70 else if (IntSet.class != object.getClass())
\r
72 IntSet r = (IntSet)object;
\r
73 // System.out.println("equals " + this + " vs. " + r);
\r
74 return sizeOrData == r.sizeOrData && Arrays.equals(data, r.data);
\r
79 * Returns the number of values in the list.
\r
81 * @return the number of values in the list.
\r
84 return data != null ? sizeOrData : (sizeOrData != NO_DATA ? 1 : 0);
\r
88 * Tests whether this list contains any values.
\r
90 * @return true if the list is empty.
\r
92 public boolean isEmpty() {
\r
93 return sizeOrData == NO_DATA;
\r
99 * Adds <tt>val</tt> to the end of the list, growing as needed.
\r
101 * @param val an <code>int</code> value
\r
103 public boolean add(int val) {
\r
105 if(sizeOrData == val) return false;
\r
106 if(sizeOrData == NO_DATA) {
\r
110 data = new int[DEFAULT_CAPACITY];
\r
111 data[0] = sizeOrData;
\r
117 for(int i=0;i<sizeOrData;i++) if(data[i] == val) return false;
\r
118 if(data.length == sizeOrData) {
\r
119 int newCap = data.length << 1;
\r
120 int[] tmp = new int[newCap];
\r
121 System.arraycopy(data, 0, tmp, 0, data.length);
\r
123 data[sizeOrData++] = val;
\r
125 data[sizeOrData++] = val;
\r
131 public void forEach(TIntProcedure proc) {
\r
133 if(sizeOrData != NO_DATA) proc.execute(sizeOrData);
\r
135 for(int i = 0;i < sizeOrData ; i++) proc.execute(data[i]);
\r
139 public boolean contains(int val) {
\r
141 return sizeOrData == val;
\r
143 for(int i = 0;i < sizeOrData ; i++) if(data[i] == val) return true;
\r
148 public void trim() {
\r
149 if(data != null && sizeOrData < data.length) {
\r
150 int newCap = sizeOrData;
\r
151 int[] tmp = new int[newCap];
\r
152 System.arraycopy(data, 0, tmp, 0, newCap);
\r
154 sizeOrData = newCap;
\r
159 public boolean add(Resource e) {
\r
160 return add(((ResourceImpl)e).id);
\r
164 public boolean addAll(Collection<? extends Resource> c) {
\r
165 boolean ret = false;
\r
166 for (Resource r : c) {
\r
174 public void clear() {
\r
175 sizeOrData = NO_DATA;
\r
180 public boolean contains(Object o) {
\r
182 return contains(((ResourceImpl)o).id);
\r
186 public boolean containsAll(Collection<?> c) {
\r
187 for (Object o : c) {
\r
195 public Iterator<Resource> iterator() {
\r
197 class ArraySetIterator implements Iterator<Resource> {
\r
202 public boolean hasNext() {
\r
203 return next < size();
\r
207 public Resource next() {
\r
210 return new ResourceImpl(support, sizeOrData);
\r
212 return new ResourceImpl(support, data[next++]);
\r
217 public void remove() {
\r
218 throw new UnsupportedOperationException();
\r
223 return new ArraySetIterator();
\r
228 public boolean remove(Object o) {
\r
229 throw new UnsupportedOperationException();
\r
233 public boolean removeAll(Collection<?> c) {
\r
234 throw new UnsupportedOperationException();
\r
238 public boolean retainAll(Collection<?> c) {
\r
239 throw new UnsupportedOperationException();
\r
243 public Object[] toArray() {
\r
244 throw new UnsupportedOperationException();
\r
248 public <T> T[] toArray(T[] a) {
\r
249 throw new UnsupportedOperationException();
\r
253 public String toString() {
\r
254 return "IntSet " + sizeOrData + " " + Arrays.toString(data);
\r
258 public boolean disjoint(Set<Resource> other) {
\r
259 if(other instanceof ResourceSet) {
\r
260 ResourceSet rs = (ResourceSet)other;
\r
262 return !rs.contains(sizeOrData);
\r
264 for(int i = 0;i < sizeOrData ; i++)
\r
265 if(rs.contains(data[i])) return false;
\r
270 return !other.contains(sizeOrData);
\r
272 for(int i = 0;i < sizeOrData ; i++)
\r
273 if(other.contains(data[i])) return false;
\r