f75e6f77ae0e19ecf8d8839d0c8587fbd1065a21
[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     public boolean hasNext() {
79         return getCurrentNode() != null;
80     }
81
82     /*
83      * (non-Javadoc)
84      * 
85      * @see java.util.Iterator#next()
86      */
87     public PhylogenyNode next() throws NoSuchElementException {
88         if ( !hasNext() ) {
89             throw new NoSuchElementException( "Attempt to call \"next()\" on iterator which has no more next elements." );
90         }
91         final PhylogenyNode n = getCurrentNode();
92         if ( n == getLastExtNode() ) {
93             setCurrentNode( null );
94         }
95         else {
96             setCurrentNode( n.getNextExternalNode() );
97         }
98         return n;
99     }
100
101     /**
102      * Not supported.
103      * 
104      */
105     public void remove() {
106         throw new UnsupportedOperationException();
107     }
108
109     /**
110      * DOCUMENT ME!
111      */
112     public void reset() {
113         setCurrentNode( getFirstExtNode() );
114     }
115
116     private void setCurrentNode( final PhylogenyNode current_node ) {
117         _current_node = current_node;
118     }
119 } // end of class ExternalForwardIterator