Some node equality changes

This commit is contained in:
Luck 2018-12-14 17:27:34 +00:00
parent 085e7af681
commit de24817d9c
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
6 changed files with 41 additions and 49 deletions

View File

@ -430,17 +430,6 @@ public interface Node {
@Override @Override
boolean equals(Object obj); boolean equals(Object obj);
/**
* Gets if this Node is equal to another node as defined by the given
* {@link StandardNodeEquality} predicate.
*
* @param other the other node
* @param equalityPredicate the predicate
* @return true if this node is considered equal
* @since 4.1
*/
boolean standardEquals(Node other, StandardNodeEquality equalityPredicate);
/** /**
* Gets if this Node is equal to another node as defined by the given * Gets if this Node is equal to another node as defined by the given
* {@link NodeEqualityPredicate}. * {@link NodeEqualityPredicate}.
@ -450,9 +439,7 @@ public interface Node {
* @return true if this node is considered equal * @return true if this node is considered equal
* @since 4.1 * @since 4.1
*/ */
default boolean equals(Node other, NodeEqualityPredicate equalityPredicate) { boolean equals(Node other, NodeEqualityPredicate equalityPredicate);
return equalityPredicate.areEqual(this, other);
}
/** /**
* Similar to {@link Node#equals(Object)}, except doesn't take note of the * Similar to {@link Node#equals(Object)}, except doesn't take note of the

View File

@ -41,7 +41,10 @@ import org.checkerframework.checker.nullness.qual.NonNull;
public interface NodeEqualityPredicate { public interface NodeEqualityPredicate {
/** /**
* Returns if the two nodes are equal * Returns if the two nodes are equal.
*
* <p>This method should avoid making calls to {@link Node#equals(Node, NodeEqualityPredicate)}
* with {@code this} as the second argument, directly or otherwise.</p>
* *
* @param o1 the first node * @param o1 the first node
* @param o2 the second node * @param o2 the second node

View File

@ -73,6 +73,6 @@ public enum StandardNodeEquality implements NodeEqualityPredicate {
@Override @Override
public boolean areEqual(@NonNull Node o1, @NonNull Node o2) { public boolean areEqual(@NonNull Node o1, @NonNull Node o2) {
return o1.standardEquals(o2, this); return o1.equals(o2, this);
} }
} }

View File

@ -166,10 +166,12 @@ public final class NodeMap {
} }
private LocalizedNode localise(Node node) { private LocalizedNode localise(Node node) {
if (node instanceof LocalizedNode) { while (node instanceof LocalizedNode) {
LocalizedNode localizedNode = (LocalizedNode) node; LocalizedNode localizedNode = (LocalizedNode) node;
if (this.holder.getObjectName().equals(localizedNode.getLocation())) { if (this.holder.getObjectName().equals(localizedNode.getLocation())) {
return localizedNode; return localizedNode;
} else {
node = localizedNode.getNode();
} }
} }

View File

@ -42,6 +42,16 @@ import java.util.Optional;
public abstract class ForwardingNode implements Node { public abstract class ForwardingNode implements Node {
static ImmutableNode unwrapForwarding(Node node) {
while (node instanceof ForwardingNode) {
node = ((ForwardingNode) node).delegate();
}
if (!(node instanceof ImmutableNode)) {
throw new IllegalArgumentException("Node type cannot be casted to ImmutableNode: " + node.getClass());
}
return ((ImmutableNode) node);
}
public abstract Node delegate(); public abstract Node delegate();
@Override @Override
@ -224,11 +234,6 @@ public abstract class ForwardingNode implements Node {
return delegate().getSuffix(); return delegate().getSuffix();
} }
@Override
public boolean standardEquals(Node other, StandardNodeEquality equalityPredicate) {
return delegate().standardEquals(other, equalityPredicate);
}
@Override @Override
public boolean equals(Node other, NodeEqualityPredicate equalityPredicate) { public boolean equals(Node other, NodeEqualityPredicate equalityPredicate) {
return delegate().equals(other, equalityPredicate); return delegate().equals(other, equalityPredicate);

View File

@ -29,6 +29,7 @@ import com.google.common.collect.ImmutableList;
import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.api.Node; import me.lucko.luckperms.api.Node;
import me.lucko.luckperms.api.NodeEqualityPredicate;
import me.lucko.luckperms.api.StandardNodeEquality; import me.lucko.luckperms.api.StandardNodeEquality;
import me.lucko.luckperms.api.context.ContextSet; import me.lucko.luckperms.api.context.ContextSet;
import me.lucko.luckperms.api.context.ImmutableContextSet; import me.lucko.luckperms.api.context.ImmutableContextSet;
@ -273,24 +274,15 @@ public final class ImmutableNode implements Node {
public boolean equals(Object o) { public boolean equals(Object o) {
if (o == this) return true; if (o == this) return true;
if (!(o instanceof Node)) return false; if (!(o instanceof Node)) return false;
return Equality.EXACT.areEqual(this, ForwardingNode.unwrapForwarding((Node) o));
Node other = (Node) o;
while (other instanceof ForwardingNode) {
other = ((ForwardingNode) other).delegate();
}
return other instanceof ImmutableNode && Equality.EXACT.areEqual(this, (ImmutableNode) other);
} }
@Override @Override
public boolean standardEquals(Node o, StandardNodeEquality equalityPredicate) { public boolean equals(Node o, NodeEqualityPredicate equalityPredicate) {
while (o instanceof ForwardingNode) { if (equalityPredicate instanceof StandardNodeEquality) {
o = ((ForwardingNode) o).delegate(); StandardNodeEquality stdEqualityPredicate = (StandardNodeEquality) equalityPredicate;
} ImmutableNode other = ForwardingNode.unwrapForwarding(o);
if (!(o instanceof ImmutableNode)) { switch (stdEqualityPredicate) {
return false;
}
ImmutableNode other = (ImmutableNode) o;
switch (equalityPredicate) {
case EXACT: case EXACT:
return Equality.EXACT.areEqual(this, other); return Equality.EXACT.areEqual(this, other);
case IGNORE_VALUE: case IGNORE_VALUE:
@ -306,6 +298,9 @@ public final class ImmutableNode implements Node {
} }
} }
return equalityPredicate.areEqual(this, o);
}
@Override @Override
public int hashCode() { public int hashCode() {
return this.hashCode; return this.hashCode;