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 Group 27 implements Comparable <Group> { 28 29 private final String name; 30 31 private SortedSet <Token> tokens = new TreeSet <Token>(); 32 33 private boolean isStable = false; 34 35 Group( 36 String name) { 37 38 this.name = name; 39 } 40 41 public boolean hasName() { 42 43 return this.name != null; 44 } 45 46 public String getName() { 47 48 if (this.name == null) { 49 throw new InternalException("this group does not have a name"); 50 } 51 52 return this.name; 53 } 54 55 public SortedSet <Token> getTokens() { 56 57 if (!this.isStable) { 58 throw new InternalException("this group is not stable yet"); 59 } 60 61 return this.tokens; 62 } 63 64 boolean isStable() { 65 66 return this.isStable; 67 } 68 69 @Override 70 public boolean equals( 71 Object obj) { 72 73 if (this == obj) { 74 return true; 75 } 76 77 if (obj == null) { 78 return false; 79 } 80 81 if (getClass() != obj.getClass()) { 82 return false; 83 } 84 85 Group group = (Group) obj; 86 87 if (this.name == null) { 88 return this.name == group.name; 89 } 90 91 return this.name.equals(group.name); 92 } 93 94 @Override 95 public int hashCode() { 96 97 if (this.name == null) { 98 return 0; 99 } 100 101 return this.name.hashCode(); 102 } 103 104 public int compareTo( 105 Group group) { 106 107 if (this.name == null) { 108 if (group.name == null) { 109 return 0; 110 } 111 112 return -1; 113 } 114 else if (group == null) { 115 return 1; 116 } 117 118 return this.name.compareTo(group.name); 119 } 120 121 void stabilize() { 122 123 if (this.isStable) { 124 throw new InternalException("this group is already stable"); 125 } 126 127 this.tokens = Collections.unmodifiableSortedSet(this.tokens); 128 this.isStable = true; 129 } 130 131 void addToken( 132 Token token) { 133 134 if (this.isStable) { 135 throw new InternalException("a stable group may not be modified"); 136 } 137 138 if (this.tokens.contains(token)) { 139 throw new InternalException( 140 "the token is already included in this group"); 141 } 142 143 if (token.getGroup() != this) { 144 throw new InternalException( 145 "the token does not belong to this group"); 146 } 147 148 this.tokens.add(token); 149 } 150 } 151 | Popular Tags |