Debian Codegraph @

net/sf/saxon/ma/trie/ImmutableList.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2012 Michael Froh.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.ma.trie;
9
10 import java.util.Iterator;
11 import java.util.NoSuchElementException;
12
13 /**
14 * An immutable list implementation that only supports sequential traversal using an iterator,
15 * prepending an item to the start, and extraction of the head()/tail() of the list. Unlike
16 * {@link net.sf.saxon.ma.parray.ImmList}, it is optimized for sequential access rather than
17 * direct access.
18 * @param <T> the type of the elements in the list
19 */
20
21 public abstract class ImmutableList<T> implements Iterable<T> {
22
23 /**
24 * Get the first item in the list
25 * @return the first item in the list
26 */
27 public abstract T head();
28
29 /**
30 * Get all items in the list other than the first
31 * @return a list containing all items except the first
32 */
33
34 public abstract ImmutableList<T> tail();
35
36 /**
37 * Ask whether the list is empty
38 * @return true if and only if the list contains no items
39 */
40
41 public abstract boolean isEmpty();
42
43 /**
44 * Get the size of the list (the number of items). Note that this is an O(n) operation.
45 * @return the size of the list.
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 * Return a list with a new item added at the start
60 * @param element the item to be added at the start
61 * @return a new list
62 */
63
64 public ImmutableList<T> prepend(T element) {
65 return new NonEmptyList<>(element, this);
66 }
67
68 /**
69 * Return an empty list
70 * @param <T> the nominal item type of the list elements
71 * @return an empty list
72 */
73 public static <T> ImmutableList<T> empty() {
74 return EMPTY_LIST;
75 }
76
77 /**
78 * Test whether two lists are equal
79 * @param o the other list
80 * @return true if the other object is an instance of this ImmutableList class, and the
81 * elements of the two lists are pairwise equal.
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 * Get an iterator over the elements of the list
103 * @return an iterator over the list
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 * Return a string representation of the list contents
124 * @return a string in the form "[item1, item2, ...]"
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 * An empty immutable list
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 * A non-empty immutable list, implemented as a one-way linked list
167 * @param <T> the type of the items in the list
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 }