i love generics

This commit is contained in:
Leijurv
2018-10-30 15:44:52 -07:00
parent 473f872d2f
commit 5e2ccdac08
18 changed files with 75 additions and 32 deletions
+4 -6
View File
@@ -17,8 +17,6 @@
package tenor; package tenor;
import java.util.List;
public class AquireCraftingItems extends QuantizedTaskNode implements IClaimProvider { public class AquireCraftingItems extends QuantizedTaskNode implements IClaimProvider {
final CraftingTask parent; final CraftingTask parent;
@@ -51,11 +49,11 @@ public class AquireCraftingItems extends QuantizedTaskNode implements IClaimProv
return x -> { return x -> {
// cost to get x copies of these items // cost to get x copies of these items
double sum = 0; double sum = 0;
for (QuantizedToQuantizedTaskRelationship resource : (List<QuantizedToQuantizedTaskRelationship>) (Object) childTasks()) { for (IQuantizedParentTaskRelationship resource : childTasks()) {
int amountPerCraft = parent.inputSizeFor((AquireItemTask) resource.childTask()); int amountPerCraft = parent.inputSizeFor((AquireItemTask) resource.childTask());
int totalAmountNeeded = x * amountPerCraft; int totalAmountNeeded = x * amountPerCraft;
int amountForUs = resource.quantityCompleted(); int amountForUs = ((IQuantizedChildTaskRelationship) resource).quantityCompleted();
totalAmountNeeded -= amountForUs; totalAmountNeeded -= amountForUs;
if (totalAmountNeeded <= 0) { if (totalAmountNeeded <= 0) {
@@ -75,8 +73,8 @@ public class AquireCraftingItems extends QuantizedTaskNode implements IClaimProv
} }
// our only parent is the crafting task // our only parent is the crafting task
int minCompletion = Integer.MAX_VALUE; int minCompletion = Integer.MAX_VALUE;
for (IQuantizedChildTaskRelationship resource : (List<IQuantizedChildTaskRelationship>) (Object) childTasks()) { for (IQuantizedParentTaskRelationship resource : childTasks()) {
int amountForUs = resource.quantityCompleted(); int amountForUs = ((IQuantizedChildTaskRelationship) resource).quantityCompleted();
int amountPerCraft = parent.inputSizeFor((AquireItemTask) resource.childTask()); int amountPerCraft = parent.inputSizeFor((AquireItemTask) resource.childTask());
+1 -1
View File
@@ -21,7 +21,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public class AquireItemTask extends QuantizedTaskNode implements IClaimProvider, IQuantizedDependentCostCalculator<IQuantizedChildTaskRelationship> { public class AquireItemTask extends QuantizedTaskNode implements IClaimProvider, IQuantizedDependentCostCalculator<IQuantizedChildTaskRelationship, IQuantizedParentTaskRelationship> {
HashMap<IQuantizedChildTaskRelationship, Integer> allocation; // allocation of what tasks have claim over what items in our inventory i guess HashMap<IQuantizedChildTaskRelationship, Integer> allocation; // allocation of what tasks have claim over what items in our inventory i guess
@@ -0,0 +1,21 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package tenor;
public interface IChildTaskRelationship {
}
@@ -0,0 +1,21 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package tenor;
public interface IParentTaskRelationship {
}
@@ -17,7 +17,7 @@
package tenor; package tenor;
public interface IQuantizedChildTaskRelationship<T extends ITaskNodeBase> extends ITaskRelationshipBase<T, IQuantizedTask> { public interface IQuantizedChildTaskRelationship<T extends ITaskNodeBase> extends ITaskRelationshipBase<T, IQuantizedTask>, IChildTaskRelationship {
double allocatedPriority(int quantity); double allocatedPriority(int quantity);
@@ -17,23 +17,23 @@
package tenor; package tenor;
public interface IQuantizedDependentCostCalculator<T extends ITaskRelationshipBase> extends ITaskNodeBase<T> { public interface IQuantizedDependentCostCalculator extends IQuantizedTaskNode {
default IQuantityRelationship cost() { default IQuantityRelationship cost() {
switch (type()) { switch (type()) {
case SERIAL: case SERIAL:
case PARALLEL_ALL: case PARALLEL_ALL:
return q -> { return q -> {
double sum = 0; double sum = 0;
for (ITaskRelationshipBase relationship : childTasks()) { for (IQuantizedParentTaskRelationship relationship : childTasks()) {
sum += ((IQuantizedParentTaskRelationship) relationship).cost().value(q); sum += relationship.cost().value(q);
} }
return sum; return sum;
}; };
case ANY_ONE_OF: // TODO this could be smarter about allocating case ANY_ONE_OF: // TODO this could be smarter about allocating
return q -> { return q -> {
double min = -1; double min = -1;
for (ITaskRelationshipBase relationship : childTasks()) { for (IQuantizedParentTaskRelationship relationship : childTasks()) {
double cost = ((IQuantizedParentTaskRelationship) relationship).cost().value(q); double cost = relationship.cost().value(q);
if (min == -1 || cost < min) { if (min == -1 || cost < min) {
min = cost; min = cost;
} }
@@ -17,7 +17,7 @@
package tenor; package tenor;
public interface IQuantizedParentTaskRelationship<T extends ITask> extends ITaskRelationshipBase<QuantizedTaskNode, T> { public interface IQuantizedParentTaskRelationship<T extends ITask> extends ITaskRelationshipBase<QuantizedTaskNode, T>, IParentTaskRelationship {
IQuantityRelationship cost(); IQuantityRelationship cost();
} }
+1 -1
View File
@@ -21,6 +21,6 @@ package tenor;
* @author Brady * @author Brady
* @since 10/30/2018 * @since 10/30/2018
*/ */
public interface IQuantizedTaskNode extends ITaskNodeBase<IQuantizedChildTaskRelationship>, IQuantizedTask { public interface IQuantizedTaskNode extends ITaskNodeBase<IQuantizedChildTaskRelationship, IQuantizedParentTaskRelationship>, IQuantizedTask {
} }
@@ -17,7 +17,7 @@
package tenor; package tenor;
public interface ISingularChildTaskRelationship<T extends ITaskNodeBase> extends ITaskRelationshipBase<T, ISingularTask> { public interface ISingularChildTaskRelationship<T extends ITaskNodeBase> extends ITaskRelationshipBase<T, ISingularTask>, IChildTaskRelationship {
double allocatedPriority(); double allocatedPriority();
} }
@@ -17,7 +17,7 @@
package tenor; package tenor;
public interface ISingularParentTaskRelationship<T extends ITask> extends ITaskRelationshipBase<SingularTaskNode, T> { public interface ISingularParentTaskRelationship<T extends ITask> extends ITaskRelationshipBase<SingularTaskNode, T>, IParentTaskRelationship {
double cost(); double cost();
} }
+1 -1
View File
@@ -21,6 +21,6 @@ package tenor;
* @author Brady * @author Brady
* @since 10/30/2018 * @since 10/30/2018
*/ */
public interface ISingularTaskNode extends ITaskNodeBase<ISingularChildTaskRelationship>, ISingularTask { public interface ISingularTaskNode extends ITaskNodeBase<ISingularChildTaskRelationship, ISingularParentTaskRelationship>, ISingularTask {
double priorityAllocatedToChild(ISingularParentTaskRelationship relationship); double priorityAllocatedToChild(ISingularParentTaskRelationship relationship);
} }
+1 -1
View File
@@ -19,7 +19,7 @@ package tenor;
import java.util.List; import java.util.List;
public interface ITask<T extends ITaskRelationshipBase> { public interface ITask<T extends IChildTaskRelationship & ITaskRelationshipBase> {
List<T> parentTasks(); List<T> parentTasks();
+3 -3
View File
@@ -19,11 +19,11 @@ package tenor;
import java.util.List; import java.util.List;
public interface ITaskNodeBase<T extends ITaskRelationshipBase> extends ITask<T> { public interface ITaskNodeBase<T extends IChildTaskRelationship & ITaskRelationshipBase, S extends IParentTaskRelationship & ITaskRelationshipBase> extends ITask<T> {
List<ITaskRelationshipBase> childTasks(); List<S> childTasks();
DependencyType type(); DependencyType type();
void addChild(ITaskRelationshipBase relationship); void addChild(S relationship);
} }
+2 -2
View File
@@ -17,7 +17,7 @@
package tenor; package tenor;
public abstract class QuantizedTaskNode extends TaskNode<IQuantizedChildTaskRelationship> implements IQuantizedTask { public abstract class QuantizedTaskNode extends TaskNode<IQuantizedChildTaskRelationship, IQuantizedParentTaskRelationship> implements IQuantizedTask {
public QuantizedTaskNode(DependencyType type) { public QuantizedTaskNode(DependencyType type) {
super(type); super(type);
@@ -37,7 +37,7 @@ public abstract class QuantizedTaskNode extends TaskNode<IQuantizedChildTaskRela
} }
int minQuantity = -1; int minQuantity = -1;
for (int i = 0; i < childTasks().indexOf(child); i++) { for (int i = 0; i < childTasks().indexOf(child); i++) {
QuantizedToQuantizedTaskRelationship relationship = (QuantizedToQuantizedTaskRelationship) childTasks().get(i); IQuantizedChildTaskRelationship relationship = (IQuantizedChildTaskRelationship) childTasks().get(i);
IClaimProvider claim = (IClaimProvider) relationship.childTask(); IClaimProvider claim = (IClaimProvider) relationship.childTask();
int amt = claim.quantityCompletedForParent(relationship); int amt = claim.quantityCompletedForParent(relationship);
if (minQuantity == -1 || amt < minQuantity) { if (minQuantity == -1 || amt < minQuantity) {
+1 -1
View File
@@ -17,7 +17,7 @@
package tenor; package tenor;
public abstract class SingularTaskNode extends TaskNode<ISingularChildTaskRelationship> implements ISingularTaskNode { public abstract class SingularTaskNode extends TaskNode<ISingularChildTaskRelationship, ISingularParentTaskRelationship> implements ISingularTaskNode {
public SingularTaskNode(DependencyType type) { public SingularTaskNode(DependencyType type) {
super(type); super(type);
+2 -2
View File
@@ -19,7 +19,7 @@ package tenor;
import java.util.List; import java.util.List;
public abstract class Task<T extends ITaskRelationshipBase> implements ITask<T> { public abstract class Task<T extends IChildTaskRelationship & ITaskRelationshipBase> implements ITask<T> {
List<T> parentRelationships; List<T> parentRelationships;
@@ -33,7 +33,7 @@ public abstract class Task<T extends ITaskRelationshipBase> implements ITask<T>
if (relationship.childTask() != this) { if (relationship.childTask() != this) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
relationship.parentTask().addChild(relationship); relationship.parentTask().addChild((IParentTaskRelationship) relationship);
parentRelationships.add(relationship); parentRelationships.add(relationship);
} }
} }
+1 -1
View File
@@ -17,5 +17,5 @@
package tenor; package tenor;
public abstract class TaskLeaf<T extends ITaskRelationshipBase> extends Task<T> { public abstract class TaskLeaf<T extends IChildTaskRelationship & ITaskRelationshipBase> extends Task<T> {
} }
+7 -4
View File
@@ -19,24 +19,27 @@ package tenor;
import java.util.List; import java.util.List;
public abstract class TaskNode<T extends ITaskRelationshipBase> extends Task<T> implements ITaskNodeBase<T> { public abstract class TaskNode<T extends IChildTaskRelationship & ITaskRelationshipBase, S extends IParentTaskRelationship & ITaskRelationshipBase> extends Task<T> implements ITaskNodeBase<T, S> {
List<ITaskRelationshipBase> childRelationships; List<S> childRelationships;
public final DependencyType type; public final DependencyType type;
public TaskNode(DependencyType type) { public TaskNode(DependencyType type) {
this.type = type; this.type = type;
} }
public List<ITaskRelationshipBase> childTasks() { @Override
public List<S> childTasks() {
return childRelationships; return childRelationships;
} }
@Override
public DependencyType type() { public DependencyType type() {
return type; return type;
} }
public void addChild(ITaskRelationshipBase relationship) { @Override
public void addChild(S relationship) {
if (relationship.parentTask() != this) { if (relationship.parentTask() != this) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }