KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > builder > BufferRunQueue


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.java.source.builder;
20
21 import java.util.*;
22
23 /**
24  * A queue of sequentially ordered BufferRuns. The individual runs may be coallesced when added, and
25  * a number of retreval techniques are made available.
26  */

27 public class BufferRunQueue implements Iterable JavaDoc<BufferRun> {
28     private ArrayList<BufferRun> runs = new ArrayList<BufferRun>();
29
30     /** Map source offsets to queue indexes. */
31     private SortedMap<Integer JavaDoc, Integer JavaDoc> positionMap =
32         new TreeMap<Integer JavaDoc, Integer JavaDoc>();
33     private SortedMap<Integer JavaDoc, Integer JavaDoc> endPositionMap =
34         new TreeMap<Integer JavaDoc, Integer JavaDoc>();
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 JavaDoc();
68             }
69         };
70     }
71
72     // array oriented accessors
73

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 JavaDoc index = positionMap.get(start);
84         return index == null ? -1 : index;
85     }
86     
87     public int findRunEndingWith(int end) {
88         Integer JavaDoc 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     // queue oriented accessors
97

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