| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.ma.trie; |
| 9 |
|
| 10 |
import java.util.Iterator; |
| 11 |
import java.util.NoSuchElementException; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
public abstract class ImmutableList<T> implements Iterable<T> { |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public abstract T head(); |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public abstract ImmutableList<T> tail(); |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public abstract boolean isEmpty(); |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public final int size() { |
| 49 |
ImmutableList<T> input = this; |
| 50 |
int size = 0; |
| 51 |
while (!input.isEmpty()) { |
| 52 |
size++; |
| 53 |
input = input.tail(); |
| 54 |
} |
| 55 |
return size; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public ImmutableList<T> prepend(T element) { |
| 65 |
return new NonEmptyList<>(element, this); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public static <T> ImmutableList<T> empty() { |
| 74 |
return EMPTY_LIST; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public boolean equals(Object o) { |
| 85 |
if (!(o instanceof ImmutableList)) { |
| 86 |
return false; |
| 87 |
} |
| 88 |
if (o == this) { |
| 89 |
return true; |
| 90 |
} |
| 91 |
Iterator<T> thisIter = this.iterator(); |
| 92 |
Iterator<?> otherIter = ((ImmutableList) o).iterator(); |
| 93 |
while (thisIter.hasNext() && otherIter.hasNext()) { |
| 94 |
if (!thisIter.next().equals(otherIter.next())) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
} |
| 98 |
return thisIter.hasNext() == otherIter.hasNext(); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public Iterator<T> iterator() { |
| 107 |
return new Iterator<T>() { |
| 108 |
private ImmutableList<T> list = ImmutableList.this; |
| 109 |
|
| 110 |
public boolean hasNext() { |
| 111 |
return !list.isEmpty(); |
| 112 |
} |
| 113 |
|
| 114 |
public T next() { |
| 115 |
T element = list.head(); |
| 116 |
list = list.tail(); |
| 117 |
return element; |
| 118 |
} |
| 119 |
}; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
@Override |
| 128 |
public String toString() { |
| 129 |
StringBuilder builder = new StringBuilder("["); |
| 130 |
boolean first = true; |
| 131 |
for (T elem : this) { |
| 132 |
if (!first) { |
| 133 |
builder.append(", "); |
| 134 |
} |
| 135 |
builder.append(elem); |
| 136 |
first = false; |
| 137 |
} |
| 138 |
builder.append(']'); |
| 139 |
return builder.toString(); |
| 140 |
} |
| 141 |
|
| 142 |
private static final EmptyList EMPTY_LIST = new EmptyList(); |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
private static class EmptyList extends ImmutableList { |
| 149 |
@Override |
| 150 |
public Object head() { |
| 151 |
throw new NoSuchElementException("head() called on empty list"); |
| 152 |
} |
| 153 |
|
| 154 |
@Override |
| 155 |
public ImmutableList tail() { |
| 156 |
throw new NoSuchElementException("head() called on empty list"); |
| 157 |
} |
| 158 |
|
| 159 |
@Override |
| 160 |
public boolean isEmpty() { |
| 161 |
return true; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
private static class NonEmptyList<T> extends ImmutableList<T> { |
| 171 |
private final T element; |
| 172 |
private final ImmutableList<T> tail; |
| 173 |
|
| 174 |
private NonEmptyList(final T element, final ImmutableList<T> tail) { |
| 175 |
this.element = element; |
| 176 |
this.tail = tail; |
| 177 |
} |
| 178 |
|
| 179 |
@Override |
| 180 |
public T head() { |
| 181 |
return element; |
| 182 |
} |
| 183 |
|
| 184 |
@Override |
| 185 |
public ImmutableList<T> tail() { |
| 186 |
return tail; |
| 187 |
} |
| 188 |
|
| 189 |
@Override |
| 190 |
public boolean isEmpty() { |
| 191 |
return false; |
| 192 |
} |
| 193 |
} |
| 194 |
} |