Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit 3133d80

Browse files
committed
Added requirements accessor for write access in framework code.
1 parent 2b27ded commit 3133d80

File tree

2 files changed

+109
-63
lines changed

2 files changed

+109
-63
lines changed

src/main/java/org/codeoverflow/chatoverflow/api/plugin/configuration/Requirement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ void setOptional(boolean optional) {
100100
*
101101
* @return A class object holding the value set while creating the requirement object
102102
*/
103-
Class getTargetType() {
103+
public Class getTargetType() {
104104
return targetType;
105105
}
106106

src/main/java/org/codeoverflow/chatoverflow/api/plugin/configuration/Requirements.java

Lines changed: 108 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,43 @@ public class Requirements {
1616
* Require a new input parameter.
1717
*/
1818
public final Input input = new Input(this);
19-
2019
/**
2120
* Require a new output parameter.
2221
*/
2322
public final Output output = new Output(this);
24-
2523
/**
2624
* Require a new parameter.
2725
*/
2826
public final Parameter parameter = new Parameter(this);
2927

28+
private RequirementsAccessor accessor;
29+
3030
private Map<String, Requirement<? extends Serializable>> inputRequirements = new HashMap<>();
3131
private Map<String, Requirement<? extends Serializable>> outputRequirements = new HashMap<>();
3232
private Map<String, Requirement<? extends Serializable>> parameterRequirements = new HashMap<>();
3333

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+
3456
<T extends Serializable> Requirement<T> requireInput(String uniqueRequirementId, String name, boolean isOptional, Class targetType) {
3557
return addRequirement(inputRequirements, uniqueRequirementId, name, isOptional, targetType);
3658
}
@@ -65,76 +87,100 @@ private <T extends Serializable> Requirement<T> addRequirement(Map<String, Requi
6587
return requirement;
6688
}
6789

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();
7894
}
7995

8096
/**
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.
8598
*/
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 {
95100

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() {
106102

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+
}
118104

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+
}
124116

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+
}
128132

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+
}
132143

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+
}
136172

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+
}
139184
}
185+
140186
}

0 commit comments

Comments
 (0)