]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/IntArray.java
Fixed multiple issues causing dangling references to discarded queries
[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 import org.simantics.db.exception.DatabaseException;
17
18
19 final public class IntArray {
20
21         public static IntArray EMPTY = new IntArray();
22         
23     public int[] data;
24
25     /** the index after the last entry in the list */
26     public int sizeOrData;
27
28     /** the default capacity for new lists */
29     protected static final int DEFAULT_CAPACITY = 3;
30
31     protected static final int NO_DATA = -1;
32
33     public IntArray() {
34         data = null;
35         sizeOrData = NO_DATA;
36     }
37
38     /**
39      * Returns the number of values in the list.
40      *
41      * @return the number of values in the list.
42      */
43     public int size() {
44         return data != null ? sizeOrData : (sizeOrData != NO_DATA ? 1 : 0);
45     }
46
47     /**
48      * Tests whether this list contains any values.
49      *
50      * @return true if the list is empty.
51      */
52     public boolean isEmpty() {
53         return sizeOrData == NO_DATA;
54     }
55
56 //    /**
57 //     * Sheds any excess capacity above and beyond the current size of
58 //     * the list.
59 //     */
60 //    public void trimToSize() {
61 //        if (_data.length > size()) {
62 //            int[] tmp = new int[size()];
63 //            toNativeArray(tmp, 0, tmp.length);
64 //            _data = tmp;
65 //        }
66 //    }
67
68     // modifying
69
70     public void trim() {
71         if(data != null) {
72                 int[] newData = new int[sizeOrData];
73                 System.arraycopy(data, 0, newData, 0, sizeOrData);
74                 data = newData;
75         }
76     }
77     
78     /**
79      * Adds <tt>val</tt> to the end of the list, growing as needed.
80      *
81      * @param val an <code>int</code> value
82      */
83     public void add(int val) {
84         if(data == null) {
85             if(sizeOrData == NO_DATA) {
86                 sizeOrData = val;
87             } else {
88                 data = new int[DEFAULT_CAPACITY];
89                 data[0] = sizeOrData;
90                 data[1] = val;
91                 sizeOrData = 2;
92             }
93         } else {
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);
98                 data = tmp;
99                 data[sizeOrData++] = val;
100             } else {
101                 data[sizeOrData++] = val;
102             }
103         }
104     }
105     
106     int[] toArray() {
107         int size = size();
108         int[] result = new int[size];
109         if(size == 1) {
110                 result[0] = sizeOrData; 
111         } else {
112             System.arraycopy(data, 0, result, 0, size);
113         }
114         return result;
115     }
116  
117     public void serialize(QuerySerializer serializer) {
118         int size = size();
119         serializer.writeLE(size);
120         if(size == 1) {
121             serializer.addResource(sizeOrData);
122         } else {
123             for(int i=0;i<size;i++)
124                 serializer.addResource(data[i]);
125         }
126     }
127  
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());
133         }
134         return result;
135     }
136
137     @Override
138     public boolean equals(Object object) {
139         if (this == object)
140             return true;
141         else if (object == null)
142             return false;
143         else if (IntArray.class != object.getClass())
144             return false;
145         IntArray r = (IntArray)object;
146 //        System.out.println("equals " + this + " vs. " + r);
147         return sizeOrData == r.sizeOrData && Arrays.equals(data, r.data);
148     }
149     
150     @Override
151     public String toString() {
152         return "IntArray " + sizeOrData + " " + Arrays.toString(data);
153     }
154     
155 }