Skip to content

Commit

Permalink
Initial ListCondition implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
MattSturgeon committed Apr 2, 2023
1 parent 6c9884c commit bfc0910
Showing 1 changed file with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package me.shedaniel.clothconfig2.impl.dependencies.conditions;

import net.minecraft.network.chat.Component;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class ListCondition<T> extends FlaggedCondition<List<T>> {

private final ListRequirement requirement;
private final Collection<T> values;

public ListCondition(ListRequirement requirement, T value){
this(requirement, Collections.singletonList(value));
}

public ListCondition(ListRequirement requirement, Collection<T> values){
this.requirement = requirement;
this.values = values;
}

@Override
public boolean check(List<T> values) {
return inverted() != this.requirement.check(values, this.values);
}

@Override
public Component getText(boolean inverted) {
return null;
}

public enum ListRequirement {
CONTAINS_ANY,
NOT_CONTAINS_ANY,
CONTAINS_ALL,
NOT_CONTAINS_ALL,
MATCHES,
NOT_MATCHES;

/**
* Checks whether {@code collection1} meets this requirement for {@code collection2}
*
* <ul>
* <li><em>{@link #CONTAINS_ANY}</em> - true if {@code collection1} contains anything in {@code collection2}</li>
* <li><em>{@link #NOT_CONTAINS_ANY}</em> - true if {@code collection1} doesn't contain anything in {@code collection2}</li>
* <li><em>{@link #CONTAINS_ALL}</em> - true if {@code collection1} contains everything in {@code collection2}</li>
* <li><em>{@link #NOT_CONTAINS_ALL}</em> - true if {@code collection1} doesn't contain everything in {@code collection2}</li>
* <li><em>{@link #MATCHES}</em> - true if {@code collection1}'s content exactly matches {@code collection2}'s</li>
* <li><em>{@link #NOT_MATCHES}</em> - true if {@code collection1}'s content is different to {@code collection2}'s</li>
* </ul>
*/
public <T> boolean check(Collection<T> collection1, Collection<T> collection2) {
return switch (this) {
case CONTAINS_ANY -> collection2.stream().anyMatch(collection1::contains);
case NOT_CONTAINS_ANY -> collection2.stream().noneMatch(collection1::contains);
case CONTAINS_ALL -> collection1.containsAll(collection2);
case NOT_CONTAINS_ALL -> collection2.stream().anyMatch(value -> !collection1.contains(value));
case MATCHES -> collection2.size() == collection1.size() && collection2.containsAll(collection1);
case NOT_MATCHES -> collection2.size() != collection1.size()
|| collection2.stream().anyMatch(value -> !collection1.contains(value));
};
}
}
}

0 comments on commit bfc0910

Please sign in to comment.