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

service builder: avoid unnecessary casting

This commit is contained in:
muzuro 2015-09-16 12:12:25 +03:00
parent 842067231d
commit a4a5af6105
6 changed files with 28 additions and 20 deletions

View file

@ -24,7 +24,7 @@ package jsprit.core.problem.job;
*/
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.

View file

@ -24,7 +24,7 @@ package jsprit.core.problem.job;
*/
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.

View file

@ -41,7 +41,7 @@ public class Service extends AbstractJob {
*
* @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
* @return the builder
*/
protected Builder setType(String name) {
protected Builder<T> setType(String name) {
this.type = name;
return this;
}
@ -101,7 +101,7 @@ public class Service extends AbstractJob {
* @param location location
* @return builder
*/
public Builder setLocation(Location location) {
public Builder<T> setLocation(Location location) {
this.location = location;
return this;
}
@ -131,7 +131,7 @@ public class Service extends AbstractJob {
* @return the builder
* @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");
capacityBuilder.addDimension(dimensionIndex, dimensionValue);
return this;
@ -146,32 +146,40 @@ public class Service extends AbstractJob {
* @return builder
* @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");
this.timeWindow = tw;
return this;
}
protected T create() {
if (location == null) throw new IllegalStateException("location is missing");
this.setType("service");
capacity = capacityBuilder.build();
skills = skillBuilder.build();
return (T) new Service(this);
}
/**
* Builds the service.
*
* @return {@link Service}
* @throws IllegalStateException if neither locationId nor coordinate is set.
*/
public Service build() {
public T build() {
if (location == null) throw new IllegalStateException("location is missing");
this.setType("service");
capacity = capacityBuilder.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);
return this;
}
public Builder setName(String name) {
public Builder<T> setName(String name) {
this.name = name;
return this;
}