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