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