]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/IntArray.java
QueryListening sync is slow
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / IntArray.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.impl.query;
13
14 import java.util.Arrays;
15
16
17 final public class IntArray {
18
19         public static IntArray EMPTY = new IntArray();
20         
21     public int[] data;
22
23     /** the index after the last entry in the list */
24     public int sizeOrData;
25
26     /** the default capacity for new lists */
27     protected static final int DEFAULT_CAPACITY = 3;
28
29     protected static final int NO_DATA = -1;
30
31     public IntArray() {
32         data = null;
33         sizeOrData = NO_DATA;
34     }
35
36     /**
37      * Returns the number of values in the list.
38      *
39      * @return the number of values in the list.
40      */
41     public int size() {
42         return data != null ? sizeOrData : (sizeOrData != NO_DATA ? 1 : 0);
43     }
44
45     /**
46      * Tests whether this list contains any values.
47      *
48      * @return true if the list is empty.
49      */
50     public boolean isEmpty() {
51         return sizeOrData == NO_DATA;
52     }
53
54 //    /**
55 //     * Sheds any excess capacity above and beyond the current size of
56 //     * the list.
57 //     */
58 //    public void trimToSize() {
59 //        if (_data.length > size()) {
60 //            int[] tmp = new int[size()];
61 //            toNativeArray(tmp, 0, tmp.length);
62 //            _data = tmp;
63 //        }
64 //    }
65
66     // modifying
67
68     public void trim() {
69         if(data != null) {
70                 int[] newData = new int[sizeOrData];
71                 System.arraycopy(data, 0, newData, 0, sizeOrData);
72                 data = newData;
73         }
74     }
75     
76     /**
77      * Adds <tt>val</tt> to the end of the list, growing as needed.
78      *
79      * @param val an <code>int</code> value
80      */
81     public void add(int val) {
82         if(data == null) {
83             if(sizeOrData == NO_DATA) {
84                 sizeOrData = val;
85             } else {
86                 data = new int[DEFAULT_CAPACITY];
87                 data[0] = sizeOrData;
88                 data[1] = val;
89                 sizeOrData = 2;
90             }
91         } else {
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);
96                 data = tmp;
97                 data[sizeOrData++] = val;
98             } else {
99                 data[sizeOrData++] = val;
100             }
101         }
102     }
103     
104     int[] toArray() {
105         int size = size();
106         int[] result = new int[size];
107         if(size == 1) {
108                 result[0] = sizeOrData; 
109         } else {
110             System.arraycopy(data, 0, result, 0, size);
111         }
112         return result;
113     }
114     
115     @Override
116     public boolean equals(Object object) {
117         if (this == object)
118             return true;
119         else if (object == null)
120             return false;
121         else if (IntArray.class != object.getClass())
122             return false;
123         IntArray r = (IntArray)object;
124 //        System.out.println("equals " + this + " vs. " + r);
125         return sizeOrData == r.sizeOrData && Arrays.equals(data, r.data);
126     }
127     
128     @Override
129     public String toString() {
130         return "IntArray " + sizeOrData + " " + Arrays.toString(data);
131     }
132     
133 }