Search in sources :

Example 1 with ImmutableContextSetImpl

use of me.lucko.luckperms.common.context.ImmutableContextSetImpl in project LuckPerms by lucko.

the class ContextSetComparator method compare.

@Override
public int compare(ImmutableContextSet o1, ImmutableContextSet o2) {
    if (o1.equals(o2)) {
        return 0;
    }
    // compare presence of any context (non-empty)
    int result = Boolean.compare(!o1.isEmpty(), !o2.isEmpty());
    if (result != 0) {
        return result;
    }
    // compare presence of a server context
    result = Boolean.compare(o1.containsKey(DefaultContextKeys.SERVER_KEY), o2.containsKey(DefaultContextKeys.SERVER_KEY));
    if (result != 0) {
        return result;
    }
    // compare presence of a world context
    result = Boolean.compare(o1.containsKey(DefaultContextKeys.WORLD_KEY), o2.containsKey(DefaultContextKeys.WORLD_KEY));
    if (result != 0) {
        return result;
    }
    // compare overall size
    result = Integer.compare(o1.size(), o2.size());
    if (result != 0) {
        return result;
    }
    // At this point, we don't really care about the order between the two sets.
    // However, we *have* to maintain transitivity in this comparator (despite how
    // expensive/complex it may be) as it is used in the PermissionHolder nodes treemap.
    // in order to have consistent ordering, we have to compare the content of the context sets.
    // to do this, obtain sorted array representations of each set, then compare which is greater
    Context[] o1Array = o1 instanceof ImmutableContextSetImpl ? ((ImmutableContextSetImpl) o1).toArray() : toArray(o1);
    Context[] o2Array = o2 instanceof ImmutableContextSetImpl ? ((ImmutableContextSetImpl) o2).toArray() : toArray(o2);
    for (int i = 0; i < o1Array.length; i++) {
        Context ent1 = o1Array[i];
        Context ent2 = o2Array[i];
        result = ContextComparator.INSTANCE.compare(ent1, ent2);
        if (result != 0) {
            return result;
        }
    }
    throw new AssertionError("sets are equal? " + o1 + " - " + o2);
}
Also used : Context(net.luckperms.api.context.Context) ImmutableContextSetImpl(me.lucko.luckperms.common.context.ImmutableContextSetImpl)

Aggregations

ImmutableContextSetImpl (me.lucko.luckperms.common.context.ImmutableContextSetImpl)1 Context (net.luckperms.api.context.Context)1