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.util;
014    
015    import java.util.ListIterator;
016    import java.util.NoSuchElementException;
017    import org.jikesrvm.VM;
018    
019    public final class LinkedListIteratorRVM<T> implements ListIterator<T> {
020      boolean canRemove = false;
021    
022      /** The list we are iterating over */
023      final LinkedListRVM<T> l;
024    
025      /** Pointer to the current (most recently returned) element. */
026      private LinkedListRVM.Element<T> cursor = null;
027    
028      /**
029       * Constructor
030       *
031       * @param l The list to iterate over.
032       */
033      LinkedListIteratorRVM(LinkedListRVM<T> l) {
034        this.l = l;
035      }
036    
037      public void add(T arg0) {
038        l.insertAfter(cursor, arg0);
039        cursor = cursor.next;
040      }
041    
042      public boolean hasNext() {
043        return cursor != l.tail;
044      }
045    
046      public boolean hasPrevious() {
047        return cursor != l.head;
048      }
049    
050      public T next() {
051        if (cursor == null) {
052          cursor = l.head;
053        } else {
054          if (cursor.next == null) {
055            throw new NoSuchElementException();
056          }
057          cursor = cursor.next;
058        }
059        canRemove = true;
060        return cursor.entry;
061      }
062    
063      public void remove() {
064        if (canRemove) {
065          l.removeInternal(cursor);
066          canRemove = false;
067        } else {
068          throw new IllegalStateException();
069        }
070      }
071    
072      /* ---------------------------------------------------------------------- */
073      /*                      Methods below unimplemented                       */
074      /* ---------------------------------------------------------------------- */
075    
076      public int nextIndex() {
077        if (VM.VerifyAssertions) VM._assert(false);
078        return 0;
079      }
080    
081      public T previous() {
082        if (VM.VerifyAssertions) VM._assert(false);
083        return null;
084      }
085    
086      public int previousIndex() {
087        if (VM.VerifyAssertions) VM._assert(false);
088        return 0;
089      }
090    
091      public void set(Object arg0) {
092        if (VM.VerifyAssertions) VM._assert(false);
093      }
094    
095    }