CollectionContentsPredicate .java

net.minecraft.advancements.predicates.CollectionContentsPredicate (interface, 76 lines): members: Multiple, Zero, Single, unpack, codec. imports: Codec.

net.minecraft.advancements.predicatesinterface76 linescommonextends Predicate<T>>2.0 KB

API Surface (6)

boolean test(final Iterable<? extends T> values)
List<P> unpack()
boolean test(final Iterable<? extends T> values)
List<P> unpack()
boolean test(final Iterable<? extends T> values)
List<P> unpack()

Full Source

CollectionContentsPredicate.java76 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.advancements.predicates;
2
3
import com.mojang.serialization.Codec;
4
import java.util.ArrayList;
5
import java.util.List;
6
import java.util.function.Predicate;
7
8
public interface CollectionContentsPredicate<T, P extends Predicate<T>> extends Predicate<Iterable<? extends T>> {
9
	List<P> unpack();
10
11
	static <T, P extends Predicate<T>> Codec<CollectionContentsPredicate<T, P>> codec(final Codec<P> elementCodec) {
12
		return elementCodec.listOf().xmap(CollectionContentsPredicate::of, CollectionContentsPredicate::unpack);
13
	}
14
15
	@SafeVarargs
16
	static <T, P extends Predicate<T>> CollectionContentsPredicate<T, P> of(final P... predicates) {
17
		return of(List.of(predicates));
18
	}
19
20
	static <T, P extends Predicate<T>> CollectionContentsPredicate<T, P> of(final List<P> predicates) {
21
		return switch (predicates.size()) {
22
			case 0 -> new CollectionContentsPredicate.Zero();
23
			case 1 -> new CollectionContentsPredicate.Single(predicates.getFirst());
24
			default -> new CollectionContentsPredicate.Multiple(predicates);
25
		};
26
	}
27
28
	record Multiple<T, P extends Predicate<T>>(List<P> tests) implements CollectionContentsPredicate<T, P> {
29
		public boolean test(final Iterable<? extends T> values) {
30
			List<Predicate<T>> testsToMatch = new ArrayList<>(this.tests);
31
32
			for (T value : values) {
33
				testsToMatch.removeIf(p -> p.test(value));
34
				if (testsToMatch.isEmpty()) {
35
					return true;
36
				}
37
			}
38
39
			return false;
40
		}
41
42
		@Override
43
		public List<P> unpack() {
44
			return this.tests;
45
		}
46
	}
47
48
	record Single<T, P extends Predicate<T>>(P test) implements CollectionContentsPredicate<T, P> {
49
		public boolean test(final Iterable<? extends T> values) {
50
			for (T value : values) {
51
				if (this.test.test(value)) {
52
					return true;
53
				}
54
			}
55
56
			return false;
57
		}
58
59
		@Override
60
		public List<P> unpack() {
61
			return List.of(this.test);
62
		}
63
	}
64
65
	class Zero<T, P extends Predicate<T>> implements CollectionContentsPredicate<T, P> {
66
		public boolean test(final Iterable<? extends T> values) {
67
			return true;
68
		}
69
70
		@Override
71
		public List<P> unpack() {
72
			return List.of();
73
		}
74
	}
75
}
76

Decompiled Minecraft 26.3 source of CollectionContentsPredicate (net/minecraft/advancements/predicates/CollectionContentsPredicate.java) with Mojang official mappings, stored verbatim on the site.