Debian Codegraph @

net/sf/saxon/ma/parray/ImmList.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
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.parray;
9
10 import java.util.List;
11
12 /**
13 * An immutable list of elements
14 * @param <E> the type of the elements in the list
15 */
16
17 public abstract class ImmList<E> implements Iterable<E> {
18
19 /**
20 * Return an empty list
21 * @param <E> the (nominal) type of the list element
22 * @return an empty immutable list
23 */
24
25 public static <E> ImmList<E> empty() {
26 return (ImmList<E>)ImmList0.INSTANCE;
27 }
28
29 /**
30 * Return a singleton list (a list containing one item)
31 * @param member the single member of the list
32 * @param <E> the type of the list members
33 * @return the singleton list
34 */
35
36 public static <E> ImmList<E> singleton(E member) {
37 return new ImmList1<>(member);
38 }
39
40 /**
41 * Return a list of length 2 (two)
42 *
43 * @param first the first member of the list
44 * @param second the second member of the list
45 * @param <E> the type of the list members
46 * @return the two-member list
47 */
48
49 public static <E> ImmList<E> pair(E first, E second) {
50 return new ImmList2<>(new ImmList1<>(first), new ImmList1<>(second));
51 }
52
53 /**
54 * Construct an immutable list from a Java list of members
55 * @param members the members to be added to the list
56 * @param <E> the type of the list members
57 * @return the immutable list
58 */
59 public static <E> ImmList<E> fromList(List<E> members) {
60 int size = members.size();
61 if (size == 0) {
62 return empty();
63 } else if (size == 1) {
64 return singleton(members.get(0));
65 } else {
66 int split = size/2;
67 List<E> left = members.subList(0, split);
68 List<E> right = members.subList(split, size);
69 return new ImmList2<>(fromList(left), fromList(right));
70 }
71 }
72
73 /**
74 * Get the element at a given index
75 * @param index the required index (zero-based)
76 * @return the element at the given index
77 * @throws IndexOutOfBoundsException if the index is out of range
78 */
79
80 abstract public E get(int index);
81
82 /**
83 * Get the first element in the list
84 * @return the result of get(0)
85 * @throws IndexOutOfBoundsException if the list is empty
86 */
87
88 public E head() {
89 return get(0);
90 }
91
92 /**
93 * Get the size of the list
94 * @return the number of members in the list
95 */
96
97 abstract public int size();
98
99 /**
100 * Ask if the list is empty
101 * @return true if the list contains no elements, otherwise false
102 */
103
104 abstract public boolean isEmpty();
105
106 /**
107 * Replace the element at a given index
108 *
109 * @param index the index (zero-based) of the element to be replaced
110 * @param member the replacement member to be included in the new list
111 * @return a new list, identical to the old except for the replacement of one member
112 * @throws IndexOutOfBoundsException if the index is out of range
113 */
114
115 abstract public ImmList<E> replace(int index, E member);
116
117 /**
118 * Insert an element at a given position
119 *
120 * @param index the position (zero-based) for the insertion. The new element will
121 * be inserted before the existing element at this position. If the index
122 * is equal to the list size, the new element is inserted at the end.
123 * @param member the new member to be included in the new list
124 * @return a new list, identical to the old except for the addition of one member
125 * @throws IndexOutOfBoundsException if the index is out of range
126 */
127
128 abstract public ImmList<E> insert(int index, E member);
129
130 /**
131 * Append an element at the end of the list
132 * @param member the new member to be included in the new list
133 * @return a new list, identical to the old except for the addition of one member
134 */
135
136 abstract public ImmList<E> append(E member);
137
138 /**
139 * Append multiple elements at the end of the list
140 *
141 * @param members the new members to be included in the new list
142 * @return a new list, identical to the old except for the addition of new members
143 */
144
145 abstract public ImmList<E> appendList(ImmList<E> members);
146
147 /**
148 * Remove the member at a given position
149 * @param index the zero-based index position of the member to be removed
150 * @return a new list, identical to the old except for the removal of one member
151 * @throws IndexOutOfBoundsException if the index is out of range
152 */
153
154 abstract public ImmList<E> remove(int index);
155
156 /**
157 * Return a sub-sequence with a given start and end position
158 * @param start the zero-based index position of the first member to be extracted
159 * @param end the zero-based index position of the first member after the sub-sequence
160 * to be extracted
161 * @return a new list containing the elements from the specified range of positions
162 * @throws IndexOutOfBoundsException if either index is out of range or if end precedes start
163 */
164
165 abstract public ImmList<E> subList(int start, int end);
166
167 /**
168 * Get a list containing all elements of the list except the first
169 * @return the result of {@code subList(1, size())}, or equivalently {@code remove(0)}
170 * @throws IndexOutOfBoundsException if the list is empty
171 */
172
173 public ImmList<E> tail() {
174 return remove(0);
175 }
176
177 /**
178 * Return a list containing the same elements as this list, but optimized for efficient access
179 * @return either this list, or a copy containing the same elements in the same order
180 */
181
182 protected ImmList<E> rebalance() {
183 return this;
184 }
185
186 /**
187 * Convenience method for use by subclasses to throw an IndexOutOfBounds exception when needed
188 * @param requested the index value that was requested by the caller
189 * @param actual the actual size of the list
190 * @return an {@code IndexOutOfBoundsException} with suitable message text
191 */
192
193 protected IndexOutOfBoundsException outOfBounds(int requested, int actual) {
194 return new IndexOutOfBoundsException("Requested " + requested + ", actual size " + actual);
195 }
196
197 }
198