View Javadoc

1   /*
2    * #%L
3    * prolobjectlink-jpi-jlog
4    * %%
5    * Copyright (C) 2019 Prolobjectlink Project
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as
9    * published by the Free Software Foundation, either version 3 of the
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Public
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/gpl-3.0.html>.
20   * #L%
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   * @author Jose Zalacain
41   * @since 1.0
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 }