working on issue of collapsed nodes in circular display
[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     @Override
94     public boolean hasNext() {
95         return ( getI() < getNode().getNumberOfDescendants() );
96     }
97
98     /**
99      * Increases the counter by one.
100      */
101     private void increaseI() {
102         ++_i;
103     }
104
105     /**
106      * Returns the next PhylogenyNode.
107      * 
108      * @return the next PhylogenyNode
109      * @throws NoSuchElementException
110      *             if iteration is complete
111      */
112     @Override
113     public PhylogenyNode next() throws NoSuchElementException {
114         if ( !hasNext() ) {
115             throw new NoSuchElementException( "Attempt to call \"next()\" on iterator which has no more next elements." );
116         }
117         final PhylogenyNode n = getNode().getChildNode( getI() );
118         increaseI();
119         return n;
120     }
121
122     /**
123      * Not supported.
124      * 
125      */
126     @Override
127     public void remove() {
128         throw new UnsupportedOperationException();
129     }
130
131     /**
132      * Resets the iterator.
133      */
134     @Override
135     public void reset() {
136         setI( 0 );
137     }
138
139     /**
140      * Sets the counter.
141      */
142     private void setI( final int i ) {
143         _i = i;
144     }
145 } // end of class ChildNodeIteratorForward.