-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial ListCondition implementation
- Loading branch information
1 parent
6c9884c
commit bfc0910
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
...n/src/main/java/me/shedaniel/clothconfig2/impl/dependencies/conditions/ListCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}; | ||
} | ||
} | ||
} |