Skip to content

Commit 337e43a

Browse files
committed
feat: remove methods for function and constant manager + exception when registering a function or constant that already exists
1 parent fcdfb99 commit 337e43a

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

api/src/main/java/com/instancify/scriptify/api/script/constant/ScriptConstantManager.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,11 @@ public interface ScriptConstantManager {
3232
* @param constant The constant to be registered
3333
*/
3434
void register(ScriptConstant constant);
35+
36+
/**
37+
* Removes an existing constant in the manager.
38+
*
39+
* @param name The name of the constant to remove
40+
*/
41+
void remove(String name);
3542
}

api/src/main/java/com/instancify/scriptify/api/script/function/ScriptFunctionManager.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,11 @@ public interface ScriptFunctionManager {
3232
* @param function The function to be registered
3333
*/
3434
void register(ScriptFunction function);
35+
36+
/**
37+
* Removes an existing function in the manager.
38+
*
39+
* @param name The name of the function to remove
40+
*/
41+
void remove(String name);
3542
}

core/src/main/java/com/instancify/scriptify/core/script/constant/StandardConstantManager.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ public Map<String, ScriptConstant> getConstants() {
2424

2525
@Override
2626
public void register(ScriptConstant constant) {
27-
constants.put(constant.getName(), constant);
27+
if (!constants.containsKey(constant.getName())) {
28+
constants.put(constant.getName(), constant);
29+
} else {
30+
throw new IllegalStateException("The constant with this name already exists");
31+
}
32+
}
33+
34+
@Override
35+
public void remove(String name) {
36+
if (constants.containsKey(name)) {
37+
constants.remove(name);
38+
} else {
39+
throw new IllegalArgumentException("The constant with this name does not exist");
40+
}
2841
}
2942
}

core/src/main/java/com/instancify/scriptify/core/script/function/StandardFunctionManager.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@ public Map<String, ScriptFunction> getFunctions() {
6060

6161
@Override
6262
public void register(ScriptFunction function) {
63-
functions.put(function.getName(), function);
63+
if (!functions.containsKey(function.getName())) {
64+
functions.put(function.getName(), function);
65+
} else {
66+
throw new IllegalStateException("The function with this name already exists");
67+
}
68+
}
69+
70+
@Override
71+
public void remove(String name) {
72+
if (functions.containsKey(name)) {
73+
functions.remove(name);
74+
} else {
75+
throw new IllegalArgumentException("The function with this name does not exist");
76+
}
6477
}
6578
}

0 commit comments

Comments
 (0)