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 java.util.Arrays;
16 import org.simantics.db.exception.DatabaseException;
19 final public class IntArray {
21 public static IntArray EMPTY = new IntArray();
25 /** the index after the last entry in the list */
26 public int sizeOrData;
28 /** the default capacity for new lists */
29 protected static final int DEFAULT_CAPACITY = 3;
31 protected static final int NO_DATA = -1;
39 * Returns the number of values in the list.
41 * @return the number of values in the list.
44 return data != null ? sizeOrData : (sizeOrData != NO_DATA ? 1 : 0);
48 * Tests whether this list contains any values.
50 * @return true if the list is empty.
52 public boolean isEmpty() {
53 return sizeOrData == NO_DATA;
57 // * Sheds any excess capacity above and beyond the current size of
60 // public void trimToSize() {
61 // if (_data.length > size()) {
62 // int[] tmp = new int[size()];
63 // toNativeArray(tmp, 0, tmp.length);
72 int[] newData = new int[sizeOrData];
73 System.arraycopy(data, 0, newData, 0, sizeOrData);
79 * Adds <tt>val</tt> to the end of the list, growing as needed.
81 * @param val an <code>int</code> value
83 public void add(int val) {
85 if(sizeOrData == NO_DATA) {
88 data = new int[DEFAULT_CAPACITY];
94 if(data.length == sizeOrData) {
95 int newCap = data.length << 1;
96 int[] tmp = new int[newCap];
97 System.arraycopy(data, 0, tmp, 0, data.length);
99 data[sizeOrData++] = val;
101 data[sizeOrData++] = val;
108 int[] result = new int[size];
110 result[0] = sizeOrData;
112 System.arraycopy(data, 0, result, 0, size);
117 public void serialize(QuerySerializer serializer) {
119 serializer.writeLE(size);
121 serializer.addResource(sizeOrData);
123 for(int i=0;i<size;i++)
124 serializer.addResource(data[i]);
128 public static IntArray deserialize(QueryDeserializer deserializer) throws DatabaseException {
129 int size = deserializer.readLE4();
130 IntArray result = new IntArray();
131 for(int i=0;i<size;i++) {
132 result.add(deserializer.readResource());
138 public boolean equals(Object object) {
141 else if (object == null)
143 else if (IntArray.class != object.getClass())
145 IntArray r = (IntArray)object;
146 // System.out.println("equals " + this + " vs. " + r);
147 return sizeOrData == r.sizeOrData && Arrays.equals(data, r.data);
151 public String toString() {
152 return "IntArray " + sizeOrData + " " + Arrays.toString(data);