-
Notifications
You must be signed in to change notification settings - Fork 497
/
TravelerSet.java
130 lines (109 loc) · 2.75 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
/*
* Copyright (c) SpaceToad, 2011-2012
* 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 com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/**
*
* @author CovertJaguar <http://www.railcraft.info/>
*/
public class TravelerSet implements Iterable<TravelingItem> {
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;
}
private boolean add(TravelingItem item) {
if (items.containsValue(item))
return false;
item.setContainer(transport.container);
items.put(item.id, item);
return true;
}
private boolean addAll(Collection<? extends TravelingItem> collection) {
boolean changed = false;
for (TravelingItem item : collection) {
changed |= add(item);
}
return changed;
}
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();
}
public void scheduleAdd(TravelingItem item) {
toAdd.add(item);
}
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 purgeBadItems() {
Iterator<TravelingItem> it = items.values().iterator();
while (it.hasNext()) {
TravelingItem item = it.next();
if (item.isCorrupted()) {
it.remove();
continue;
}
if (item.getContainer() != transport.container) {
it.remove();
continue;
}
}
}
void flush() {
loadScheduledItems();
addScheduledItems();
removeScheduledItems();
purgeBadItems();
}
@Override
public Iterator<TravelingItem> iterator() {
return items.values().iterator();
}
public int size() {
return items.values().size();
}
void clear() {
toRemove.addAll(items.values());
}
public boolean isEmpty() {
return items.isEmpty();
}
}