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.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.ResourceSet;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.impl.ResourceImpl;
26 import org.simantics.db.impl.graph.ReadGraphImpl;
27 import org.simantics.db.impl.support.ResourceSupport;
30 final public class IntSet implements ResourceSet {
32 final private ResourceSupport support;
36 /** the index after the last entry in the list */
37 public int sizeOrData;
39 /** the default capacity for new lists */
40 protected static final int DEFAULT_CAPACITY = 3;
42 public static final int NO_DATA = -1;
44 private static final Object[] EMPTY_ARRAY = new Object[0];
46 public static IntSet EMPTY = new IntSet();
52 public IntSet(QuerySupport support) {
53 this.support = support.getSupport();
58 public IntSet(QuerySupport support, int value) {
59 this.support = support.getSupport();
65 public int hashCode() {
66 return 31 * sizeOrData + 41 * Arrays.hashCode(data);
70 public boolean equals(Object object) {
73 else if (object == null)
75 else if (IntSet.class != object.getClass())
77 IntSet r = (IntSet)object;
78 // System.out.println("equals " + this + " vs. " + r);
79 return sizeOrData == r.sizeOrData && Arrays.equals(data, r.data);
84 * Returns the number of values in the list.
86 * @return the number of values in the list.
89 return data != null ? sizeOrData : (sizeOrData != NO_DATA ? 1 : 0);
93 * Tests whether this list contains any values.
95 * @return true if the list is empty.
97 public boolean isEmpty() {
98 return sizeOrData == NO_DATA;
104 * Adds <tt>val</tt> to the end of the list, growing as needed.
106 * @param val an <code>int</code> value
108 public boolean add(int val) {
110 if(sizeOrData == val) return false;
111 if(sizeOrData == NO_DATA) {
115 data = new int[DEFAULT_CAPACITY];
116 data[0] = sizeOrData;
122 for(int i=0;i<sizeOrData;i++) if(data[i] == val) return false;
123 if(data.length == sizeOrData) {
124 int newCap = data.length << 1;
125 int[] tmp = new int[newCap];
126 System.arraycopy(data, 0, tmp, 0, data.length);
128 data[sizeOrData++] = val;
130 data[sizeOrData++] = val;
136 public void forEach(TIntProcedure proc) {
138 if(sizeOrData != NO_DATA) proc.execute(sizeOrData);
140 for(int i = 0;i < sizeOrData ; i++) proc.execute(data[i]);
144 public boolean contains(int val) {
146 return sizeOrData == val;
148 for(int i = 0;i < sizeOrData ; i++) if(data[i] == val) return true;
154 if(data != null && sizeOrData < data.length) {
155 int newCap = sizeOrData;
156 int[] tmp = new int[newCap];
157 System.arraycopy(data, 0, tmp, 0, newCap);
164 public boolean add(Resource e) {
165 return add(((ResourceImpl)e).id);
169 public boolean addAll(Collection<? extends Resource> c) {
171 for (Resource r : c) {
179 public void clear() {
180 sizeOrData = NO_DATA;
185 public boolean contains(Object o) {
187 return contains(((ResourceImpl)o).id);
191 public boolean containsAll(Collection<?> c) {
200 public Iterator<Resource> iterator() {
202 class ArraySetIterator implements Iterator<Resource> {
207 public boolean hasNext() {
208 return next < size();
212 public Resource next() {
215 return new ResourceImpl(support, sizeOrData);
217 return new ResourceImpl(support, data[next++]);
222 public void remove() {
223 throw new UnsupportedOperationException();
228 return new ArraySetIterator();
233 public boolean remove(Object o) {
234 throw new UnsupportedOperationException();
238 public boolean removeAll(Collection<?> c) {
239 throw new UnsupportedOperationException();
243 public boolean retainAll(Collection<?> c) {
244 throw new UnsupportedOperationException();
248 public Object[] toArray() {
250 Object[] result = new Object[sizeOrData];
251 for(int i=0;i<sizeOrData;++i)
252 result[i] = new ResourceImpl(support, data[i]);
255 else if(sizeOrData == NO_DATA)
258 return new Object[] { new ResourceImpl(support, sizeOrData) };
262 public <T> T[] toArray(T[] a) {
263 throw new UnsupportedOperationException();
267 public String toString() {
268 return "IntSet " + sizeOrData + " " + Arrays.toString(data);
272 public boolean disjoint(Set<Resource> other) {
273 if(other instanceof ResourceSet) {
274 ResourceSet rs = (ResourceSet)other;
276 return !rs.contains(sizeOrData);
278 for(int i = 0;i < sizeOrData ; i++)
279 if(rs.contains(data[i])) return false;
284 return !other.contains(sizeOrData);
286 for(int i = 0;i < sizeOrData ; i++)
287 if(other.contains(data[i])) return false;
293 public void forEach(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException {
295 for(int i=0;i<sizeOrData;++i)
296 procedure.execute(graph, data[i]);
298 else if(sizeOrData == NO_DATA) {
300 procedure.execute(graph, sizeOrData);
302 procedure.finished(graph);