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.jlog;
23
24 import static io.github.prolobjectlink.prolog.PrologTermType.LIST_TYPE;
25
26 import java.util.Iterator;
27 import java.util.NoSuchElementException;
28
29 import io.github.prolobjectlink.prolog.AbstractIterator;
30 import io.github.prolobjectlink.prolog.PrologList;
31 import io.github.prolobjectlink.prolog.PrologProvider;
32 import io.github.prolobjectlink.prolog.PrologTerm;
33 import ubc.cs.JLog.Terms.jList;
34 import ubc.cs.JLog.Terms.jListPair;
35 import ubc.cs.JLog.Terms.jNullList;
36 import ubc.cs.JLog.Terms.jTerm;
37
38
39
40
41
42
43 class JLogList extends JLogTerm implements PrologList {
44
45 protected JLogList(PrologProvider provider) {
46 super(LIST_TYPE, provider, jNullList.NULL_LIST);
47 }
48
49 protected JLogList(PrologProvider provider, PrologTerm[] arguments) {
50 super(LIST_TYPE, provider);
51 value = adaptList(arguments);
52 }
53
54 protected JLogList(PrologProvider provider, jTerm[] arguments) {
55 super(LIST_TYPE, provider);
56 value = jNullList.NULL_LIST;
57 for (int i = arguments.length - 1; i >= 0; --i) {
58 value = new jListPair(arguments[i], value);
59 }
60 }
61
62 protected JLogList(PrologProvider provider, PrologTerm[] arguments, PrologTerm tail) {
63 super(LIST_TYPE, provider);
64 value = fromTerm(tail, jTerm.class);
65 for (int i = arguments.length - 1; i >= 0; --i) {
66 value = new jListPair(fromTerm(arguments[i], jTerm.class), value);
67 }
68 }
69
70 protected JLogList(PrologProvider provider, PrologTerm head, PrologTerm tail) {
71 super(LIST_TYPE, provider);
72 value = new jListPair(fromTerm(head, jTerm.class), fromTerm(tail, jTerm.class));
73 }
74
75 public int size() {
76 int size = 0;
77 jList list = ((jList) value);
78 Iterator<?> i = new JLogIterator(list);
79 while (i.hasNext()) {
80 i.next();
81 size++;
82 }
83 return size;
84 }
85
86 public void clear() {
87 value = jNullList.NULL_LIST;
88 }
89
90 public boolean isEmpty() {
91 return size() == 0;
92 }
93
94 public Iterator<PrologTerm> iterator() {
95 jListPair list = (jListPair) value;
96 return new JLogListIter(list);
97 }
98
99 public PrologTerm getHead() {
100 jListPair list = (jListPair) value;
101 return toTerm(list.getHead(), PrologTerm.class);
102 }
103
104 public PrologTerm getTail() {
105 jListPair list = (jListPair) value;
106 return toTerm(list.getTail(), PrologTerm.class);
107 }
108
109 public int getArity() {
110 return 2;
111 }
112
113 public String getFunctor() {
114 jListPair list = (jListPair) value;
115 return list.getName();
116 }
117
118 public PrologTerm[] getArguments() {
119 int index = 0;
120 PrologTerm[] arguments = new PrologTerm[size()];
121 Iterator<PrologTerm> i = iterator();
122 while (i.hasNext()) {
123 arguments[index++] = i.next();
124 }
125 return arguments;
126 }
127
128 private final class JLogListIter extends AbstractIterator<PrologTerm> implements Iterator<PrologTerm> {
129
130 private final Iterator<? extends jTerm> e;
131
132 private JLogListIter(jListPair list) {
133 e = new JLogIterator(list);
134 }
135
136 public boolean hasNext() {
137 return e.hasNext();
138 }
139
140 public PrologTerm next() {
141 if (!hasNext()) {
142 throw new NoSuchElementException();
143 }
144 return toTerm(e.next(), PrologTerm.class);
145 }
146
147 }
148
149 }