Streams are nice
This commit is contained in:
@@ -21,7 +21,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class AquireItemTask extends QuantizedTaskNode implements IClaimProvider, IQuantizedDependentCostCalculator<IQuantizedChildTaskRelationship, IQuantizedParentTaskRelationship> {
|
||||
public class AquireItemTask extends QuantizedTaskNode implements IClaimProvider, IQuantizedDependentCostCalculator {
|
||||
|
||||
HashMap<IQuantizedChildTaskRelationship, Integer> allocation; // allocation of what tasks have claim over what items in our inventory i guess
|
||||
|
||||
|
||||
@@ -18,28 +18,21 @@
|
||||
package tenor;
|
||||
|
||||
public interface IQuantizedDependentCostCalculator extends IQuantizedTaskNode {
|
||||
|
||||
@Override
|
||||
default IQuantityRelationship cost() {
|
||||
switch (type()) {
|
||||
case SERIAL:
|
||||
case PARALLEL_ALL:
|
||||
return q -> {
|
||||
double sum = 0;
|
||||
for (IQuantizedParentTaskRelationship relationship : childTasks()) {
|
||||
sum += relationship.cost().value(q);
|
||||
}
|
||||
return sum;
|
||||
};
|
||||
return q -> childTasks().stream()
|
||||
.map(IQuantizedParentTaskRelationship::cost)
|
||||
.mapToDouble(relationship -> relationship.value(q))
|
||||
.sum();
|
||||
case ANY_ONE_OF: // TODO this could be smarter about allocating
|
||||
return q -> {
|
||||
double min = -1;
|
||||
for (IQuantizedParentTaskRelationship relationship : childTasks()) {
|
||||
double cost = relationship.cost().value(q);
|
||||
if (min == -1 || cost < min) {
|
||||
min = cost;
|
||||
}
|
||||
}
|
||||
return min;
|
||||
};
|
||||
return q -> childTasks().stream()
|
||||
.map(IQuantizedParentTaskRelationship::cost)
|
||||
.mapToDouble(relationship -> relationship.value(q))
|
||||
.min().orElse(-1);
|
||||
default:
|
||||
throw new UnsupportedOperationException();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user