-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathEntityGrouperResult.java
More file actions
63 lines (51 loc) · 2.02 KB
/
EntityGrouperResult.java
File metadata and controls
63 lines (51 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.froobworld.farmcontrol.group;
import com.froobworld.farmcontrol.controller.entity.SnapshotEntity;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.stream.Collectors;
public class EntityGrouperResult {
private final List<Group> groups;
public EntityGrouperResult(List<Group> groups) {
this.groups = groups;
}
public List<Group> getGroups() {
return groups;
}
public static class Builder {
private final GroupDefinition groupDefinition;
private final List<ProtoGroup> protoGroups = new ArrayList<>();
Builder(GroupDefinition groupDefinition) {
this.groupDefinition = groupDefinition;
}
public void addEntity(SnapshotEntity entity) {
if (!groupDefinition.getTypePredicate().test(entity) || groupDefinition.getExcludeTypePredicate().test(entity) || !groupDefinition.getNamePredicate().test(entity)) {
return;
}
ListIterator<ProtoGroup> iterator = protoGroups.listIterator();
ProtoGroup entityProtoGroup = null;
while (iterator.hasNext()) {
ProtoGroup nextProtoGroup = iterator.next();
if (nextProtoGroup.shouldBeMember(entity)) {
if (entityProtoGroup == null) {
entityProtoGroup = nextProtoGroup;
entityProtoGroup.add(entity);
} else {
entityProtoGroup.merge(nextProtoGroup);
iterator.remove();
}
}
}
if (entityProtoGroup == null) {
protoGroups.add(new ProtoGroup(groupDefinition, entity));
}
}
public EntityGrouperResult build() {
return new EntityGrouperResult(
protoGroups.stream()
.map(ProtoGroup::toGroup)
.collect(Collectors.toList())
);
}
}
}