001    /*
002     *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003     *
004     *  This file is licensed to You under the Eclipse Public License (EPL);
005     *  You may not use this file except in compliance with the License. You
006     *  may obtain a copy of the License at
007     *
008     *      http://www.opensource.org/licenses/eclipse-1.0.php
009     *
010     *  See the COPYRIGHT.txt file distributed with this work for information
011     *  regarding copyright ownership.
012     */
013    package org.jikesrvm.compilers.opt.util;
014    
015    import java.util.Enumeration;
016    import java.util.Iterator;
017    
018    
019    public abstract class GraphNodeEnumerator implements Enumeration<GraphNode> {
020    
021      @Override
022      public abstract GraphNode nextElement();
023    
024      public static GraphNodeEnumerator create(Enumeration<GraphNode> e) {
025        return new Enum(e);
026      }
027    
028      public static GraphNodeEnumerator create(Iterator<GraphNode> i) {
029        return new Iter(i);
030      }
031    
032      public static GraphNodeEnumerator create(Iterable<GraphNode> i) {
033        return new Iter(i.iterator());
034      }
035    
036      private static final class Enum extends GraphNodeEnumerator {
037        private final Enumeration<GraphNode> e;
038    
039        Enum(Enumeration<GraphNode> e) {
040          this.e = e;
041        }
042    
043        @Override
044        public boolean hasMoreElements() { return e.hasMoreElements(); }
045    
046        @Override
047        public GraphNode nextElement() { return e.nextElement(); }
048      }
049    
050      private static final class Iter extends GraphNodeEnumerator {
051        private final Iterator<GraphNode> i;
052    
053        Iter(Iterator<GraphNode> i) {
054          this.i = i;
055        }
056    
057        @Override
058        public boolean hasMoreElements() { return i.hasNext(); }
059    
060        @Override
061        public GraphNode nextElement() { return i.next(); }
062      }
063    }