1
0
Fork 0
mirror of https://github.com/graphhopper/jsprit.git synced 2020-01-24 07:45:05 +01:00

Merge pull request #184 from muzuro/master

service builder: avoid unnecessary casting
This commit is contained in:
Stefan Schröder 2015-09-17 08:28:52 +02:00
commit ddb6c8d658
6 changed files with 20 additions and 20 deletions

View file

@ -24,7 +24,7 @@ package jsprit.core.problem.job;
*/ */
public class Delivery extends Service { public class Delivery extends Service {
public static class Builder extends Service.Builder { public static class Builder extends Service.Builder<Delivery> {
/** /**
* Returns a new instance of builder that builds a delivery. * Returns a new instance of builder that builds a delivery.

View file

@ -24,7 +24,7 @@ package jsprit.core.problem.job;
*/ */
public class Pickup extends Service { public class Pickup extends Service {
public static class Builder extends Service.Builder { public static class Builder extends Service.Builder<Pickup> {
/** /**
* Returns a new instance of builder that builds a pickup. * Returns a new instance of builder that builds a pickup.

View file

@ -41,7 +41,7 @@ public class Service extends AbstractJob {
* *
* @author schroeder * @author schroeder
*/ */
public static class Builder { public static class Builder <T extends Service> {
/** /**
@ -90,7 +90,7 @@ public class Service extends AbstractJob {
* @param name the name of service * @param name the name of service
* @return the builder * @return the builder
*/ */
protected Builder setType(String name) { protected Builder<T> setType(String name) {
this.type = name; this.type = name;
return this; return this;
} }
@ -101,7 +101,7 @@ public class Service extends AbstractJob {
* @param location location * @param location location
* @return builder * @return builder
*/ */
public Builder setLocation(Location location) { public Builder<T> setLocation(Location location) {
this.location = location; this.location = location;
return this; return this;
} }
@ -131,7 +131,7 @@ public class Service extends AbstractJob {
* @return the builder * @return the builder
* @throws IllegalArgumentException if dimensionValue < 0 * @throws IllegalArgumentException if dimensionValue < 0
*/ */
public Builder addSizeDimension(int dimensionIndex, int dimensionValue) { public Builder<T> addSizeDimension(int dimensionIndex, int dimensionValue) {
if (dimensionValue < 0) throw new IllegalArgumentException("capacity value cannot be negative"); if (dimensionValue < 0) throw new IllegalArgumentException("capacity value cannot be negative");
capacityBuilder.addDimension(dimensionIndex, dimensionValue); capacityBuilder.addDimension(dimensionIndex, dimensionValue);
return this; return this;
@ -146,7 +146,7 @@ public class Service extends AbstractJob {
* @return builder * @return builder
* @throws IllegalArgumentException if timeWindow is null * @throws IllegalArgumentException if timeWindow is null
*/ */
public Builder setTimeWindow(TimeWindow tw) { public Builder<T> setTimeWindow(TimeWindow tw) {
if (tw == null) throw new IllegalArgumentException("time-window arg must not be null"); if (tw == null) throw new IllegalArgumentException("time-window arg must not be null");
this.timeWindow = tw; this.timeWindow = tw;
return this; return this;
@ -158,20 +158,20 @@ public class Service extends AbstractJob {
* @return {@link Service} * @return {@link Service}
* @throws IllegalStateException if neither locationId nor coordinate is set. * @throws IllegalStateException if neither locationId nor coordinate is set.
*/ */
public Service build() { public T build() {
if (location == null) throw new IllegalStateException("location is missing"); if (location == null) throw new IllegalStateException("location is missing");
this.setType("service"); this.setType("service");
capacity = capacityBuilder.build(); capacity = capacityBuilder.build();
skills = skillBuilder.build(); skills = skillBuilder.build();
return new Service(this); return (T) new Service(this);
} }
public Builder addRequiredSkill(String skill) { public Builder<T> addRequiredSkill(String skill) {
skillBuilder.addSkill(skill); skillBuilder.addSkill(skill);
return this; return this;
} }
public Builder setName(String name) { public Builder<T> setName(String name) {
this.name = name; this.name = name;
return this; return this;
} }

View file

@ -267,7 +267,7 @@ public class RefuseCollection_IT {
/* /*
* build service * build service
*/ */
Pickup service = (Pickup) Pickup.Builder.newInstance(lineTokens[0]).addSizeDimension(0, Integer.parseInt(lineTokens[1])) Pickup service = Pickup.Builder.newInstance(lineTokens[0]).addSizeDimension(0, Integer.parseInt(lineTokens[1]))
.setLocation(Location.newInstance(lineTokens[0])).build(); .setLocation(Location.newInstance(lineTokens[0])).build();
/* /*
* and add it to problem * and add it to problem

View file

@ -62,11 +62,11 @@ public class SimpleDepotBoundedPickupAndDeliveryExample {
/* /*
* build pickups and deliveries at the required locations, each with a capacity-demand of 1. * build pickups and deliveries at the required locations, each with a capacity-demand of 1.
*/ */
Pickup pickup1 = (Pickup) Pickup.Builder.newInstance("1").addSizeDimension(0, 1).setLocation(Location.newInstance(5, 7)).build(); Pickup pickup1 = Pickup.Builder.newInstance("1").addSizeDimension(0, 1).setLocation(Location.newInstance(5, 7)).build();
Delivery delivery1 = (Delivery) Delivery.Builder.newInstance("2").addSizeDimension(0, 1).setLocation(Location.newInstance(5, 13)).build(); Delivery delivery1 = Delivery.Builder.newInstance("2").addSizeDimension(0, 1).setLocation(Location.newInstance(5, 13)).build();
Pickup pickup2 = (Pickup) Pickup.Builder.newInstance("3").addSizeDimension(0, 1).setLocation(Location.newInstance(15, 7)).build(); Pickup pickup2 = Pickup.Builder.newInstance("3").addSizeDimension(0, 1).setLocation(Location.newInstance(15, 7)).build();
Delivery delivery2 = (Delivery) Delivery.Builder.newInstance("4").addSizeDimension(0, 1).setLocation(Location.newInstance(15, 13)).build(); Delivery delivery2 = Delivery.Builder.newInstance("4").addSizeDimension(0, 1).setLocation(Location.newInstance(15, 13)).build();
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();

View file

@ -65,11 +65,11 @@ public class SimpleVRPWithBackhaulsExample {
/* /*
* build pickups and deliveries at the required locations, each with a capacity-demand of 1. * build pickups and deliveries at the required locations, each with a capacity-demand of 1.
*/ */
Pickup pickup1 = (Pickup) Pickup.Builder.newInstance("1").addSizeDimension(0, 1).setLocation(Location.newInstance(5, 7)).build(); Pickup pickup1 = Pickup.Builder.newInstance("1").addSizeDimension(0, 1).setLocation(Location.newInstance(5, 7)).build();
Delivery delivery1 = (Delivery) Delivery.Builder.newInstance("2").addSizeDimension(0, 1).setLocation(Location.newInstance(5, 13)).build(); Delivery delivery1 = Delivery.Builder.newInstance("2").addSizeDimension(0, 1).setLocation(Location.newInstance(5, 13)).build();
Pickup pickup2 = (Pickup) Pickup.Builder.newInstance("3").addSizeDimension(0, 1).setLocation(Location.newInstance(15, 7)).build(); Pickup pickup2 = Pickup.Builder.newInstance("3").addSizeDimension(0, 1).setLocation(Location.newInstance(15, 7)).build();
Delivery delivery2 = (Delivery) Delivery.Builder.newInstance("4").addSizeDimension(0, 1).setLocation(Location.newInstance(15, 13)).build(); Delivery delivery2 = Delivery.Builder.newInstance("4").addSizeDimension(0, 1).setLocation(Location.newInstance(15, 13)).build();
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();