[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[1862] | 2 | package org.openstreetmap.josm.tools;
|
---|
| 3 |
|
---|
| 4 | import java.util.AbstractList;
|
---|
[6717] | 5 | import java.util.Arrays;
|
---|
[2741] | 6 | import java.util.ConcurrentModificationException;
|
---|
| 7 | import java.util.Iterator;
|
---|
| 8 | import java.util.NoSuchElementException;
|
---|
[1862] | 9 | import java.util.RandomAccess;
|
---|
| 10 |
|
---|
| 11 | /**
|
---|
| 12 | * A List implementation initially based on given array, but never modifying
|
---|
| 13 | * the array directly. On the first modification, the implementation will
|
---|
| 14 | * create its own copy of the array, and after that it behaves mostly as
|
---|
| 15 | * an ArrayList.
|
---|
| 16 | *
|
---|
| 17 | * @author nenik
|
---|
[9231] | 18 | * @param <E> the type of elements in this list
|
---|
[1862] | 19 | */
|
---|
[10309] | 20 | public final class CopyList<E> extends AbstractList<E> implements RandomAccess {
|
---|
[1862] | 21 | private E[] array;
|
---|
| 22 | private int size;
|
---|
| 23 | private boolean pristine;
|
---|
| 24 |
|
---|
| 25 | /**
|
---|
| 26 | * Create a List over given array.
|
---|
[9231] | 27 | * @param array The initial List content. The array is never modified by the {@code CopyList}.
|
---|
[1862] | 28 | */
|
---|
| 29 | public CopyList(E[] array) {
|
---|
| 30 | this(array, array.length);
|
---|
| 31 | }
|
---|
| 32 |
|
---|
[9231] | 33 | /**
|
---|
| 34 | * Create a List over given array and size.
|
---|
| 35 | * @param array The initial List content. The array is never modified by the {@code CopyList}.
|
---|
| 36 | * @param size number of items
|
---|
| 37 | */
|
---|
[2907] | 38 | public CopyList(E[] array, int size) {
|
---|
[1862] | 39 | this.array = array;
|
---|
| 40 | this.size = size;
|
---|
| 41 | pristine = true;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | // read-only access:
|
---|
[6362] | 45 | @Override
|
---|
| 46 | public E get(int index) {
|
---|
[1862] | 47 | rangeCheck(index);
|
---|
| 48 | return array[index];
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[6362] | 51 | @Override
|
---|
| 52 | public int size() {
|
---|
[1862] | 53 | return size;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | // modification:
|
---|
[6362] | 57 | @Override
|
---|
| 58 | public E set(int index, E element) {
|
---|
[1862] | 59 | rangeCheck(index);
|
---|
| 60 | changeCheck();
|
---|
| 61 |
|
---|
| 62 | E old = array[index];
|
---|
| 63 | array[index] = element;
|
---|
| 64 | return old;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | // full resizable semantics:
|
---|
[6362] | 68 | @Override
|
---|
| 69 | public void add(int index, E element) {
|
---|
[1862] | 70 | // range check
|
---|
| 71 | ensureCapacity(size+1);
|
---|
| 72 | changeCheck();
|
---|
| 73 |
|
---|
| 74 | System.arraycopy(array, index, array, index+1, size-index);
|
---|
| 75 | array[index] = element;
|
---|
| 76 | size++;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[6362] | 79 | @Override
|
---|
| 80 | public E remove(int index) {
|
---|
[1862] | 81 | rangeCheck(index);
|
---|
| 82 | changeCheck();
|
---|
| 83 |
|
---|
| 84 | modCount++;
|
---|
| 85 | E element = array[index];
|
---|
| 86 | if (index < size-1) {
|
---|
| 87 | System.arraycopy(array, index+1, array, index, size-index-1);
|
---|
| 88 | } else {
|
---|
| 89 | array[index] = null;
|
---|
| 90 | }
|
---|
| 91 | size--;
|
---|
| 92 | return element;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | // speed optimizations:
|
---|
[6362] | 96 | @Override
|
---|
| 97 | public boolean add(E element) {
|
---|
[1862] | 98 | ensureCapacity(size+1);
|
---|
| 99 | changeCheck();
|
---|
| 100 | array[size++] = element;
|
---|
| 101 | return true;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[6362] | 104 | @Override
|
---|
| 105 | public void clear() {
|
---|
[2741] | 106 | modCount++;
|
---|
[1862] | 107 |
|
---|
| 108 | // clean up the array
|
---|
[2741] | 109 | while (size > 0) {
|
---|
| 110 | array[--size] = null;
|
---|
| 111 | }
|
---|
[1862] | 112 | }
|
---|
| 113 |
|
---|
| 114 | // helpers:
|
---|
| 115 |
|
---|
| 116 | private void rangeCheck(int index) {
|
---|
[2930] | 117 | if (index >= size || index < 0) throw new IndexOutOfBoundsException("Index:" + index + " Size:" + size);
|
---|
[1862] | 118 | }
|
---|
| 119 |
|
---|
| 120 | private void changeCheck() {
|
---|
| 121 | if (pristine) {
|
---|
| 122 | array = array.clone();
|
---|
| 123 | pristine = false;
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | private void ensureCapacity(int target) {
|
---|
| 128 | modCount++;
|
---|
| 129 | if (target > array.length) {
|
---|
| 130 | int newCapacity = Math.max(target, (array.length * 3)/2 + 1);
|
---|
[6717] | 131 | array = Arrays.copyOf(array, newCapacity);
|
---|
[1862] | 132 | pristine = false;
|
---|
[2741] | 133 | }
|
---|
[1862] | 134 | }
|
---|
[2741] | 135 |
|
---|
| 136 | @Override
|
---|
| 137 | public Iterator<E> iterator() {
|
---|
| 138 | return new Itr();
|
---|
[1862] | 139 | }
|
---|
[2741] | 140 |
|
---|
| 141 | private class Itr implements Iterator<E> {
|
---|
| 142 | /**
|
---|
| 143 | * Index of element to be returned by subsequent call to next.
|
---|
| 144 | */
|
---|
[8840] | 145 | private int cursor;
|
---|
[2741] | 146 |
|
---|
| 147 | /**
|
---|
| 148 | * Index of element returned by most recent call to next or
|
---|
| 149 | * previous. Reset to -1 if this element is deleted by a call
|
---|
| 150 | * to remove.
|
---|
| 151 | */
|
---|
[8285] | 152 | private int lastRet = -1;
|
---|
[2741] | 153 |
|
---|
| 154 | /**
|
---|
| 155 | * The modCount value that the iterator believes that the backing
|
---|
| 156 | * List should have. If this expectation is violated, the iterator
|
---|
| 157 | * has detected concurrent modification.
|
---|
| 158 | */
|
---|
[8285] | 159 | private int expectedModCount = modCount;
|
---|
[2741] | 160 |
|
---|
[6084] | 161 | @Override
|
---|
[2741] | 162 | public boolean hasNext() {
|
---|
| 163 | return cursor != size;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[6084] | 166 | @Override
|
---|
[2741] | 167 | public E next() {
|
---|
[10314] | 168 | if (!hasNext()) {
|
---|
| 169 | throw new NoSuchElementException();
|
---|
| 170 | }
|
---|
[2741] | 171 | checkForComodification();
|
---|
| 172 | try {
|
---|
| 173 | E next = array[cursor];
|
---|
| 174 | lastRet = cursor++;
|
---|
| 175 | return next;
|
---|
| 176 | } catch (IndexOutOfBoundsException e) {
|
---|
| 177 | checkForComodification();
|
---|
[9997] | 178 | throw (NoSuchElementException) new NoSuchElementException(e.getMessage()).initCause(e);
|
---|
[2741] | 179 | }
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[6084] | 182 | @Override
|
---|
[2741] | 183 | public void remove() {
|
---|
| 184 | if (lastRet == -1)
|
---|
| 185 | throw new IllegalStateException();
|
---|
| 186 | checkForComodification();
|
---|
| 187 |
|
---|
| 188 | try {
|
---|
| 189 | CopyList.this.remove(lastRet);
|
---|
| 190 | if (lastRet < cursor) {
|
---|
| 191 | cursor--;
|
---|
| 192 | }
|
---|
| 193 | lastRet = -1;
|
---|
| 194 | expectedModCount = modCount;
|
---|
| 195 | } catch (IndexOutOfBoundsException e) {
|
---|
[8394] | 196 | throw new ConcurrentModificationException(e);
|
---|
[2741] | 197 | }
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | final void checkForComodification() {
|
---|
| 201 | if (modCount != expectedModCount)
|
---|
| 202 | throw new ConcurrentModificationException();
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[1862] | 206 | }
|
---|