1 17 18 package org.sablecc.sablecc.structure; 19 20 import java.util.Collections ; 21 import java.util.SortedSet ; 22 import java.util.TreeSet ; 23 24 import org.sablecc.sablecc.exception.InternalException; 25 26 public class Selector 27 implements Comparable <Selector> { 28 29 private final String name; 30 31 private SortedSet <Token> selection = new TreeSet <Token>(); 32 33 private boolean isStable = false; 34 35 Selector( 36 String name) { 37 38 if (name == null) { 39 throw new InternalException("name may not be null"); 40 } 41 42 this.name = name; 43 } 44 45 public String getName() { 46 47 return this.name; 48 } 49 50 public SortedSet <Token> getSelection() { 51 52 if (!this.isStable) { 53 throw new InternalException("this selector is not stable yet"); 54 } 55 56 return this.selection; 57 } 58 59 boolean isStable() { 60 61 return this.isStable; 62 } 63 64 @Override 65 public boolean equals( 66 Object obj) { 67 68 if (this == obj) { 69 return true; 70 } 71 72 if (obj == null) { 73 return false; 74 } 75 76 if (getClass() != obj.getClass()) { 77 return false; 78 } 79 80 Selector selector = (Selector) obj; 81 82 return this.name.equals(selector.name); 83 } 84 85 @Override 86 public int hashCode() { 87 88 return this.name.hashCode(); 89 } 90 91 public int compareTo( 92 Selector selector) { 93 94 return this.name.compareTo(selector.name); 95 } 96 97 void stabilize() { 98 99 if (this.isStable) { 100 throw new InternalException("this selector is already stable"); 101 } 102 103 if (this.selection.size() < 2) { 104 throw new InternalException("the selection is too small"); 105 } 106 107 this.selection = Collections.unmodifiableSortedSet(this.selection); 108 this.isStable = true; 109 } 110 111 public void addToken( 112 Token token) { 113 114 if (token == null) { 115 throw new InternalException("token may not be null"); 116 } 117 118 if (this.selection.contains(token)) { 119 throw new InternalException( 120 "the token is already included in the selection"); 121 } 122 123 this.selection.add(token); 124 } 125 } 126 | Popular Tags |