Code cleanup

This commit is contained in:
0xb00bface 2023-06-16 18:14:14 +02:00
parent 97ab13891e
commit 2faab3d67f
1 changed files with 24 additions and 54 deletions

View File

@ -1,5 +1,14 @@
package ctbrec.event; package ctbrec.event;
import com.google.common.eventbus.Subscribe;
import ctbrec.Config;
import ctbrec.event.Event.Type;
import ctbrec.event.EventHandlerConfiguration.ActionConfiguration;
import ctbrec.event.EventHandlerConfiguration.PredicateConfiguration;
import lombok.EqualsAndHashCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -7,16 +16,7 @@ import java.util.List;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Predicate; import java.util.function.Predicate;
import ctbrec.Config; @EqualsAndHashCode(of = "id")
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.eventbus.Subscribe;
import ctbrec.event.Event.Type;
import ctbrec.event.EventHandlerConfiguration.ActionConfiguration;
import ctbrec.event.EventHandlerConfiguration.PredicateConfiguration;
public class EventHandler { public class EventHandler {
private static final Logger LOG = LoggerFactory.getLogger(EventHandler.class); private static final Logger LOG = LoggerFactory.getLogger(EventHandler.class);
@ -36,17 +36,13 @@ public class EventHandler {
return id; return id;
} }
@SafeVarargs
public EventHandler(Action action, EventPredicate... predicates) { public EventHandler(Action action, EventPredicate... predicates) {
this(Collections.singletonList(action), predicates); this(Collections.singletonList(action), predicates);
} }
@SafeVarargs
public EventHandler(List<Action> actions, EventPredicate... predicates) { public EventHandler(List<Action> actions, EventPredicate... predicates) {
this.actions = actions; this.actions = actions;
for (EventPredicate predicate : predicates) { Collections.addAll(this.predicates, predicates);
this.predicates.add(predicate);
}
} }
@Subscribe @Subscribe
@ -59,85 +55,59 @@ public class EventHandler {
try { try {
boolean matches = true; boolean matches = true;
for (Predicate<Event> predicate : predicates) { for (Predicate<Event> predicate : predicates) {
if(!predicate.test(evt)) { if (!predicate.test(evt)) {
matches = false; matches = false;
} }
} }
if(matches) { if (matches) {
for (Consumer<Event> action : actions) { for (Consumer<Event> action : actions) {
action.accept(evt); action.accept(evt);
} }
} }
} catch(Exception e) { } catch (Exception e) {
LOG.error("Error while processing event", e); LOG.error("Error while processing event", e);
} }
} }
private List<EventPredicate> createPredicates(EventHandlerConfiguration config) { private List<EventPredicate> createPredicates(EventHandlerConfiguration config) {
List<EventPredicate> predicates = new ArrayList<>(config.getPredicates().size()); List<EventPredicate> predicateList = new ArrayList<>(config.getPredicates().size());
for (PredicateConfiguration pc : config.getPredicates()) { for (PredicateConfiguration pc : config.getPredicates()) {
try { try {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Class<EventPredicate> cls = (Class<EventPredicate>) Class.forName(pc.getType()); Class<EventPredicate> cls = (Class<EventPredicate>) Class.forName(pc.getType());
if(cls == null) { if (cls == null) {
LOG.warn("Ignoring unknown action {}", cls); LOG.warn("Ignoring unknown action {}", cls);
continue; continue;
} }
EventPredicate predicate = cls.getDeclaredConstructor().newInstance(); EventPredicate predicate = cls.getDeclaredConstructor().newInstance();
predicate.configure(pc); predicate.configure(pc);
predicates.add(predicate); predicateList.add(predicate);
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | IllegalArgumentException | InvocationTargetException |
NoSuchMethodException | SecurityException e) {
LOG.warn("Error while creating action {} {}", pc.getType(), pc.getConfiguration(), e); LOG.warn("Error while creating action {} {}", pc.getType(), pc.getConfiguration(), e);
} }
} }
return predicates; return predicateList;
} }
private List<Action> createActions(EventHandlerConfiguration config) { private List<Action> createActions(EventHandlerConfiguration config) {
List<Action> actions = new ArrayList<>(config.getActions().size()); List<Action> actionList = new ArrayList<>(config.getActions().size());
for (ActionConfiguration ac : config.getActions()) { for (ActionConfiguration ac : config.getActions()) {
try { try {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Class<Action> cls = (Class<Action>) Class.forName(ac.getType()); Class<Action> cls = (Class<Action>) Class.forName(ac.getType());
if(cls == null) { if (cls == null) {
LOG.warn("Ignoring unknown action {}", cls); LOG.warn("Ignoring unknown action {}", cls);
continue; continue;
} }
Action action = cls.getDeclaredConstructor().newInstance(); Action action = cls.getDeclaredConstructor().newInstance();
action.configure(ac); action.configure(ac);
actions.add(action); actionList.add(action);
} catch (Exception e) { } catch (Exception e) {
LOG.warn("Error while creating action {} {}", ac.getType(), ac.getConfiguration(), e); LOG.warn("Error while creating action {} {}", ac.getType(), ac.getConfiguration(), e);
} }
} }
return actions; return actionList;
} }
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.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;
EventHandler other = (EventHandler) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
} }