Debian Codegraph @

src/tools/org/h2/dev/util/ImmutableArray2.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.dev.util;
7
8 import java.util.Arrays;
9 import java.util.Iterator;
10 import java.util.concurrent.atomic.AtomicBoolean;
11 import org.h2.mvstore.DataUtils;
12
13 /**
14 * An immutable array.
15 *
16 * @param <K> the type
17 */
18 public final class ImmutableArray2<K> implements Iterable<K> {
19
20 private static final ImmutableArray2<?> EMPTY = new ImmutableArray2<>(
21 new Object[0], 0);
22
23 /**
24 * The array.
25 */
26 private final K[] array;
27 private final int length;
28 private AtomicBoolean canExtend;
29
30 private ImmutableArray2(K[] array, int len) {
31 this.array = array;
32 this.length = len;
33 }
34
35 private ImmutableArray2(K[] array, int len, boolean canExtend) {
36 this.array = array;
37 this.length = len;
38 if (canExtend) {
39 this.canExtend = new AtomicBoolean(true);
40 }
41 }
42
43 /**
44 * Get the entry at this index.
45 *
46 * @param index the index
47 * @return the entry
48 */
49 public K get(int index) {
50 if (index >= length) {
51 throw new IndexOutOfBoundsException();
52 }
53 return array[index];
54 }
55
56 /**
57 * Get the length.
58 *
59 * @return the length
60 */
61 public int length() {
62 return length;
63 }
64
65 /**
66 * Set the entry at this index.
67 *
68 * @param index the index
69 * @param obj the object
70 * @return the new immutable array
71 */
72 public ImmutableArray2<K> set(int index, K obj) {
73 K[] a2 = Arrays.copyOf(array, length);
74 a2[index] = obj;
75 return new ImmutableArray2<>(a2, length);
76 }
77
78 /**
79 * Insert an entry at this index.
80 *
81 * @param index the index
82 * @param obj the object
83 * @return the new immutable array
84 */
85 public ImmutableArray2<K> insert(int index, K obj) {
86 int len = length + 1;
87 int newLen = len;
88 boolean extendable;
89 if (index == len - 1) {
90 AtomicBoolean x = canExtend;
91 if (x != null) {
92 // can set it to null early - we anyway
93 // reset the flag, so it is no longer useful
94 canExtend = null;
95 if (array.length > index && x.getAndSet(false)) {
96 array[index] = obj;
97 return new ImmutableArray2<>(array, len, true);
98 }
99 }
100 extendable = true;
101 newLen = len + 4;
102 } else {
103 extendable = false;
104 }
105 @SuppressWarnings("unchecked")
106 K[] a2 = (K[]) new Object[newLen];
107 DataUtils.copyWithGap(array, a2, length, index);
108 a2[index] = obj;
109 return new ImmutableArray2<>(a2, len, extendable);
110 }
111
112 /**
113 * Remove the entry at this index.
114 *
115 * @param index the index
116 * @return the new immutable array
117 */
118 public ImmutableArray2<K> remove(int index) {
119 int len = length - 1;
120 if (index == len) {
121 return new ImmutableArray2<>(array, len);
122 }
123 @SuppressWarnings("unchecked")
124 K[] a2 = (K[]) new Object[len];
125 DataUtils.copyExcept(array, a2, length, index);
126 return new ImmutableArray2<>(a2, len);
127 }
128
129 /**
130 * Get a sub-array.
131 *
132 * @param fromIndex the index of the first entry
133 * @param toIndex the end index, plus one
134 * @return the new immutable array
135 */
136 public ImmutableArray2<K> subArray(int fromIndex, int toIndex) {
137 int len = toIndex - fromIndex;
138 if (fromIndex == 0) {
139 return new ImmutableArray2<>(array, len);
140 }
141 return new ImmutableArray2<>(Arrays.copyOfRange(array, fromIndex, toIndex), len);
142 }
143
144 /**
145 * Create an immutable array.
146 *
147 * @param array the data
148 * @return the new immutable array
149 */
150 @SafeVarargs
151 public static <K> ImmutableArray2<K> create(K... array) {
152 return new ImmutableArray2<>(array, array.length);
153 }
154
155 /**
156 * Get the data.
157 *
158 * @return the data
159 */
160 public K[] array() {
161 return array;
162 }
163
164 @Override
165 public String toString() {
166 StringBuilder buff = new StringBuilder();
167 for (K obj : this) {
168 buff.append(' ').append(obj);
169 }
170 return buff.toString();
171 }
172
173 /**
174 * Get an empty immutable array.
175 *
176 * @param <K> the key type
177 * @return the array
178 */
179 @SuppressWarnings("unchecked")
180 public static <K> ImmutableArray2<K> empty() {
181 return (ImmutableArray2<K>) EMPTY;
182 }
183
184 /**
185 * Get an iterator over all entries.
186 *
187 * @return the iterator
188 */
189 @Override
190 public Iterator<K> iterator() {
191 return new Iterator<K>() {
192
193 ImmutableArray2<K> a = ImmutableArray2.this;
194 int index;
195
196 @Override
197 public boolean hasNext() {
198 return index < a.length();
199 }
200
201 @Override
202 public K next() {
203 return a.get(index++);
204 }
205
206 };
207 }
208
209 }
210