Debian Codegraph @

net/sf/saxon/ma/parray/ImmList1.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 net.sf.saxon.tree.jiter.MonoIterator;
11
12 import java.util.Iterator;
13
14 /**
15 * Implementation of an immutable list of length 1 (one)
16 * @param <E> the type of the list element
17 */
18
19 public class ImmList1<E> extends ImmList<E> {
20
21 private E member;
22
23 public ImmList1(E member) {
24 this.member = member;
25 }
26
27 @Override
28 public E get(int index) {
29 if (index == 0) {
30 return member;
31 } else {
32 throw outOfBounds(index, 1);
33 }
34 }
35
36 @Override
37 public int size() {
38 return 1;
39 }
40
41 @Override
42 public boolean isEmpty() {
43 return false;
44 }
45
46 @Override
47 public ImmList<E> replace(int index, E member) {
48 if (index == 0) {
49 return new ImmList1<>(member);
50 } else {
51 throw outOfBounds(index, 1);
52 }
53 }
54
55 @Override
56 public ImmList<E> insert(int index, E member) {
57 if (index == 0) {
58 return new ImmList2<>(new ImmList1<>(member), this);
59 } else if (index == 1) {
60 return new ImmList2<>(this, new ImmList1<>(member));
61 } else {
62 throw outOfBounds(index,1);
63 }
64 }
65
66 @Override
67 public ImmList<E> append(E member) {
68 return new ImmList2<>(this, new ImmList1<>(member));
69 }
70
71 @Override
72 public ImmList<E> appendList(ImmList<E> members) {
73 return new ImmList2<>(this, members);
74 }
75
76 @Override
77 public ImmList<E> remove(int index) {
78 if (index == 0) {
79 return ImmList.empty();
80 } else {
81 throw outOfBounds(index, 1);
82 }
83 }
84
85 @Override
86 public ImmList<E> subList(int start, int end) {
87 if (start != 0) {
88 throw outOfBounds(start, 1);
89 }
90 if (end == 0) {
91 return ImmList.empty();
92 } else if (end == 1) {
93 return this;
94 } else {
95 throw outOfBounds(end, 1);
96 }
97 }
98
99 @Override
100 public Iterator<E> iterator() {
101 return new MonoIterator<>(member);
102 }
103 }
104