49 lines
1.5 KiB
Java
49 lines
1.5 KiB
Java
package items.requirements;
|
|
|
|
import items.Item;
|
|
import items.Recipe;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
public abstract class Accumulator<E extends Number> implements RequirementAccumulator<E>{
|
|
protected Map<Item, E> inputs;
|
|
|
|
public Accumulator(Map<Item, E> inputs) {
|
|
this.inputs = inputs;
|
|
}
|
|
|
|
protected abstract E calculate(Item i, E subAmount, E amount, Map<Item, E> total);
|
|
|
|
protected abstract E dequeue(Item item, E amount, Map<Item, E> totals);
|
|
|
|
@Override
|
|
public Map<Item, E> accumulate() {
|
|
Map<Item, E> total = new HashMap<>();
|
|
Map<Item, E> queue = new HashMap<>();
|
|
for (Item i : inputs.keySet()) {
|
|
queue.put(i, inputs.get(i));
|
|
}
|
|
while (!queue.isEmpty()) {
|
|
Item i = queue.keySet().iterator().next();
|
|
E amount = queue.remove(i);
|
|
E newTotal = dequeue(i, amount, total);
|
|
total.put(i,newTotal);
|
|
if (i.getRecipes().isEmpty()) {
|
|
continue;
|
|
}
|
|
Recipe r = i.getRecipes().iterator().next();
|
|
Map<Item, E> subRequirements = getRequirements(r, i);
|
|
for (Item subItem : subRequirements.keySet()) {
|
|
E subAmount = subRequirements.get(subItem);
|
|
E newSubTotal = calculate(subItem, subAmount, amount, total);
|
|
total.put(subItem, newSubTotal);
|
|
}
|
|
}
|
|
return total;
|
|
}
|
|
|
|
protected abstract Map<Item,E> getRequirements(Recipe r, Item i);// r.getTotalRequirements()
|
|
|
|
}
|