1 19 package org.netbeans.modules.java.source.builder; 20 21 import java.util.*; 22 23 27 public class BufferRunQueue implements Iterable <BufferRun> { 28 private ArrayList<BufferRun> runs = new ArrayList<BufferRun>(); 29 30 31 private SortedMap<Integer , Integer > positionMap = 32 new TreeMap<Integer , Integer >(); 33 private SortedMap<Integer , Integer > endPositionMap = 34 new TreeMap<Integer , Integer >(); 35 36 void add(BufferRun br) { 37 runs.add(br); 38 int lastRun = runs.size() - 1; 39 positionMap.put(br.start, lastRun); 40 endPositionMap.put(br.end, lastRun); 41 } 42 43 void addCoallescing(BufferRun br) { 44 if (br.kind == BufferRun.Kind.WHITESPACE) { 45 int sz = runs.size(); 46 if (sz > 0) { 47 BufferRun last = runs.get(sz - 1); 48 if (last.kind == BufferRun.Kind.WHITESPACE && last.end == br.start) { 49 last.end = br.end; 50 return; 51 } 52 } 53 } 54 runs.add(br); 55 } 56 57 public Iterator<BufferRun> iterator() { 58 return new Iterator<BufferRun>() { 59 int next = 0; 60 public boolean hasNext() { 61 return next < runs.size(); 62 } 63 public BufferRun next() { 64 return get(next++); 65 } 66 public void remove() { 67 throw new UnsupportedOperationException (); 68 } 69 }; 70 } 71 72 74 public int size() { 75 return runs.size(); 76 } 77 78 public BufferRun get(int i) { 79 return runs.get(i); 80 } 81 82 public int findRunStartingAt(int start) { 83 Integer index = positionMap.get(start); 84 return index == null ? -1 : index; 85 } 86 87 public int findRunEndingWith(int end) { 88 Integer index = endPositionMap.get(end); 89 return index == null ? -1 : index; 90 } 91 92 public BufferRun[] toArray() { 93 return runs.toArray(new BufferRun[0]); 94 } 95 96 98 private int next; 99 100 public void reset() { next = 0; } 101 102 public int getPos() { return next; } 103 104 public boolean hasNext() { return next < runs.size(); } 105 106 public BufferRun peekNext() { 107 return next < runs.size() ? runs.get(next) : null; 108 } 109 110 public BufferRun getNext() { 111 return next < runs.size() ? runs.get(next++) : null; 112 } 113 114 public BufferRun getNextBefore(int offset) { 115 if (next < runs.size()) { 116 BufferRun br = runs.get(next); 117 if (br.getEnd() <= offset) { 118 next++; 119 return br; 120 } 121 } 122 return null; 123 } 124 125 public void unget() { 126 if (next > 0) 127 next--; 128 } 129 } 130 | Popular Tags |