@@ -16,21 +16,43 @@ public class Requirements {
16
16
* Require a new input parameter.
17
17
*/
18
18
public final Input input = new Input (this );
19
-
20
19
/**
21
20
* Require a new output parameter.
22
21
*/
23
22
public final Output output = new Output (this );
24
-
25
23
/**
26
24
* Require a new parameter.
27
25
*/
28
26
public final Parameter parameter = new Parameter (this );
29
27
28
+ private RequirementsAccessor accessor ;
29
+
30
30
private Map <String , Requirement <? extends Serializable >> inputRequirements = new HashMap <>();
31
31
private Map <String , Requirement <? extends Serializable >> outputRequirements = new HashMap <>();
32
32
private Map <String , Requirement <? extends Serializable >> parameterRequirements = new HashMap <>();
33
33
34
+ public Requirements () {
35
+ this .accessor = new RequirementsAccessor ();
36
+ }
37
+
38
+ /**
39
+ * Retrieves the Accessor for write privileges on requirements.
40
+ * Please note, that this function is only legal to call in the framework, not inside your plugins code.
41
+ *
42
+ * @return the requirement accessor with utility functions on requirements
43
+ * @throws SecurityException thrown when used from inside a plugin
44
+ */
45
+ public RequirementsAccessor getAccess () throws SecurityException {
46
+ if (System .getSecurityManager () == null ) {
47
+ throw new SecurityException ("No security manager available." );
48
+ } else {
49
+ // If this throws an exception, we are probably inside a plugin
50
+ System .getSecurityManager ().checkCreateClassLoader ();
51
+
52
+ return this .accessor ;
53
+ }
54
+ }
55
+
34
56
<T extends Serializable > Requirement <T > requireInput (String uniqueRequirementId , String name , boolean isOptional , Class targetType ) {
35
57
return addRequirement (inputRequirements , uniqueRequirementId , name , isOptional , targetType );
36
58
}
@@ -65,76 +87,100 @@ private <T extends Serializable> Requirement<T> addRequirement(Map<String, Requi
65
87
return requirement ;
66
88
}
67
89
68
- /**
69
- * Returns a requirement of any type (input / output / parameter) with the given unique id.
70
- *
71
- * @param uniqueRequirementId the plugin unique requirement id
72
- * @return an optional encapsulating the requirement or none
73
- */
74
- Optional <? extends Requirement <? extends Serializable >> getRequirementById (String uniqueRequirementId ) {
75
- return getAllEntries ()
76
- .filter (entry -> entry .getKey ().equals (uniqueRequirementId ))
77
- .map (Map .Entry ::getValue ).findFirst ();
90
+ private Stream <Map .Entry <String , Requirement <? extends Serializable >>> getAllEntries () {
91
+ return Stream .of (inputRequirements .entrySet ().stream (),
92
+ outputRequirements .entrySet ().stream (),
93
+ parameterRequirements .entrySet ().stream ()).reduce (Stream ::concat ).get ();
78
94
}
79
95
80
96
/**
81
- * Unsets the value of a requirement specified by the unique id.
82
- *
83
- * @param uniqueRequirementId the plugin unique requirement id
84
- * @return true, if the requirement exists and previously had a value
97
+ * This accessor is used to get write access to encapsulated requirements.
85
98
*/
86
- boolean unsetRequirementById (String uniqueRequirementId ) {
87
- Optional <? extends Requirement <? extends Serializable >> requirement =
88
- getAllEntries ().filter (entry -> entry .getKey ().equals (uniqueRequirementId ))
89
- .map (Map .Entry ::getValue ).findFirst ();
90
-
91
- boolean wasSet = requirement .isPresent () && requirement .get ().isSet ();
92
- requirement .ifPresent (Requirement ::unsetValue );
93
- return wasSet ;
94
- }
99
+ public class RequirementsAccessor {
95
100
96
- /**
97
- * Tests, if all non-optional requirements had been set (e.g. by the framework)
98
- *
99
- * @return true, if all needed requirements had been set
100
- */
101
- Boolean isComplete () {
102
- return getAllEntries ()
103
- .map (Map .Entry ::getValue )
104
- .allMatch (req -> req .isSet () || req .isOptional ());
105
- }
101
+ private RequirementsAccessor () {
106
102
107
- /**
108
- * Returns a list of all optional and non-optional requirements which are not set.
109
- *
110
- * @return a list of unique requirement ids
111
- */
112
- List <Requirement <? extends Serializable >> getMissingRequirements () {
113
- return getAllEntries ()
114
- .filter (entry -> !entry .getValue ().isSet ())
115
- .map (Map .Entry ::getValue )
116
- .collect (Collectors .toList ());
117
- }
103
+ }
118
104
119
- private Stream <Map .Entry <String , Requirement <? extends Serializable >>> getAllEntries () {
120
- return Stream .of (inputRequirements .entrySet ().stream (),
121
- outputRequirements .entrySet ().stream (),
122
- parameterRequirements .entrySet ().stream ()).reduce (Stream ::concat ).get ();
123
- }
105
+ /**
106
+ * Returns a requirement of any type (input / output / parameter) with the given unique id.
107
+ *
108
+ * @param uniqueRequirementId the plugin unique requirement id
109
+ * @return an optional encapsulating the requirement or none
110
+ */
111
+ public Optional <? extends Requirement <? extends Serializable >> getRequirementById (String uniqueRequirementId ) {
112
+ return getAllEntries ()
113
+ .filter (entry -> entry .getKey ().equals (uniqueRequirementId ))
114
+ .map (Map .Entry ::getValue ).findFirst ();
115
+ }
124
116
125
- Map <String , Requirement <? extends Serializable >> getRequirementMap () {
126
- return getAllEntries ().collect (Collectors .toMap (Map .Entry ::getKey , Map .Entry ::getValue ));
127
- }
117
+ /**
118
+ * Unsets the value of a requirement specified by the unique id.
119
+ *
120
+ * @param uniqueRequirementId the plugin unique requirement id
121
+ * @return true, if the requirement exists and previously had a value
122
+ */
123
+ public boolean unsetRequirementById (String uniqueRequirementId ) {
124
+ Optional <? extends Requirement <? extends Serializable >> requirement =
125
+ getAllEntries ().filter (entry -> entry .getKey ().equals (uniqueRequirementId ))
126
+ .map (Map .Entry ::getValue ).findFirst ();
127
+
128
+ boolean wasSet = requirement .isPresent () && requirement .get ().isSet ();
129
+ requirement .ifPresent (Requirement ::unsetValue );
130
+ return wasSet ;
131
+ }
128
132
129
- Collection <Requirement <? extends Serializable >> getInputRequirements () {
130
- return inputRequirements .values ();
131
- }
133
+ /**
134
+ * Tests, if all non-optional requirements had been set (e.g. by the framework)
135
+ *
136
+ * @return true, if all needed requirements had been set
137
+ */
138
+ public Boolean isComplete () {
139
+ return getAllEntries ()
140
+ .map (Map .Entry ::getValue )
141
+ .allMatch (req -> req .isSet () || req .isOptional ());
142
+ }
132
143
133
- Collection <Requirement <? extends Serializable >> getOutputRequirements () {
134
- return outputRequirements .values ();
135
- }
144
+ /**
145
+ * Returns a list of all optional and non-optional requirements which are not set.
146
+ *
147
+ * @return a list of unique requirement ids
148
+ */
149
+ public List <Requirement <? extends Serializable >> getMissingRequirements () {
150
+ return getAllEntries ()
151
+ .filter (entry -> !entry .getValue ().isSet ())
152
+ .map (Map .Entry ::getValue )
153
+ .collect (Collectors .toList ());
154
+ }
155
+
156
+ /**
157
+ * Sets a requirements content if the specified requirement exists.
158
+ *
159
+ * @param uniqueRequirementId the unique id of a requirement which must be created first
160
+ * @param content the serialized content to set
161
+ */
162
+ public void setRequirementContent (String uniqueRequirementId , Serializable content ) {
163
+ // This is a crazy cast but should never go wrong. If this fails, please call 911 (don't)
164
+ //noinspection unchecked
165
+ getRequirementById (uniqueRequirementId )
166
+ .ifPresent (req -> ((Requirement <Serializable >) req ).set (content ));
167
+ }
168
+
169
+ public Map <String , Requirement <? extends Serializable >> getRequirementMap () {
170
+ return getAllEntries ().collect (Collectors .toMap (Map .Entry ::getKey , Map .Entry ::getValue ));
171
+ }
136
172
137
- Collection <Requirement <? extends Serializable >> getParameterRequirements () {
138
- return parameterRequirements .values ();
173
+ public Collection <Requirement <? extends Serializable >> getInputRequirements () {
174
+ return inputRequirements .values ();
175
+ }
176
+
177
+ public Collection <Requirement <? extends Serializable >> getOutputRequirements () {
178
+ return outputRequirements .values ();
179
+ }
180
+
181
+ public Collection <Requirement <? extends Serializable >> getParameterRequirements () {
182
+ return parameterRequirements .values ();
183
+ }
139
184
}
185
+
140
186
}
0 commit comments