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;
17 final public class IntArray {
19 public static IntArray EMPTY = new IntArray();
23 /** the index after the last entry in the list */
24 public int sizeOrData;
26 /** the default capacity for new lists */
27 protected static final int DEFAULT_CAPACITY = 3;
29 protected static final int NO_DATA = -1;
37 * Returns the number of values in the list.
39 * @return the number of values in the list.
42 return data != null ? sizeOrData : (sizeOrData != NO_DATA ? 1 : 0);
46 * Tests whether this list contains any values.
48 * @return true if the list is empty.
50 public boolean isEmpty() {
51 return sizeOrData == NO_DATA;
55 // * Sheds any excess capacity above and beyond the current size of
58 // public void trimToSize() {
59 // if (_data.length > size()) {
60 // int[] tmp = new int[size()];
61 // toNativeArray(tmp, 0, tmp.length);
70 int[] newData = new int[sizeOrData];
71 System.arraycopy(data, 0, newData, 0, sizeOrData);
77 * Adds <tt>val</tt> to the end of the list, growing as needed.
79 * @param val an <code>int</code> value
81 public void add(int val) {
83 if(sizeOrData == NO_DATA) {
86 data = new int[DEFAULT_CAPACITY];
92 if(data.length == sizeOrData) {
93 int newCap = data.length << 1;
94 int[] tmp = new int[newCap];
95 System.arraycopy(data, 0, tmp, 0, data.length);
97 data[sizeOrData++] = val;
99 data[sizeOrData++] = val;
106 int[] result = new int[size];
108 result[0] = sizeOrData;
110 System.arraycopy(data, 0, result, 0, size);
116 public boolean equals(Object object) {
119 else if (object == null)
121 else if (IntArray.class != object.getClass())
123 IntArray r = (IntArray)object;
124 // System.out.println("equals " + this + " vs. " + r);
125 return sizeOrData == r.sizeOrData && Arrays.equals(data, r.data);
129 public String toString() {
130 return "IntArray " + sizeOrData + " " + Arrays.toString(data);