initial commit
[jalview.git] / forester / java / src / org / forester / phylogeny / iterators / ChildNodeIteratorForward.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.PhylogenyNode;
31
32 /*
33  * An iterator to forward iterate over child nodes of a PhylogenyNode. Created:
34  * 10/23/2005 by Christian M. Zmasek. Last modified: 12/28/2006 by Christian M.
35  * Zmasek.
36  * 
37  * @author Christian M. Zmasek
38  * 
39  * @version 1.000
40  */
41 public class ChildNodeIteratorForward implements PhylogenyNodeIterator {
42
43     // Instance variables
44     // ------------------
45     private int                 _i;
46     final private PhylogenyNode _node;
47
48     // Constructor
49     // -----------
50     /**
51      * Creates a new ChildNodeIteratorForward.
52      * 
53      * @param node
54      *            the parent of the PhylogenyNodes to iterate over.
55      * @throws IllegalArgumentException
56      *             if node has no child nodes
57      */
58     public ChildNodeIteratorForward( final PhylogenyNode node ) throws IllegalArgumentException {
59         if ( node.getNumberOfDescendants() < 1 ) {
60             throw new IllegalArgumentException( "Attempt to use ChildNodeIteratorForward on node with no child nodes." );
61         }
62         _node = node;
63         reset();
64     }
65
66     // Private methods
67     // ---------------
68     /**
69      * Returns the counter.
70      */
71     private int getI() {
72         return _i;
73     }
74
75     /**
76      * Returns the parent of the nodes to iterate over.
77      * 
78      * @return the parent of the nodes to iterate over.
79      */
80     private PhylogenyNode getNode() {
81         return _node;
82     }
83
84     // Public methods
85     // --------------
86     /**
87      * Returns true is this iterator has at least one more element, false
88      * otherwise.
89      * 
90      * @return true is this iterator has at least one more element, false
91      *         otherwise
92      */
93     public boolean hasNext() {
94         return ( getI() < getNode().getNumberOfDescendants() );
95     }
96
97     /**
98      * Increases the counter by one.
99      */
100     private void increaseI() {
101         ++_i;
102     }
103
104     /**
105      * Returns the next PhylogenyNode.
106      * 
107      * @return the next PhylogenyNode
108      * @throws NoSuchElementException
109      *             if iteration is complete
110      */
111     public PhylogenyNode next() throws NoSuchElementException {
112         if ( !hasNext() ) {
113             throw new NoSuchElementException( "Attempt to call \"next()\" on iterator which has no more next elements." );
114         }
115         final PhylogenyNode n = getNode().getChildNode( getI() );
116         increaseI();
117         return n;
118     }
119
120     /**
121      * Not supported.
122      * 
123      */
124     public void remove() {
125         throw new UnsupportedOperationException();
126     }
127
128     /**
129      * Resets the iterator.
130      */
131     public void reset() {
132         setI( 0 );
133     }
134
135     /**
136      * Sets the counter.
137      */
138     private void setI( final int i ) {
139         _i = i;
140     }
141 } // end of class ChildNodeIteratorForward.