File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\util\IntArray.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.util;
8
9 import org.h2.constant.SysProperties;
10
11 /**
12 * An array with integer element.
13 */
14 public class IntArray {
15
16 private int[] data;
17 private int size;
18 private int hash;
19
20 /**
21 * Create an int array with the default initial capacity.
22 */
23 public IntArray() {
24 this(10);
25 }
26
27 /**
28 * Create an int array with specified initial capacity.
29 *
30 * @param capacity the initial capacity
31 */
32 public IntArray(int capacity) {
33 data = new int[capacity];
34 }
35
36 /**
37 * Create an int array with the given values and size.
38 *
39 * @param data the int array
40 */
41 public IntArray(int[] data) {
42 this.data = data;
43 size = data.length;
44 }
45
46 /**
47 * Append a value.
48 *
49 * @param value the value to append
50 */
51 public void add(int value) {
52 if (size >= data.length) {
53 ensureCapacity(size + size);
54 }
55 data[size++] = value;
56 }
57
58 /**
59 * Get the value at the given index.
60 *
61 * @param index the index
62 * @return the value
63 */
64 public int get(int index) {
65 if (SysProperties.CHECK) {
66 if (index >= size) {
67 throw new ArrayIndexOutOfBoundsException("i=" + index + " size=" + size);
68 }
69 }
70 return data[index];
71 }
72
73 /**
74 * Remove the value at the given index.
75 *
76 * @param index the index
77 */
78 public void remove(int index) {
79 if (SysProperties.CHECK) {
80 if (index >= size) {
81 throw new ArrayIndexOutOfBoundsException("i=" + index + " size=" + size);
82 }
83 }
84 System.arraycopy(data, index + 1, data, index, size - index - 1);
85 size--;
86 }
87
88 /**
89 * Ensure the the underlying array is large enough for the given number of
90 * entries.
91 *
92 * @param minCapacity the minimum capacity
93 */
94 public void ensureCapacity(int minCapacity) {
95 minCapacity = Math.max(4, minCapacity);
96 if (minCapacity >= data.length) {
97 int[] d = new int[minCapacity];
98 System.arraycopy(data, 0, d, 0, data.length);
99 data = d;
100 }
101 }
102
103 public boolean equals(Object obj) {
104 if (!(obj instanceof IntArray)) {
105 return false;
106 }
107 IntArray other = (IntArray) obj;
108 if (hashCode() != other.hashCode() || size != other.size) {
109 return false;
110 }
111 for (int i = 0; i < size; i++) {
112 if (data[i] != other.data[i]) {
113 return false;
114 }
115 }
116 return true;
117 }
118
119 public int hashCode() {
120 if (hash != 0) {
121 return hash;
122 }
123 int h = size + 1;
124 for (int i = 0; i < size; i++) {
125 h = h * 31 + data[i];
126 }
127 hash = h;
128 return h;
129 }
130
131 /**
132 * Get the size of the list.
133 *
134 * @return the size
135 */
136 public int size() {
137 return size;
138 }
139
140 /**
141 * Convert this list to an array. The target array must be big enough.
142 *
143 * @param array the target array
144 */
145 public void toArray(int[] array) {
146 System.arraycopy(data, 0, array, 0, size);
147 }
148
149 public String toString() {
150 StatementBuilder buff = new StatementBuilder("{");
151 for (int i = 0; i < size; i++) {
152 buff.appendExceptFirst(", ");
153 buff.append(data[i]);
154 }
155 return buff.append('}').toString();
156 }
157
158 /**
159 * Remove a number of elements.
160 *
161 * @param fromIndex the index of the first item to remove
162 * @param toIndex upper bound (exclusive)
163 */
164 public void removeRange(int fromIndex, int toIndex) {
165 if (SysProperties.CHECK) {
166 if (fromIndex > toIndex || toIndex > size) {
167 throw new ArrayIndexOutOfBoundsException("from=" + fromIndex + " to=" + toIndex + " size=" + size);
168 }
169 }
170 System.arraycopy(data, toIndex, data, fromIndex, size - toIndex);
171 size -= toIndex - fromIndex;
172 }
173
174 }
175