| 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 org.h2.mvstore.DataUtils; |
| 11 |
|
| 12 |
/** |
| 13 |
* An immutable array. |
| 14 |
* |
| 15 |
* @param <K> the type |
| 16 |
*/ |
| 17 |
public final class ImmutableArray<K> implements Iterable<K> { |
| 18 |
|
| 19 |
private static final ImmutableArray<?> EMPTY = new ImmutableArray<>( |
| 20 |
new Object[0]); |
| 21 |
|
| 22 |
/** |
| 23 |
* The array. |
| 24 |
*/ |
| 25 |
private final K[] array; |
| 26 |
|
| 27 |
private ImmutableArray(K[] array) { |
| 28 |
this.array = array; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Get the entry at this index. |
| 33 |
* |
| 34 |
* @param index the index |
| 35 |
* @return the entry |
| 36 |
*/ |
| 37 |
public K get(int index) { |
| 38 |
return array[index]; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Get the length. |
| 43 |
* |
| 44 |
* @return the length |
| 45 |
*/ |
| 46 |
public int length() { |
| 47 |
return array.length; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Set the entry at this index. |
| 52 |
* |
| 53 |
* @param index the index |
| 54 |
* @param obj the object |
| 55 |
* @return the new immutable array |
| 56 |
*/ |
| 57 |
public ImmutableArray<K> set(int index, K obj) { |
| 58 |
K[] array = this.array.clone(); |
| 59 |
array[index] = obj; |
| 60 |
return new ImmutableArray<>(array); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Insert an entry at this index. |
| 65 |
* |
| 66 |
* @param index the index |
| 67 |
* @param obj the object |
| 68 |
* @return the new immutable array |
| 69 |
*/ |
| 70 |
public ImmutableArray<K> insert(int index, K obj) { |
| 71 |
int len = array.length + 1; |
| 72 |
@SuppressWarnings("unchecked") |
| 73 |
K[] array = (K[]) new Object[len]; |
| 74 |
DataUtils.copyWithGap(this.array, array, this.array.length, index); |
| 75 |
array[index] = obj; |
| 76 |
return new ImmutableArray<>(array); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Remove the entry at this index. |
| 81 |
* |
| 82 |
* @param index the index |
| 83 |
* @return the new immutable array |
| 84 |
*/ |
| 85 |
public ImmutableArray<K> remove(int index) { |
| 86 |
int len = array.length - 1; |
| 87 |
@SuppressWarnings("unchecked") |
| 88 |
K[] array = (K[]) new Object[len]; |
| 89 |
DataUtils.copyExcept(this.array, array, this.array.length, index); |
| 90 |
return new ImmutableArray<>(array); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Get a sub-array. |
| 95 |
* |
| 96 |
* @param fromIndex the index of the first entry |
| 97 |
* @param toIndex the end index, plus one |
| 98 |
* @return the new immutable array |
| 99 |
*/ |
| 100 |
public ImmutableArray<K> subArray(int fromIndex, int toIndex) { |
| 101 |
return new ImmutableArray<>(Arrays.copyOfRange(array, fromIndex, toIndex)); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Create an immutable array. |
| 106 |
* |
| 107 |
* @param array the data |
| 108 |
* @return the new immutable array |
| 109 |
*/ |
| 110 |
@SafeVarargs |
| 111 |
public static <K> ImmutableArray<K> create(K... array) { |
| 112 |
return new ImmutableArray<>(array); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Get the data. |
| 117 |
* |
| 118 |
* @return the data |
| 119 |
*/ |
| 120 |
public K[] array() { |
| 121 |
return array; |
| 122 |
} |
| 123 |
|
| 124 |
@Override |
| 125 |
public String toString() { |
| 126 |
StringBuilder buff = new StringBuilder(); |
| 127 |
for (K obj : this) { |
| 128 |
buff.append(' ').append(obj); |
| 129 |
} |
| 130 |
return buff.toString(); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Get an empty immutable array. |
| 135 |
* |
| 136 |
* @param <K> the key type |
| 137 |
* @return the array |
| 138 |
*/ |
| 139 |
@SuppressWarnings("unchecked") |
| 140 |
public static <K> ImmutableArray<K> empty() { |
| 141 |
return (ImmutableArray<K>) EMPTY; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Get an iterator over all entries. |
| 146 |
* |
| 147 |
* @return the iterator |
| 148 |
*/ |
| 149 |
@Override |
| 150 |
public Iterator<K> iterator() { |
| 151 |
return new Iterator<K>() { |
| 152 |
|
| 153 |
ImmutableArray<K> a = ImmutableArray.this; |
| 154 |
int index; |
| 155 |
|
| 156 |
@Override |
| 157 |
public boolean hasNext() { |
| 158 |
return index < a.length(); |
| 159 |
} |
| 160 |
|
| 161 |
@Override |
| 162 |
public K next() { |
| 163 |
return a.get(index++); |
| 164 |
} |
| 165 |
|
| 166 |
}; |
| 167 |
} |
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|