1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-25 15:52:06 +01:00

[core] Add String-based StateDescriptor precedence/inferiority declaration methods

This commit is contained in:
Florian Schmaus 2020-07-25 14:27:45 +02:00
parent 64fb47c98b
commit ec80d5287b

View file

@ -22,12 +22,16 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection; import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection;
import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal; import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal;
public abstract class StateDescriptor { public abstract class StateDescriptor {
private static final Logger LOGGER = Logger.getLogger(StateDescriptor.class.getName());
public enum Property { public enum Property {
multiVisitState, multiVisitState,
finalState, finalState,
@ -133,10 +137,35 @@ public abstract class StateDescriptor {
addAndCheckNonExistent(precedenceOver, subordinate); addAndCheckNonExistent(precedenceOver, subordinate);
} }
protected void declarePrecedenceOver(String subordinate) {
addAndCheckNonExistent(precedenceOver, subordinate);
}
protected void declareInferiorityTo(Class<? extends StateDescriptor> superior) { protected void declareInferiorityTo(Class<? extends StateDescriptor> superior) {
addAndCheckNonExistent(inferiorTo, superior); addAndCheckNonExistent(inferiorTo, superior);
} }
protected void declareInferiorityTo(String superior) {
addAndCheckNonExistent(inferiorTo, superior);
}
private static void addAndCheckNonExistent(Set<Class<? extends StateDescriptor>> set, String clazzName) {
Class<?> clazz;
try {
clazz = Class.forName(clazzName);
} catch (ClassNotFoundException e) {
// The state descriptor class is not in classpath, which probably means that the smack module is not loaded
// into the classpath. Hence we can silently ignore that.
LOGGER.log(Level.FINEST, "Ignoring unknown state descriptor '" + clazzName + "'", e);
return;
}
if (!StateDescriptor.class.isAssignableFrom(clazz)) {
throw new IllegalArgumentException(clazz + " is no state descriptor class");
}
Class<? extends StateDescriptor> stateDescriptorClass = clazz.asSubclass(StateDescriptor.class);
addAndCheckNonExistent(set, stateDescriptorClass);
}
private static <E> void addAndCheckNonExistent(Set<E> set, E e) { private static <E> void addAndCheckNonExistent(Set<E> set, E e) {
boolean newElement = set.add(e); boolean newElement = set.add(e);
if (!newElement) { if (!newElement) {