1 19 package org.netbeans.modules.javacore; 20 21 import java.util.AbstractSequentialList ; 22 import java.util.Iterator ; 23 import java.util.ListIterator ; 24 import java.util.NoSuchElementException ; 25 26 30 public class LazyImmutableList extends AbstractSequentialList { 31 private LazyIterator internal; 32 private boolean complete = false; 33 private final Object [] array; 34 private int nextIndex = 0; 35 36 37 public LazyImmutableList(LazyIterator internalIterator) { 38 internal = internalIterator; 39 array = new Object [internalIterator.maxEstimatedSize()]; 40 } 41 42 public int size() { 43 while (internal != null) { 44 nextItem(); 45 } 46 return nextIndex; 47 } 48 49 public ListIterator listIterator(int index) { 50 return new LazyImmutableIterator(index); 51 } 52 53 public int maxEstimatedSize() { 54 return internal == null ? nextIndex : internal.maxEstimatedSize(); 55 } 56 57 private void nextItem() { 58 if (internal != null && internal.hasNext()) { 59 array[nextIndex] = internal.next(); 60 nextIndex++; 61 } else { 62 internal = null; 63 } 64 } 65 66 private class LazyImmutableIterator implements ListIterator { 67 private int currentIndex; 68 69 public LazyImmutableIterator(int index) { 70 currentIndex = index; 71 } 72 73 public boolean hasNext() { 74 while (nextIndex <= currentIndex && internal != null) { 75 nextItem(); 76 } 77 return nextIndex > currentIndex; 78 } 79 80 public int previousIndex() { 81 return currentIndex - 1; 82 } 83 84 public Object previous() { 85 if (hasPrevious()) { 86 currentIndex--; 87 return array[currentIndex]; 88 } 89 throw new NoSuchElementException (); 90 } 91 92 public boolean hasPrevious() { 93 return currentIndex > 0; 94 } 95 96 public Object next() { 97 if (hasNext()) { 98 currentIndex++; 99 return array[currentIndex - 1]; 100 } 101 throw new NoSuchElementException (); 102 } 103 104 public int nextIndex() { 105 return currentIndex; 106 } 107 108 public void add(Object o) { 109 throw new UnsupportedOperationException (); 110 } 111 112 public void set(Object o) { 113 throw new UnsupportedOperationException (); 114 } 115 116 public void remove() { 117 throw new UnsupportedOperationException (); 118 } 119 } 120 121 public static abstract class LazyIterator implements Iterator { 122 public final void remove() { 123 throw new UnsupportedOperationException (); 124 } 125 126 protected abstract int maxEstimatedSize(); 127 } 128 } 129 | Popular Tags |