001: /*
002: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package org.mandarax.util;
019:
020: import org.mandarax.kernel.Term;
021:
022: /**
023: * Iterator for terms. The iterator provides a simple mean for
024: * iterating but avoiding casting as it is the case when using
025: * the <code>java.util.Enumeration</code> or <code>java.util.Iterator</code>
026: * interface. But both interfaces are also supported.
027: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
028: * @version 3.4 <7 March 05>
029: * @since 1.0
030: */
031: public final class TermIterator implements java.util.Enumeration,
032: java.util.Iterator {
033:
034: private Term[] terms = null;
035: private int cursor = 0;
036:
037: /**
038: * Constructor.
039: * @param t an array of terms
040: */
041: public TermIterator(Term[] t) {
042: super ();
043:
044: terms = t;
045: }
046:
047: /**
048: * Constructor. Pass the initial cursor.
049: * @param t an array of terms
050: * @param c the initial value of the cursor
051: */
052: private TermIterator(Term[] t, int c) {
053: super ();
054:
055: terms = t;
056: cursor = c;
057: }
058:
059: /**
060: * Indicates whether there are more elements.
061: * Duplicates the functionality of <code>hasMoreTerms()</code> in order to implement
062: * the <code>java.util.Enumeration</code> interface.
063: * @return true if there are more elements, false otherwise
064: */
065: public boolean hasMoreElements() {
066: return hasMoreTerms();
067: }
068:
069: /**
070: * Indicates whether there are more terms.
071: * @return true if there are more elements, false otherwise
072: */
073: public boolean hasMoreTerms() {
074: return cursor < terms.length;
075: }
076:
077: /**
078: * Indicates whether there are more elements.
079: * Duplicates the functionality of <code>hasMoreTerms()</code> in order to implement
080: * the <code>java.util.Iterator</code> interface.
081: * @return true if there are more elements, false otherwise
082: */
083: public boolean hasNext() {
084: return hasMoreTerms();
085: }
086:
087: /**
088: * Get the next element.
089: * Duplicates the functionality of <code>nextTerm()</code> in order to implement
090: * the <code>java.util.Iterator</code> interface.
091: * @return the next term
092: */
093: public Object next() {
094: return nextTerm();
095: }
096:
097: /**
098: * Get the next term.
099: * Duplicates the functionality of <code>nextTerm()</code> in order to implement
100: * the <code>java.util.Enumeration</code> interface.
101: * @return the next term
102: */
103: public Object nextElement() {
104: return nextTerm();
105: }
106:
107: /**
108: * Get the next term.
109: * @return the next term
110: */
111: public Term nextTerm() {
112: int i = cursor;
113:
114: cursor = cursor + 1;
115:
116: return terms[i];
117: }
118:
119: /**
120: * Remove an element. Not supported, throws a <code>UnsupportedOperationException</code>.
121: */
122: public void remove() {
123: throw new UnsupportedOperationException();
124: }
125: }
|