Clarify APIUtils methods meaning by renaming, deprecate former.

This commit is contained in:
asofold 2018-01-15 23:15:25 +01:00
parent adc0d16d73
commit 073220cc01
7 changed files with 83 additions and 44 deletions

View File

@ -361,7 +361,7 @@ public class ViolationHistory {
final ViolationLevel vl = it.next();
final CheckType refType = checkTypeMap.get(vl.check);
if (refType == null) continue;
if (refType == checkType || APIUtils.isParent(checkType, refType)){
if (refType == checkType || APIUtils.isAncestor(checkType, refType)){
found = true;
it.remove();
}

View File

@ -107,7 +107,7 @@ public class FightData extends ACheckData implements IRemoveSubCheckData {
public static CheckDataFactory getCheckDataFactory(CheckType checkType) {
if (checkType != CheckType.FIGHT && !APIUtils.isParent(CheckType.FIGHT, checkType)) {
if (checkType != CheckType.FIGHT && !APIUtils.isAncestor(CheckType.FIGHT, checkType)) {
throw new IllegalArgumentException("Can only return a CheckDataFactory for the check group FIGHT.");
}
switch(checkType) {

View File

@ -203,7 +203,7 @@ public class TopCommand extends BaseCommand{
type = CheckType.valueOf(args[i].trim().toUpperCase().replace('-', '_').replace('.', '_'));
} catch (Throwable t) {} // ...
if (type != null) {
checkTypes.addAll(APIUtils.getWithChildren(type)); // Includes type.
checkTypes.addAll(APIUtils.getWithDescendants(type)); // Includes type.
}
}
if (checkTypes.isEmpty()) {

View File

@ -30,11 +30,11 @@ import fr.neatmonster.nocheatplus.checks.CheckType;
*/
public class APIUtils {
/** Only the children. */
private static final Map<CheckType, Set<CheckType>> childrenMap = new HashMap<CheckType, Set<CheckType>>();
/** All descendants recursively. */
private static final Map<CheckType, Set<CheckType>> descendantsMap = new HashMap<CheckType, Set<CheckType>>();
/** Check including children, for convenient iteration. */
private static final Map<CheckType, Set<CheckType>> withChildrenMap = new HashMap<CheckType, Set<CheckType>>();
/** Check including all descendants recursively, for convenient iteration. */
private static final Map<CheckType, Set<CheckType>> withDescendantsMap = new HashMap<CheckType, Set<CheckType>>();
/** Checks/groups that might be run off the primary thread. */
private static final Set<CheckType> needSync = new HashSet<CheckType>();
@ -45,33 +45,37 @@ public class APIUtils {
for (final CheckType type : CheckType.values()) {
map.put(type, new LinkedHashSet<CheckType>());
}
for (final CheckType type : CheckType.values()){
for (final CheckType type : CheckType.values()) {
if (type != CheckType.ALL) {
map.get(CheckType.ALL).add(type);
}
for (final CheckType other : CheckType.values()){
if (isParent(other, type)) {
for (final CheckType other : CheckType.values()) {
if (isAncestor(other, type)) {
map.get(other).add(type);
}
}
}
for (final CheckType parent : map.keySet()){
for (final CheckType parent : map.keySet()) {
final Set<CheckType> set = map.get(parent);
childrenMap.put(parent, Collections.unmodifiableSet(set));
descendantsMap.put(parent, Collections.unmodifiableSet(set));
final Set<CheckType> wpSet = new LinkedHashSet<CheckType>(set);
wpSet.add(parent);
withChildrenMap.put(parent, Collections.unmodifiableSet(wpSet));
withDescendantsMap.put(parent,
Collections.unmodifiableSet(wpSet));
}
// needSync: Note that tests use the same definitions.
for (final CheckType checkType : new CheckType[]{CheckType.CHAT, CheckType.NET}) {
for (final CheckType checkType : new CheckType[] { CheckType.CHAT,
CheckType.NET }) {
needSync.add(checkType);
}
boolean added = true;
while (added) { // Just in case.
added = false;
for (final CheckType checkType: CheckType.values()) {
for (final CheckType checkType : CheckType.values()) {
// Fill in needSync.
if (checkType.getParent() != null && !needSync.contains(checkType) && needSync.contains(checkType.getParent())) {
if (checkType.getParent() != null
&& !needSync.contains(checkType)
&& needSync.contains(checkType.getParent())) {
needSync.add(checkType);
added = true;
}
@ -87,8 +91,9 @@ public class APIUtils {
* the check type
* @return the children
*/
public static final Set<CheckType> getChildren(final CheckType type) {
return childrenMap.get(type);
public static final Set<CheckType> getDescendants(
final CheckType type) {
return descendantsMap.get(type);
}
/**
@ -99,39 +104,45 @@ public class APIUtils {
* the check type
* @return the children
*/
public static final Set<CheckType> getWithChildren(final CheckType type) {
return withChildrenMap.get(type);
public static final Set<CheckType> getWithDescendants(
final CheckType type) {
return withDescendantsMap.get(type);
}
/**
* Check if the supposed parent is ancestor of the supposed child. Does not
* check versus the supposed child directly.
* Check if the supposed ancestor is an ancestor of the supposed descendant.
* Equality doesn't match here.
*
* @param supposedParent
* @param supposedAncestor
* the supposed parent
* @param supposedChild
* @param supposedDescendant
* the supposed child
* @return true, if is parent
*/
public static final boolean isParent(final CheckType supposedParent, final CheckType supposedChild) {
public static final boolean isAncestor(final CheckType supposedAncestor,
final CheckType supposedDescendant) {
// TODO: Perhaps rename to isAncestor !?
if (supposedParent == supposedChild) {
if (supposedAncestor == supposedDescendant) {
return false;
}
else if (supposedParent == CheckType.ALL) {
} else if (supposedAncestor == CheckType.ALL) {
return true;
}
CheckType parent = supposedChild.getParent();
CheckType parent = supposedDescendant.getParent();
while (parent != null)
if (parent == supposedParent) {
if (parent == supposedAncestor) {
return true;
}
else {
} else {
parent = parent.getParent();
}
return false;
}
@Deprecated
public static final boolean isParent(final CheckType supposedAncestor,
final CheckType supposedDescendant) {
return isAncestor(supposedAncestor, supposedDescendant);
}
/**
* Return if the check type requires synchronization. This indicates, if a
* check can be called off primary thread at all.
@ -146,4 +157,14 @@ public class APIUtils {
return needSync.contains(type);
}
@Deprecated
public static final Set<CheckType> getChildren(final CheckType type) {
return getDescendants(type);
}
@Deprecated
public static final Set<CheckType> getWithChildren(final CheckType type) {
return getWithDescendants(type);
}
}

View File

@ -109,7 +109,7 @@ public class NCPExemptionManager {
*/
public static final void exemptPermanently(final UUID id, final CheckType checkType) {
exempted.get(checkType).add(id);
for (final CheckType child : APIUtils.getChildren(checkType)) {
for (final CheckType child : APIUtils.getDescendants(checkType)) {
exempted.get(child).add(id);
}
}
@ -212,7 +212,7 @@ public class NCPExemptionManager {
*/
public static final void unexempt(final UUID id, final CheckType checkType) {
exempted.get(checkType).remove(id);
for (final CheckType child : APIUtils.getChildren(checkType)) {
for (final CheckType child : APIUtils.getDescendants(checkType)) {
exempted.get(child).remove(id);
}
}

View File

@ -389,7 +389,7 @@ public class DataManager implements Listener, INeedConfig, ComponentRegistry<IRe
public static boolean removeExecutionHistory(final CheckType type, final String playerName) {
boolean removed = false;
// TODO: design ...
for (final CheckType refType : APIUtils.getWithChildren(type)) {
for (final CheckType refType : APIUtils.getWithDescendants(type)) {
final Map<String, ExecutionHistory> map = instance.executionHistories.get(refType);
if (map != null && map.remove(playerName) != null) {
removed = true;
@ -406,7 +406,7 @@ public class DataManager implements Listener, INeedConfig, ComponentRegistry<IRe
*/
public static void clearData(final CheckType checkType) {
final Set<CheckDataFactory> factories = new HashSet<CheckDataFactory>();
for (final CheckType type : APIUtils.getWithChildren(checkType)) {
for (final CheckType type : APIUtils.getWithDescendants(checkType)) {
final Map<String, ExecutionHistory> map = instance.executionHistories.get(type);
if (map != null) {
map.clear();
@ -427,7 +427,7 @@ public class DataManager implements Listener, INeedConfig, ComponentRegistry<IRe
}
else if (rmd instanceof IHaveCheckType) {
final CheckType refType = ((IHaveCheckType) rmd).getCheckType();
if (refType == checkType || APIUtils.isParent(checkType, refType)) {
if (refType == checkType || APIUtils.isAncestor(checkType, refType)) {
rmd.removeAllData();
}
}
@ -448,7 +448,7 @@ public class DataManager implements Listener, INeedConfig, ComponentRegistry<IRe
public static void handleSystemTimeRanBackwards() {
// Collect data factories and clear execution history.
final Set<CheckDataFactory> factories = new HashSet<CheckDataFactory>();
for (final CheckType type : APIUtils.getWithChildren(CheckType.ALL)) {
for (final CheckType type : APIUtils.getWithDescendants(CheckType.ALL)) {
final Map<String, ExecutionHistory> map = instance.executionHistories.get(type);
if (map != null) {
map.clear();
@ -541,7 +541,7 @@ public class DataManager implements Listener, INeedConfig, ComponentRegistry<IRe
// Collect factories.
final Set<CheckDataFactory> factories = new HashSet<CheckDataFactory>();
for (CheckType otherType : APIUtils.getWithChildren(checkType)) {
for (CheckType otherType : APIUtils.getWithDescendants(checkType)) {
final CheckDataFactory otherFactory = otherType.getDataFactory();
if (otherFactory != null) {
factories.add(otherFactory);
@ -634,7 +634,7 @@ public class DataManager implements Listener, INeedConfig, ComponentRegistry<IRe
}
else if (rmd instanceof IHaveCheckType) {
final CheckType refType = ((IHaveCheckType) rmd).getCheckType();
if (refType == checkType || APIUtils.isParent(checkType, refType)) {
if (refType == checkType || APIUtils.isAncestor(checkType, refType)) {
if (rmd.removeData(PlayerName) != null) {
removed = true;
}

View File

@ -26,32 +26,50 @@ public class TestCheckType {
@Test
public void testIsParent() {
for (CheckType parent : CheckType.values()) {
if (parent != CheckType.ALL && !APIUtils.isParent(CheckType.ALL, parent)) {
if (parent != CheckType.ALL && !APIUtils.isAncestor(CheckType.ALL, parent)) {
fail("Expect ALL to be parent of " + parent + " .");
}
// Rough simplified check by naming.
String parentName = parent.getName();
for (final CheckType child : CheckType.values()) {
if (child == parent) {
if (APIUtils.isParent(parent, child)) {
if (APIUtils.isAncestor(parent, child)) {
fail("Check can't be parent of itself: " + parent);
}
// Ignore otherwise.
continue;
}
String childName = child.getName();
if (childName.startsWith(parentName) && childName.charAt(parentName.length()) == '.' && !APIUtils.isParent(parent, child)) {
if (childName.startsWith(parentName) && childName.charAt(parentName.length()) == '.' && !APIUtils.isAncestor(parent, child)) {
fail("Expect " + parentName + " to be parent of " + childName + ".");
}
}
}
}
@Test
public void testChildren() {
for (CheckType checkType : CheckType.values()) {
// checkType is child of parent.
if (checkType.getParent() != null) {
if (!APIUtils.getDescendants(checkType.getParent()).contains(checkType)) {
fail("Expect parents children to contain self: " + checkType);
}
}
// checkType is direct parent of all children.
for (CheckType child : APIUtils.getDescendants(checkType)) {
if (child.getParent() != checkType) {
fail("Expect " + checkType + " to be direct parent of " + child + ", insteat parent is set to: " + child.getParent());
}
}
}
}
@Test
public void testNeedsSynchronization() {
for (CheckType parent : new CheckType[]{CheckType.CHAT, CheckType.NET}) {
for (CheckType type : CheckType.values()) {
if ((parent == type || APIUtils.isParent(parent, type)) && !APIUtils.needsSynchronization(type)) {
if ((parent == type || APIUtils.isAncestor(parent, type)) && !APIUtils.needsSynchronization(type)) {
fail("Expect " + type + " to need synchronization, as it is child of " + parent);
}
}