2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
26 package org.forester.phylogeny.iterators;
28 import java.util.NoSuchElementException;
30 import org.forester.datastructures.Queue;
31 import org.forester.phylogeny.Phylogeny;
32 import org.forester.phylogeny.PhylogenyNode;
35 * An iterator to iterate a Phylogeny in level order.
37 * Created: 10/23/2005 by Christian M. Zmasek. Last modified: 10/23/2005 by
38 * Christian M. Zmasek.
40 * @author Christian M. Zmasek
44 public class LevelOrderTreeIterator implements PhylogenyNodeIterator {
48 private final Queue _queue;
49 private final PhylogenyNode _root;
54 * Creates a new LevelOrderTreeIterator for iterating over all the nodes of
58 * the Phylogeny to iterate over
59 * @throws IllegalArgumentException
60 * if phylogeny is empty
62 public LevelOrderTreeIterator( final Phylogeny phylogeny ) throws IllegalArgumentException {
63 this( phylogeny.getRoot() );
64 if ( phylogeny.isEmpty() ) {
65 throw new IllegalArgumentException( "Attempt to use LevelOrderTreeIterator on an empty phylogeny." );
70 * Creates a new LevelOrderTreeIterator for iterating over all the child
71 * nodes of PhylogenyNode node (including node itself).
74 * the parent of the nodes to iterate over
76 public LevelOrderTreeIterator( final PhylogenyNode node ) {
85 * Returns the queue upon which this iterator is based.
88 private Queue getQueue() {
93 * Returns the root of the phylogeny this iterators parses over.
95 * @return the root of the phylogeny this iterators parses over.
97 private PhylogenyNode getRoot() {
104 * Returns true is this iterator has at least one more element, false
107 * @return true is this iterator has at least one more element, false
110 public boolean hasNext() {
111 return !getQueue().isEmpty();
115 * Returns the next PhylogenyNode.
117 * @return the next PhylogenyNode
118 * @throws NoSuchElementException
119 * if iteration is complete
121 public PhylogenyNode next() throws NoSuchElementException {
123 throw new NoSuchElementException( "Attempt to call \"next()\" on iterator which has no more next elements." );
125 final PhylogenyNode node = ( PhylogenyNode ) getQueue().dequeue();
126 for( int i = 0; i < node.getNumberOfDescendants(); ++i ) {
127 getQueue().enqueue( node.getChildNode( i ) );
136 public void remove() {
137 throw new UnsupportedOperationException();
141 * Resets the iterator.
143 public void reset() {
145 getQueue().enqueue( getRoot() );
147 } // enod of class LevelOrderTreeIterator