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    /**
016     * Reverse the order of an enumeration.
017     */
018    
019    import java.util.ArrayList;
020    import java.util.Enumeration;
021    import java.util.NoSuchElementException;
022    
023    public final class ReverseEnumerator<T> implements Enumeration<T> {
024    
025      private final ArrayList<T> vec;
026      private int index;
027    
028      public boolean hasMoreElements() {
029        return index > 0;
030      }
031    
032      public T nextElement() {
033        index--;
034        if (index >= 0) {
035          return vec.get(index);
036        } else {
037          throw new NoSuchElementException();
038        }
039      }
040    
041      public ReverseEnumerator(Enumeration<T> e) {
042        vec = new ArrayList<T>();
043        while (e.hasMoreElements()) {
044          vec.add(e.nextElement());
045        }
046        index = vec.size();
047      }
048    }
049    
050    
051