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 */
013package org.jikesrvm.compilers.opt.util;
014
015import java.util.Enumeration;
016import java.util.Iterator;
017
018
019public 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() {
045      return e.hasMoreElements();
046    }
047
048    @Override
049    public GraphNode nextElement() {
050      return e.nextElement();
051    }
052  }
053
054  private static final class Iter extends GraphNodeEnumerator {
055    private final Iterator<GraphNode> i;
056
057    Iter(Iterator<GraphNode> i) {
058      this.i = i;
059    }
060
061    @Override
062    public boolean hasMoreElements() {
063      return i.hasNext();
064    }
065
066    @Override
067    public GraphNode nextElement() {
068      return i.next();
069    }
070  }
071}