add readonly bit to sets

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9458]
This commit is contained in:
parrt 2011-11-25 14:39:05 -08:00
parent c69814515b
commit 6898dc6f5e
1 changed files with 14 additions and 0 deletions

View File

@ -57,6 +57,8 @@ public class IntervalSet implements IntSet {
/** The list of sorted, disjoint intervals. */ /** The list of sorted, disjoint intervals. */
protected List<Interval> intervals; protected List<Interval> intervals;
protected boolean readonly;
/** Create a set with no elements */ /** Create a set with no elements */
public IntervalSet() { public IntervalSet() {
intervals = new ArrayList<Interval>(2); // most sets are 1 or 2 elements intervals = new ArrayList<Interval>(2); // most sets are 1 or 2 elements
@ -87,6 +89,7 @@ public class IntervalSet implements IntSet {
} }
public void clear() { public void clear() {
if ( readonly ) throw new IllegalStateException("can't alter readonly IntervalSet");
intervals.clear(); intervals.clear();
} }
@ -95,6 +98,7 @@ public class IntervalSet implements IntSet {
*/ */
@Override @Override
public void add(int el) { public void add(int el) {
if ( readonly ) throw new IllegalStateException("can't alter readonly IntervalSet");
add(el,el); add(el,el);
} }
@ -111,6 +115,7 @@ public class IntervalSet implements IntSet {
// copy on write so we can cache a..a intervals and sets of that // copy on write so we can cache a..a intervals and sets of that
protected void add(Interval addition) { protected void add(Interval addition) {
if ( readonly ) throw new IllegalStateException("can't alter readonly IntervalSet");
//System.out.println("add "+addition+" to "+intervals.toString()); //System.out.println("add "+addition+" to "+intervals.toString());
if ( addition.b<addition.a ) { if ( addition.b<addition.a ) {
return; return;
@ -564,6 +569,7 @@ public class IntervalSet implements IntSet {
@Override @Override
public void remove(int el) { public void remove(int el) {
if ( readonly ) throw new IllegalStateException("can't alter readonly IntervalSet");
int n = intervals.size(); int n = intervals.size();
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
Interval I = intervals.get(i); Interval I = intervals.get(i);
@ -595,4 +601,12 @@ public class IntervalSet implements IntSet {
} }
} }
} }
public boolean isReadonly() {
return readonly;
}
public void setReadonly(boolean readonly) {
this.readonly = readonly;
}
} }