001: /*
002: * @(#)PathIteratorImpl.java
003: *
004: * Copyright (C) 2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Part of the GroboUtils package at:
009: * http://groboutils.sourceforge.net
010: *
011: * Permission is hereby granted, free of charge, to any person obtaining a
012: * copy of this software and associated documentation files (the "Software"),
013: * to deal in the Software without restriction, including without limitation
014: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
015: * and/or sell copies of the Software, and to permit persons to whom the
016: * Software is furnished to do so, subject to the following conditions:
017: *
018: * The above copyright notice and this permission notice shall be included in
019: * all copies or substantial portions of the Software.
020: *
021: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
022: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
024: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
025: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
026: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
027: * DEALINGS IN THE SOFTWARE.
028: */
029: package net.sourceforge.groboutils.mbtf.v1.engine;
030:
031: import java.util.Enumeration;
032: import java.util.NoSuchElementException;
033:
034: import net.sourceforge.groboutils.mbtf.v1.IPathIterator;
035: import net.sourceforge.groboutils.mbtf.v1.ITransition;
036:
037: import org.apache.log4j.Logger;
038:
039: /**
040: * An iterator to allow the tracing of the transitions through a state machine.
041: *
042: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
043: * @version $Date: 2003/02/10 22:52:26 $
044: * @since June 12, 2002
045: */
046: public class PathIteratorImpl implements IPathIterator {
047: private static final Logger LOG = Logger
048: .getLogger(PathIteratorImpl.class);
049:
050: private int index = 0;
051: private ITransition[] trans;
052:
053: public PathIteratorImpl(ITransition[] trans) {
054: if (trans == null) {
055: LOG.debug("no transitions");
056: this .trans = new ITransition[0];
057: } else {
058: int len = trans.length;
059: ITransition[] t = new ITransition[len];
060: for (int i = 0; i < len; ++i) {
061: if (trans[i] == null) {
062: throw new IllegalArgumentException(
063: "no nulls allowed in ITransition array");
064: }
065: // else
066: LOG.debug("transition " + i + "=" + trans[i]);
067: t[i] = trans[i];
068: }
069: this .trans = t;
070: }
071: }
072:
073: public boolean hasMoreElements() {
074: return hasNext();
075: }
076:
077: public Object nextElement() {
078: return nextTransition();
079: }
080:
081: /**
082: * Performs same functionality as <tt>hasMoreElements()</tt>.
083: */
084: public boolean hasNext() {
085: return (this .index < trans.length);
086: }
087:
088: /**
089: * The same as <tt>nextElement()()</tt>, but without the required cast.
090: *
091: * @exception NoSuchElementException if the iterator is at the end.
092: */
093: public synchronized ITransition nextTransition() {
094: if (!hasMoreElements()) {
095: throw new NoSuchElementException("end of list");
096: }
097: ITransition t = this .trans[this .index];
098: ++this .index;
099: LOG.debug("nextTranstion() = " + t);
100: return t;
101: }
102: }
|