improving GSDI, under construction...
[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: www.phylosoft.org/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 /*
34  * @author Christian Zmasek
35  */
36 public class ExternalForwardIterator implements PhylogenyNodeIterator {
37
38     private PhylogenyNode       _current_node;
39     private final PhylogenyNode _last_ext_node;
40     private final PhylogenyNode _first_ext_node;
41
42     /**
43      * Constructor for ExternalForwardIterator.
44      * 
45      * @param tree
46      *            the tree on which to iterate over all external nodes.
47      */
48     public ExternalForwardIterator( final Phylogeny phylogeny ) throws IllegalArgumentException {
49         if ( phylogeny.isEmpty() ) {
50             throw new IllegalArgumentException( "attempt to use ExternalForwardIterator on an empty phylogeny" );
51         }
52         PhylogenyNode n = phylogeny.getRoot();
53         while ( !n.isExternal() ) {
54             n = n.getLastChildNode();
55         }
56         _last_ext_node = n;
57         _first_ext_node = phylogeny.getFirstExternalNode();
58         reset();
59     }
60
61     private PhylogenyNode getCurrentNode() {
62         return _current_node;
63     }
64
65     private PhylogenyNode getFirstExtNode() {
66         return _first_ext_node;
67     }
68
69     private PhylogenyNode getLastExtNode() {
70         return _last_ext_node;
71     }
72
73     /*
74      * (non-Javadoc)
75      * 
76      * @see java.util.Iterator#hasNext()
77      */
78     @Override
79     public boolean hasNext() {
80         return getCurrentNode() != null;
81     }
82
83     /*
84      * (non-Javadoc)
85      * 
86      * @see java.util.Iterator#next()
87      */
88     @Override
89     public PhylogenyNode next() throws NoSuchElementException {
90         if ( !hasNext() ) {
91             throw new NoSuchElementException( "attempt to call \"next()\" on iterator which has no more next elements" );
92         }
93         final PhylogenyNode n = getCurrentNode();
94         if ( n == getLastExtNode() ) {
95             setCurrentNode( null );
96         }
97         else {
98             setCurrentNode( n.getNextExternalNode() );
99         }
100         return n;
101     }
102
103     /**
104      * Not supported.
105      * 
106      */
107     @Override
108     public void remove() {
109         throw new UnsupportedOperationException();
110     }
111
112     /**
113      * DOCUMENT ME!
114      */
115     @Override
116     public void reset() {
117         setCurrentNode( getFirstExtNode() );
118     }
119
120     private void setCurrentNode( final PhylogenyNode current_node ) {
121         _current_node = current_node;
122     }
123 } // end of class ExternalForwardIterator