35a02aae3be815df80d6988d7d6ff2ff2f6aef13
[jalview.git] / forester / java / src / org / forester / phylogeny / iterators / ExternalForwardIterator.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // All rights reserved
8 //
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.
13 //
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.
18 //
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
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
25
26 package org.forester.phylogeny.iterators;
27
28 import java.util.NoSuchElementException;
29
30 import org.forester.phylogeny.Phylogeny;
31 import org.forester.phylogeny.PhylogenyNode;
32
33 public class ExternalForwardIterator implements PhylogenyNodeIterator {
34
35     private PhylogenyNode       _current_node;
36     private final PhylogenyNode _last_ext_node;
37     private final PhylogenyNode _first_ext_node;
38
39     /**
40      * Constructor for ExternalForwardIterator.
41      * 
42      * @param tree
43      *            the tree on which to iterate over all external nodes.
44      */
45     public ExternalForwardIterator( final Phylogeny phylogeny ) throws IllegalArgumentException {
46         if ( phylogeny.isEmpty() ) {
47             throw new IllegalArgumentException( "attempt to use ExternalForwardIterator on an empty phylogeny" );
48         }
49         PhylogenyNode n = phylogeny.getRoot();
50         while ( !n.isExternal() ) {
51             n = n.getLastChildNode();
52         }
53         _last_ext_node = n;
54         _first_ext_node = phylogeny.getFirstExternalNode();
55         reset();
56     }
57
58     private PhylogenyNode getCurrentNode() {
59         return _current_node;
60     }
61
62     private PhylogenyNode getFirstExtNode() {
63         return _first_ext_node;
64     }
65
66     private PhylogenyNode getLastExtNode() {
67         return _last_ext_node;
68     }
69
70     /*
71      * (non-Javadoc)
72      * 
73      * @see java.util.Iterator#hasNext()
74      */
75     @Override
76     public boolean hasNext() {
77         return getCurrentNode() != null;
78     }
79
80     /*
81      * (non-Javadoc)
82      * 
83      * @see java.util.Iterator#next()
84      */
85     @Override
86     public PhylogenyNode next() throws NoSuchElementException {
87         if ( !hasNext() ) {
88             throw new NoSuchElementException( "attempt to call \"next()\" on iterator which has no more next elements" );
89         }
90         final PhylogenyNode n = getCurrentNode();
91         if ( n == getLastExtNode() ) {
92             setCurrentNode( null );
93         }
94         else {
95             setCurrentNode( n.getNextExternalNode() );
96         }
97         return n;
98     }
99
100     /**
101      * Not supported.
102      * 
103      */
104     @Override
105     public void remove() {
106         throw new UnsupportedOperationException();
107     }
108
109     /**
110      * DOCUMENT ME!
111      */
112     @Override
113     public void reset() {
114         setCurrentNode( getFirstExtNode() );
115     }
116
117     private void setCurrentNode( final PhylogenyNode current_node ) {
118         _current_node = current_node;
119     }
120 } // end of class ExternalForwardIterator