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

add helper to build new vehicle based on another vehicle

This commit is contained in:
oblonski 2018-12-04 10:19:58 +01:00
parent 25a3052a54
commit 9ec84d0e48
No known key found for this signature in database
GPG key ID: 179DE487285680D1
2 changed files with 45 additions and 0 deletions

View file

@ -140,6 +140,24 @@ public class VehicleImpl extends AbstractVehicle {
if (id == null) throw new IllegalArgumentException("Vehicle id must not be null.");
}
/**
* This can be used to initialize the new vehicle (to be built) with another vehicle.
*
* @param baseVehicle
*/
private Builder(Vehicle baseVehicle) {
this.id = baseVehicle.getId();
this.earliestStart = baseVehicle.getEarliestDeparture();
this.latestArrival = baseVehicle.getLatestArrival();
this.returnToDepot = baseVehicle.isReturnToDepot();
this.type = baseVehicle.getType();
this.skills = baseVehicle.getSkills();
this.startLocation = baseVehicle.getStartLocation();
this.endLocation = baseVehicle.getEndLocation();
this.aBreak = baseVehicle.getBreak();
this.userData = baseVehicle.getUserData();
}
/**
* Sets the {@link VehicleType}.<br>
*
@ -291,6 +309,16 @@ public class VehicleImpl extends AbstractVehicle {
return new Builder(vehicleId);
}
/**
* Returns new instance of vehicle builder and initializes every attribute with a attributes of baseVehicle
*
* @param baseVehicle
* @return
*/
public static Builder newInstance(Vehicle baseVehicle) {
return new Builder(baseVehicle);
}
public Builder addSkills(Skills skills) {
this.skillBuilder.addAllSkills(skills.values());
return this;

View file

@ -52,6 +52,23 @@ public class VehicleImplTest {
assertEquals(30., v.getBreak().getServiceDuration(), 0.1);
}
@Test
public void buildingANewVehicleBasedOnAnotherOneShouldWork() {
VehicleTypeImpl type1 = VehicleTypeImpl.Builder.newInstance("type").build();
Break aBreak = Break.Builder.newInstance("break").setTimeWindow(TimeWindow.newInstance(100, 200)).setServiceTime(30).build();
Vehicle v = VehicleImpl.Builder.newInstance("v").setStartLocation(Location.newInstance("start"))
.setType(type1).setEndLocation(Location.newInstance("start"))
.setBreak(aBreak).build();
Vehicle newVehicle = VehicleImpl.Builder.newInstance(v).setStartLocation(Location.newInstance("newStartLocation")).build();
assertNotNull(newVehicle.getBreak());
assertEquals(100., newVehicle.getBreak().getTimeWindow().getStart(), 0.1);
assertEquals(200., newVehicle.getBreak().getTimeWindow().getEnd(), 0.1);
assertEquals(30., newVehicle.getBreak().getServiceDuration(), 0.1);
assertEquals("newStartLocation", newVehicle.getStartLocation().getId());
assertEquals(type1, newVehicle.getType());
}
@Test
public void whenAddingSkills_theyShouldBeAddedCorrectly() {