1 | /* ============================================================= |
2 | * SmallSQL : a free Java DBMS library for the Java(tm) platform |
3 | * ============================================================= |
4 | * |
5 | * (C) Copyright 2004-2006, by Volker Berlin. |
6 | * |
7 | * Project Info: http://www.smallsql.de/ |
8 | * |
9 | * This library is free software; you can redistribute it and/or modify it |
10 | * under the terms of the GNU Lesser General Public License as published by |
11 | * the Free Software Foundation; either version 2.1 of the License, or |
12 | * (at your option) any later version. |
13 | * |
14 | * This library is distributed in the hope that it will be useful, but |
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
16 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
17 | * License for more details. |
18 | * |
19 | * You should have received a copy of the GNU Lesser General Public |
20 | * License along with this library; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
22 | * USA. |
23 | * |
24 | * [Java is a trademark or registered trademark of Sun Microsystems, Inc. |
25 | * in the United States and other countries.] |
26 | * |
27 | * --------------- |
28 | * Expressions.java |
29 | * --------------- |
30 | * Author: Volker Berlin |
31 | * |
32 | */ |
33 | package smallsql.database; |
34 | |
35 | /** |
36 | * @author Volker Berlin |
37 | * |
38 | */ |
39 | final class Expressions { |
40 | private int size; |
41 | private Expression[] data; |
42 | |
43 | Expressions(){ |
44 | data = new Expression[16]; |
45 | } |
46 | |
47 | Expressions(int initSize){ |
48 | data = new Expression[initSize]; |
49 | } |
50 | |
51 | |
52 | final int size(){ |
53 | return size; |
54 | } |
55 | |
56 | |
57 | final void setSize(int newSize){ |
58 | for(int i=newSize; i<size; i++) data[i] = null; |
59 | size = newSize; |
60 | if(size>data.length) resize(newSize); |
61 | } |
62 | |
63 | |
64 | final Expression get(int idx){ |
65 | if (idx >= size) |
66 | throw new IndexOutOfBoundsException("Index: "+idx+", Size: "+size); |
67 | return data[idx]; |
68 | } |
69 | |
70 | |
71 | final void add(Expression expr){ |
72 | if(size >= data.length ){ |
73 | resize(size << 1); |
74 | } |
75 | data[size++] = expr; |
76 | } |
77 | |
78 | final void add(int idx, Expression expr){ |
79 | if(size >= data.length ){ |
80 | resize(size << 1); |
81 | } |
82 | System.arraycopy( data, idx, data, idx+1, (size++)-idx); |
83 | data[idx] = expr; |
84 | } |
85 | |
86 | final void addAll(Expressions cols){ |
87 | int count = cols.size(); |
88 | if(size + count >= data.length ){ |
89 | resize(size + count); |
90 | } |
91 | System.arraycopy( cols.data, 0, data, size, count); |
92 | size += count; |
93 | } |
94 | |
95 | final void clear(){ |
96 | size = 0; |
97 | } |
98 | |
99 | final void remove(int idx){ |
100 | System.arraycopy( data, idx+1, data, idx, (--size)-idx); |
101 | } |
102 | |
103 | final void set(int idx, Expression expr){ |
104 | data[idx] = expr; |
105 | } |
106 | |
107 | final int indexOf(Expression expr) { |
108 | if (expr == null) { |
109 | for (int i = 0; i < size; i++) |
110 | if (data[i]==null) |
111 | return i; |
112 | } else { |
113 | for (int i = 0; i < size; i++) |
114 | if (expr.equals(data[i])) |
115 | return i; |
116 | } |
117 | return -1; |
118 | } |
119 | |
120 | final void toArray(Expression[] array){ |
121 | System.arraycopy( data, 0, array, 0, size); |
122 | } |
123 | |
124 | final Expression[] toArray(){ |
125 | Expression[] array = new Expression[size]; |
126 | System.arraycopy( data, 0, array, 0, size); |
127 | return array; |
128 | } |
129 | |
130 | private final void resize(int newSize){ |
131 | Expression[] dataNew = new Expression[newSize]; |
132 | System.arraycopy(data, 0, dataNew, 0, size); |
133 | data = dataNew; |
134 | } |
135 | } |