1 19 package org.netbeans.mdr.util; 20 21 import java.util.*; 22 23 28 public class ComposedLazyList extends AbstractList { 29 private final List lists = new ArrayList(); 30 private int elementCount = 0; 31 32 33 public ComposedLazyList() { 34 super(); 35 } 36 37 public ComposedLazyList(Collection col) { 38 this(); 39 addAll(col); 40 } 41 42 public boolean addAll(Collection col) { 43 List result = (col instanceof List) ? (List) col : new ArrayList(col); 44 45 this.modCount++; 46 47 elementCount += result.size(); 48 return lists.add(result); 49 } 50 51 public int size() { 52 return elementCount; 53 } 54 55 public Object get(int index) { 56 int listUBound = 0; 57 int listLBound = 0; 58 List currentList = null; 59 60 for (Iterator it = lists.iterator(); it.hasNext() && listUBound <= index; listUBound += currentList.size()) { 61 currentList = (List) it.next(); 62 listLBound = listUBound; 63 } 64 65 return currentList.get(index - listLBound); 66 } 67 68 public Iterator iterator() { 69 return new LazyIterator(); 70 } 71 72 private class LazyIterator implements Iterator { 73 Iterator listIterator; 74 Iterator currentIterator; 75 76 private LazyIterator() { 77 listIterator = ComposedLazyList.this.lists.iterator(); 78 currentIterator = null; 79 } 80 81 public void remove() { 82 throw new UnsupportedOperationException (); 83 } 84 85 public Object next() { 86 hasNext(); 87 return currentIterator.next(); 88 } 89 90 public boolean hasNext() { 91 if (currentIterator == null) { 92 if (listIterator.hasNext()) { 93 currentIterator = ((List) listIterator.next()).iterator(); 94 } else { 95 return false; 96 } 97 } 98 99 while (!currentIterator.hasNext()) { 100 if (listIterator.hasNext()) { 101 currentIterator = ((List) listIterator.next()).iterator(); 102 } else { 103 return false; 104 } 105 } 106 return true; 107 } 108 } 109 } 110 | Popular Tags |