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