1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package io.github.prolobjectlink.prolog.jtrolog;
23
24 import static io.github.prolobjectlink.prolog.PrologTermType.LIST_TYPE;
25
26 import java.util.Iterator;
27
28 import io.github.prolobjectlink.prolog.AbstractIterator;
29 import io.github.prolobjectlink.prolog.PrologList;
30 import io.github.prolobjectlink.prolog.PrologProvider;
31 import io.github.prolobjectlink.prolog.PrologTerm;
32 import jTrolog.terms.Struct;
33 import jTrolog.terms.Term;
34
35
36
37
38
39
40 class JTrologList extends JTrologTerm implements PrologList {
41
42 protected JTrologList(PrologProvider provider) {
43 super(LIST_TYPE, provider, Term.emptyList);
44 }
45
46 protected JTrologList(PrologProvider provider, Term[] arguments) {
47 super(LIST_TYPE, provider);
48 if (arguments != null) {
49 int length = arguments.length;
50 if (length > 0) {
51 if (arguments[length - 1].equals(Term.emptyList)) {
52 value = arguments[length - 1];
53 for (int i = length - 2; i >= 0; --i) {
54 value = new Struct(".", new Term[] { arguments[i], value });
55 }
56 } else {
57 value = Term.emptyList;
58 for (int i = length - 1; i >= 0; --i) {
59 value = new Struct(".", new Term[] { arguments[i], value });
60 }
61 }
62 } else {
63 value = Term.emptyList;
64 }
65 } else {
66 value = Term.emptyList;
67 }
68 }
69
70 protected JTrologList(PrologProvider provider, PrologTerm[] arguments) {
71 super(LIST_TYPE, provider);
72 if (arguments != null) {
73 int length = arguments.length;
74 if (length > 0) {
75 if (arguments[length - 1].isEmptyList()) {
76 value = ((JTrologTerm) arguments[length - 1]).value;
77 for (int i = length - 2; i >= 0; --i) {
78 value = new Struct(".", new Term[] { ((JTrologTerm) arguments[i]).value, value });
79 }
80 } else {
81 value = Term.emptyList;
82 for (int i = length - 1; i >= 0; --i) {
83 value = new Struct(".", new Term[] { ((JTrologTerm) arguments[i]).value, value });
84 }
85 }
86 } else {
87 value = Term.emptyList;
88 }
89 } else {
90 value = Term.emptyList;
91 }
92 }
93
94 protected JTrologList(PrologProvider provider, PrologTerm head, PrologTerm tail) {
95 super(LIST_TYPE, provider);
96 Term h = ((JTrologTerm) head).value;
97 Term t = ((JTrologTerm) tail).value;
98 value = new Struct(".", new Term[] { h, t });
99 }
100
101 protected JTrologList(PrologProvider provider, PrologTerm[] arguments, PrologTerm tail) {
102 super(LIST_TYPE, provider);
103 value = ((JTrologTerm) tail).value;
104 for (int i = arguments.length - 1; i >= 0; --i) {
105 value = new Struct(".", new Term[] { ((JTrologTerm) arguments[i]).value, value });
106 }
107 }
108
109 public int size() {
110 int counter = 0;
111 Iterator<?> i = iterator();
112 while (i.hasNext()) {
113 counter++;
114 i.next();
115 }
116 return counter;
117 }
118
119 public void clear() {
120 value = Term.emptyList;
121 }
122
123 public boolean isEmpty() {
124 return value == Term.emptyList;
125 }
126
127 public Iterator<PrologTerm> iterator() {
128 Struct list = (Struct) value;
129 return new JTrologListIter(list);
130 }
131
132 public PrologTerm getHead() {
133 Term head = ((Struct) value).getArg(0);
134 return toTerm(head, PrologTerm.class);
135 }
136
137 public PrologTerm getTail() {
138 Term tail = ((Struct) value).getArg(1);
139 return toTerm(tail, PrologTerm.class);
140 }
141
142 public int getArity() {
143 return ((Struct) value).arity;
144 }
145
146 public String getFunctor() {
147 return ((Struct) value).name;
148 }
149
150 public PrologTerm[] getArguments() {
151 PrologTerm[] a = new PrologTerm[size()];
152 Iterator<PrologTerm> i = iterator();
153 for (int index = 0; i.hasNext(); index++) {
154 a[index] = i.next();
155 }
156 return a;
157 }
158
159 private class JTrologListIter extends AbstractIterator<PrologTerm> implements Iterator<PrologTerm> {
160
161 private PrologTerm next;
162 private final Iterator<?> i;
163
164 private JTrologListIter(Struct list) {
165 i = Struct.iterator(list);
166 if (i.hasNext()) {
167 next = toTerm(i.next(), PrologTerm.class);
168 }
169 }
170
171 public boolean hasNext() {
172 return next != null && !next.isEmptyList();
173 }
174
175 public PrologTerm next() {
176 PrologTerm lastReturned = next;
177 next = toTerm(i.next(), PrologTerm.class);
178 return lastReturned;
179 }
180
181 }
182
183 }