-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathState.java
More file actions
114 lines (86 loc) · 3.26 KB
/
State.java
File metadata and controls
114 lines (86 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package syntax_analysis.grammar_structure_creation;
import static helper_objects.ToStringFormatting.indentFormat;
import java.util.*;
import java.util.Map.*;
import grammar_objects.*;
public class State {
private Set<? extends GrammarPosition> positions;
private State parentState;
private Set<Route> branches;
public State(Set<? extends GrammarPosition> positions, State parentState) {
if(positions == null) { positions = new HashSet<>(); }
this.positions = positions;
this.parentState = parentState;
branches = new HashSet<>();
}
public Set<? extends GrammarPosition> getPositions() {
return positions;
}
public State getParentState() {
return parentState;
}
public State addBranch(Route branch) {
branches.add(branch);
return this;
}
public Set<Route> getBranches() {
return branches;
}
@Override
public boolean equals(Object obj) {
if(!(obj instanceof State)) { return false; }
State otherState = (State)obj;
if(positions.size() != otherState.getPositions().size()) { return false; }
for(GrammarPosition position : positions) {
if(!otherState.getPositions().contains(position)) { return false; }
}
if(branches.size() != otherState.getBranches().size()) { return false; }
return branchesEqual(otherState);
}
/**
* Tests equality of branches between this and another state, based on number of Routes for each LexicalElement
* @param otherState The other state to be compared with
* @return The equality of both state's branches
*/
private boolean branchesEqual(State otherState) {
HashMap<LexicalElement, Integer> currentRouteCount = new HashMap<>();
HashMap<LexicalElement, Integer> otherRouteCount = new HashMap<>();
for (Route branch : getBranches()) {
LexicalElement elementTraversed = branch.elementTraversed();
if(currentRouteCount.get(elementTraversed) != null) {
currentRouteCount.put(elementTraversed, currentRouteCount.get(elementTraversed) + 1);
}
else {
currentRouteCount.put(elementTraversed, 1);
}
}
for (Route otherBranch : otherState.getBranches()) {
LexicalElement elementTraversed = otherBranch.elementTraversed();
if(otherRouteCount.get(elementTraversed) != null) {
otherRouteCount.put(elementTraversed, otherRouteCount.get(elementTraversed) + 1);
}
else {
otherRouteCount.put(elementTraversed, 1);
}
}
boolean routesEqual = true;
for(Entry<LexicalElement, Integer> entry : currentRouteCount.entrySet()) {
if(otherRouteCount.get(entry.getKey()) != entry.getValue()) {
routesEqual = false;
}
}
return routesEqual;
}
@Override
public int hashCode() {
int hashCode = 1;
for(GrammarPosition position : positions) {
hashCode *= position.hashCode();
}
return hashCode;
}
@Override
public String toString() {
return "State:\n" + indentFormat(positions);
}
}