forked from runtimeverification/k
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathToolActivation.java
More file actions
150 lines (132 loc) · 4.53 KB
/
ToolActivation.java
File metadata and controls
150 lines (132 loc) · 4.53 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright (c) 2014-2019 K Team. All Rights Reserved.
package org.kframework.krun;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.ParameterDescription;
/**
* Represents a set of information used to determine whether a particular
* transformation should be added to the list of active transformations.
* (see {@link ActivatedTransformationProvider}).
* @author dwightguth
*
*/
public interface ToolActivation {
/**
* Returns true if the current option set activates this transformation;
* false otherwise.
* @param jcommander
* @return
*/
boolean isActive(JCommander jcommander);
/**
* Represents a key of a transformation which activates if
* a particular option is specified (i.e. true or non-null).
* @author dwightguth
*
*/
public static class OptionActivation implements ToolActivation {
private final String optionName;
public OptionActivation(String optionName) {
this.optionName = optionName;
}
@Override
public boolean isActive(JCommander jc) {
for (ParameterDescription param : jc.getParameters()) {
if (param.getLongestName().equals(optionName)) {
Object value = param.getParameterized().get(param.getObject());
if (value == null) {
return false;
}
return !(value instanceof Boolean) || (boolean) value;
}
}
return false;
}
@Override
public String toString() {
return optionName;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((optionName == null) ? 0 : optionName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
OptionActivation other = (OptionActivation) obj;
if (optionName == null) {
if (other.optionName != null)
return false;
} else if (!optionName.equals(other.optionName))
return false;
return true;
}
}
/**
* Represents the key of a transformation which activates if a particular
* optino has a particular value.
* @author dwightguth
*
* @param <T> The type of the value of the option.
*/
public static class OptionValueActivation<T> implements ToolActivation {
private final String optionName;
private final T value;
public OptionValueActivation(String optionName, T value) {
this.optionName = optionName;
this.value = value;
}
@Override
public boolean isActive(JCommander jc) {
for (ParameterDescription param : jc.getParameters()) {
if (param.getLongestName().equals(optionName)) {
Object value = param.getParameterized().get(param.getObject());
return value.equals(this.value);
}
}
return false;
}
@Override
public String toString() {
return optionName + " " + value;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((optionName == null) ? 0 : optionName.hashCode());
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
OptionValueActivation<?> other = (OptionValueActivation<?>) obj;
if (optionName == null) {
if (other.optionName != null)
return false;
} else if (!optionName.equals(other.optionName))
return false;
if (value == null) {
if (other.value != null)
return false;
} else if (!value.equals(other.value))
return false;
return true;
}
}
}