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.phylogeny.Phylogeny;
31 import org.forester.phylogeny.PhylogenyNode;
34 * @author Christian Zmasek
36 public class ExternalForwardIterator implements PhylogenyNodeIterator {
38 private PhylogenyNode _current_node;
39 private final PhylogenyNode _last_ext_node;
40 private final PhylogenyNode _first_ext_node;
43 * Constructor for ExternalForwardIterator.
46 * the tree on which to iterate over all external nodes.
48 public ExternalForwardIterator( final Phylogeny phylogeny ) throws IllegalArgumentException {
49 if ( phylogeny.isEmpty() ) {
50 throw new IllegalArgumentException( "Attempt to use ExternalForwardIterator on an empty phylogeny." );
52 PhylogenyNode n = phylogeny.getRoot();
53 while ( !n.isExternal() ) {
54 n = n.getLastChildNode();
57 _first_ext_node = phylogeny.getFirstExternalNode();
61 private PhylogenyNode getCurrentNode() {
65 private PhylogenyNode getFirstExtNode() {
66 return _first_ext_node;
69 private PhylogenyNode getLastExtNode() {
70 return _last_ext_node;
76 * @see java.util.Iterator#hasNext()
79 public boolean hasNext() {
80 return getCurrentNode() != null;
86 * @see java.util.Iterator#next()
89 public PhylogenyNode next() throws NoSuchElementException {
91 throw new NoSuchElementException( "Attempt to call \"next()\" on iterator which has no more next elements." );
93 final PhylogenyNode n = getCurrentNode();
94 if ( n == getLastExtNode() ) {
95 setCurrentNode( null );
98 setCurrentNode( n.getNextExternalNode() );
108 public void remove() {
109 throw new UnsupportedOperationException();
116 public void reset() {
117 setCurrentNode( getFirstExtNode() );
120 private void setCurrentNode( final PhylogenyNode current_node ) {
121 _current_node = current_node;
123 } // end of class ExternalForwardIterator