-
Notifications
You must be signed in to change notification settings - Fork 497
/
TravelerSet.java
144 lines (121 loc) · 3.07 KB
/
TravelerSet.java
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.transport;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import com.google.common.collect.BiMap;
import com.google.common.collect.ForwardingSet;
import com.google.common.collect.HashBiMap;
public class TravelerSet extends ForwardingSet<TravelingItem> {
public boolean iterating;
private final BiMap<Integer, TravelingItem> items = HashBiMap.create();
private final Set<TravelingItem> toLoad = new HashSet<TravelingItem>();
private final Set<TravelingItem> toAdd = new HashSet<TravelingItem>();
private final Set<TravelingItem> toRemove = new HashSet<TravelingItem>();
private int delay = 0;
private final PipeTransportItems transport;
public TravelerSet(PipeTransportItems transport) {
this.transport = transport;
}
@Override
protected Set<TravelingItem> delegate() {
return items.values();
}
@Override
public boolean add(TravelingItem item) {
if (iterating) {
return toAdd.add(item);
}
if (items.containsValue(item)) {
return false;
}
item.setContainer(transport.container);
items.put(item.id, item);
return true;
}
@Override
public boolean addAll(Collection<? extends TravelingItem> collection) {
if (iterating) {
return toAdd.addAll(collection);
}
return standardAddAll(collection);
}
@Override
public boolean remove(Object object) {
if (iterating) {
return toRemove.add((TravelingItem) object);
}
return delegate().remove(object);
}
@Override
public boolean removeAll(Collection<?> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean retainAll(Collection<?> collection) {
throw new UnsupportedOperationException();
}
public TravelingItem get(int id) {
return items.get(id);
}
void scheduleLoad(TravelingItem item) {
delay = 10;
toLoad.add(item);
}
private void loadScheduledItems() {
if (delay > 0) {
delay--;
return;
}
addAll(toLoad);
toLoad.clear();
}
private void addScheduledItems() {
addAll(toAdd);
toAdd.clear();
}
public boolean scheduleRemoval(TravelingItem item) {
return toRemove.add(item);
}
public boolean unscheduleRemoval(TravelingItem item) {
return toRemove.remove(item);
}
void removeScheduledItems() {
items.values().removeAll(toRemove);
toRemove.clear();
}
void purgeCorruptedItems() {
Iterator<TravelingItem> it = items.values().iterator();
while (it.hasNext()) {
TravelingItem item = it.next();
if (item.isCorrupted()) {
it.remove();
}
}
}
void flush() {
loadScheduledItems();
addScheduledItems();
removeScheduledItems();
}
@Override
public Iterator<TravelingItem> iterator() {
return items.values().iterator();
}
@Override
public void clear() {
if (iterating) {
toRemove.addAll(this);
} else {
items.clear();
}
}
}