|
@@ -27,17 +27,25 @@ import com.dmdirc.events.UserErrorEvent;
|
27
|
27
|
import com.dmdirc.logger.ErrorLevel;
|
28
|
28
|
|
29
|
29
|
import java.lang.reflect.Method;
|
|
30
|
+import java.util.HashMap;
|
|
31
|
+import java.util.Map;
|
30
|
32
|
import java.util.Optional;
|
|
33
|
+import java.util.function.Function;
|
31
|
34
|
|
32
|
35
|
import javax.inject.Inject;
|
33
|
36
|
|
34
|
37
|
public class EventPropertyManager {
|
35
|
38
|
|
36
|
39
|
private final DMDircMBassador eventBus;
|
|
40
|
+ private final Map<String, Function<String, String>> functions = new HashMap<>();
|
37
|
41
|
|
38
|
42
|
@Inject
|
39
|
43
|
public EventPropertyManager(final DMDircMBassador eventBus) {
|
40
|
44
|
this.eventBus = eventBus;
|
|
45
|
+
|
|
46
|
+ functions.put("uppercase", String::toUpperCase);
|
|
47
|
+ functions.put("lowercase", String::toLowerCase);
|
|
48
|
+ functions.put("trim", String::trim);
|
41
|
49
|
}
|
42
|
50
|
|
43
|
51
|
public <S> Optional<Object> getProperty(final S object, final Class<? extends S> type,
|
|
@@ -48,15 +56,20 @@ public class EventPropertyManager {
|
48
|
56
|
final Method method = type.getMethod(methodName);
|
49
|
57
|
return Optional.ofNullable(method.invoke(object));
|
50
|
58
|
} catch (ReflectiveOperationException ex) {
|
51
|
|
- eventBus.publish(new UserErrorEvent(ErrorLevel.MEDIUM, ex,
|
|
59
|
+ eventBus.publishAsync(new UserErrorEvent(ErrorLevel.MEDIUM, ex,
|
52
|
60
|
"Unable to format event: could not retrieve property " + property,
|
53
|
61
|
ex.getMessage()));
|
54
|
62
|
}
|
55
|
63
|
return Optional.empty();
|
56
|
64
|
}
|
57
|
65
|
|
58
|
|
- public Optional<String> applyFunction(final String input, final String function) {
|
59
|
|
- return Optional.empty();
|
|
66
|
+ public String applyFunction(final String input, final String function) {
|
|
67
|
+ if (functions.containsKey(function)) {
|
|
68
|
+ return functions.get(function).apply(input);
|
|
69
|
+ }
|
|
70
|
+ eventBus.publishAsync(new UserErrorEvent(ErrorLevel.LOW, null,
|
|
71
|
+ "Unable to format event: no such function " + function, null));
|
|
72
|
+ return input;
|
60
|
73
|
}
|
61
|
74
|
|
62
|
75
|
}
|