clean up
[jalview.git] / forester / java / src / org / forester / phylogeny / iterators / PreorderTreeIterator.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 import java.util.Stack;
30
31 import org.forester.phylogeny.Phylogeny;
32 import org.forester.phylogeny.PhylogenyNode;
33
34 // import java.util.Iterator; TODO should implement this, not some iterator of
35 // this package.
36 /*
37  * @author Christian M. Zmasek
38  * 
39  * @version 1.020 -- last modified: 10/10/05
40  */
41 public class PreorderTreeIterator implements PhylogenyNodeIterator {
42
43     final private Phylogeny            _tree;
44     final private Stack<PhylogenyNode> _stack;
45
46     /**
47      * @param tree
48      *            Phylogeny for which a Iterator is to be constructed.
49      */
50     public PreorderTreeIterator( final Phylogeny tree ) throws IllegalArgumentException {
51         if ( tree.isEmpty() ) {
52             throw new IllegalArgumentException( "Attempt to use PreorderTreeIterator on empty tree." );
53         }
54         _stack = new Stack<PhylogenyNode>();
55         _tree = tree;
56         reset();
57     }
58
59     public PreorderTreeIterator( final PhylogenyNode node ) throws IllegalArgumentException {
60         _stack = new Stack<PhylogenyNode>();
61         _tree = null;
62         reset( node );
63     }
64
65     private Stack<PhylogenyNode> getStack() {
66         return _stack;
67     }
68
69     private Phylogeny getTree() {
70         return _tree;
71     }
72
73     /*
74      * (non-Javadoc)
75      * 
76      * @see java.util.Iterator#hasNext()
77      */
78     @Override
79     public boolean hasNext() {
80         return !getStack().isEmpty();
81     }
82
83     /**
84      * Advances the Iterator by one.
85      */
86     @Override
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 node = getStack().pop();
92         if ( !node.isExternal() ) {
93             for( int i = node.getNumberOfDescendants() - 1; i >= 0; --i ) {
94                 getStack().push( node.getChildNode( i ) );
95             }
96         }
97         return node;
98     } // next()
99
100     /**
101      * Not supported.
102      * 
103      */
104     @Override
105     public void remove() {
106         throw new UnsupportedOperationException();
107     }
108
109     @Override
110     public void reset() {
111         getStack().clear();
112         getStack().push( getTree().getRoot() );
113     }
114
115     private void reset( final PhylogenyNode node ) {
116         getStack().clear();
117         getStack().push( node );
118     }
119 } // End of class PreorderTreeIterator.