From d41ad158cdd99450b08b8675e27bcf6f63586965 Mon Sep 17 00:00:00 2001 From: oblonski <4sschroeder@gmail.com> Date: Wed, 15 Jan 2014 21:06:33 -0500 Subject: [PATCH] adds value boundaries for vehicleType as well as vehicleTypeTests --- .../core/problem/vehicle/VehicleTypeImpl.java | 49 +++++++++-- .../problem/vehicle/VehicleTypeImplTest.java | 85 +++++++++++++++++++ 2 files changed, 125 insertions(+), 9 deletions(-) create mode 100644 jsprit-core/src/test/java/jsprit/core/problem/vehicle/VehicleTypeImplTest.java diff --git a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleTypeImpl.java b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleTypeImpl.java index f0404396..e669e7cf 100644 --- a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleTypeImpl.java +++ b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleTypeImpl.java @@ -37,7 +37,7 @@ public class VehicleTypeImpl implements VehicleType { public static VehicleTypeImpl.VehicleCostParams newInstance(double fix, double perTimeUnit,double perDistanceUnit){ return new VehicleCostParams(fix, perTimeUnit, perDistanceUnit); } - + public final double fix; public final double perTimeUnit; public final double perDistanceUnit; @@ -71,9 +71,12 @@ public class VehicleTypeImpl implements VehicleType { * * @param id * @param capacity - * @return + * @return the vehicleType builder + * @throws IllegalStateException if capacity is smaller than zero or id is null */ public static VehicleTypeImpl.Builder newInstance(String id, int capacity){ + if(capacity < 0) throw new IllegalStateException("capacity cannot be smaller than zero"); + if(id == null) throw new IllegalStateException("typeId must be null"); return new Builder(id,capacity); } @@ -104,8 +107,12 @@ public class VehicleTypeImpl implements VehicleType { * * @param inMeterPerSeconds * @return this builder + * @throws IllegalStateException if velocity is smaller than zero */ - public VehicleTypeImpl.Builder setMaxVelocity(double inMeterPerSeconds){ this.maxVelo = inMeterPerSeconds; return this; } + public VehicleTypeImpl.Builder setMaxVelocity(double inMeterPerSeconds){ + if(inMeterPerSeconds < 0.0) throw new IllegalStateException("velocity cannot be smaller than zero"); + this.maxVelo = inMeterPerSeconds; return this; + } /** * Sets the fixed costs of the vehicle-type. @@ -114,8 +121,13 @@ public class VehicleTypeImpl implements VehicleType { * * @param fixedCost * @return this builder + * @throws IllegalStateException if fixedCost is smaller than zero */ - public VehicleTypeImpl.Builder setFixedCost(double fixedCost) { this.fixedCost = fixedCost; return this; } + public VehicleTypeImpl.Builder setFixedCost(double fixedCost) { + if(fixedCost < 0.0) throw new IllegalStateException("fixed costs cannot be smaller than zero"); + this.fixedCost = fixedCost; + return this; + } /** * Sets the cost per distance unit, for instance € per meter. @@ -124,8 +136,13 @@ public class VehicleTypeImpl implements VehicleType { * * @param perDistance * @return this builder + * @throws IllegalStateException if perDistance is smaller than zero */ - public VehicleTypeImpl.Builder setCostPerDistance(double perDistance){ this.perDistance = perDistance; return this; } + public VehicleTypeImpl.Builder setCostPerDistance(double perDistance){ + if(perDistance < 0.0) throw new IllegalStateException("cost per distance must not be smaller than zero"); + this.perDistance = perDistance; + return this; + } /** * Sets cost per time unit, for instance € per second. @@ -133,14 +150,19 @@ public class VehicleTypeImpl implements VehicleType { *

by default it is 0.0 * * @param perTime - * @return + * @return this builder + * @throws IllegalStateException if costPerTime is smaller than zero */ - public VehicleTypeImpl.Builder setCostPerTime(double perTime){ this.perTime = perTime; return this; } + public VehicleTypeImpl.Builder setCostPerTime(double perTime){ + if(perTime < 0.0) throw new IllegalStateException(); + this.perTime = perTime; + return this; + } /** * Builds the vehicle-type. * - * @return + * @return VehicleTypeImpl */ public VehicleTypeImpl build(){ return new VehicleTypeImpl(this); @@ -183,7 +205,7 @@ public class VehicleTypeImpl implements VehicleType { private final VehicleTypeImpl.VehicleCostParams vehicleCostParams; - private double maxVelocity; + private final double maxVelocity; /** * @deprecated use builder instead @@ -205,11 +227,20 @@ public class VehicleTypeImpl implements VehicleType { vehicleCostParams = new VehicleCostParams(builder.fixedCost, builder.perTime, builder.perDistance); } + /** + * @deprecated use Builder.newInstance(...) instead. + * + * @param typeId + * @param capacity + * @param vehicleCostParams + */ + @Deprecated public VehicleTypeImpl(String typeId, int capacity,VehicleTypeImpl.VehicleCostParams vehicleCostParams) { super(); this.typeId = typeId; this.capacity = capacity; this.vehicleCostParams = vehicleCostParams; + this.maxVelocity = Double.MAX_VALUE; } /* (non-Javadoc) diff --git a/jsprit-core/src/test/java/jsprit/core/problem/vehicle/VehicleTypeImplTest.java b/jsprit-core/src/test/java/jsprit/core/problem/vehicle/VehicleTypeImplTest.java new file mode 100644 index 00000000..19c05e50 --- /dev/null +++ b/jsprit-core/src/test/java/jsprit/core/problem/vehicle/VehicleTypeImplTest.java @@ -0,0 +1,85 @@ +package jsprit.core.problem.vehicle; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class VehicleTypeImplTest { + + @Test + public void whenCallingStaticNewBuilderInstance_itShouldReturnNewBuilderInstance(){ + VehicleTypeImpl.Builder builder = VehicleTypeImpl.Builder.newInstance("foo", 0); + assertNotNull(builder); + } + + @Test + public void whenBuildingTypeJustByCallingNewInstance_typeIdMustBeCorrect(){ + VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("foo", 0).build(); + assertEquals("foo",type.getTypeId()); + } + + @Test + public void whenBuildingTypeJustByCallingNewInstance_capMustBeCorrect(){ + VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("foo", 0).build(); + assertEquals(0,type.getCapacity()); + } + + @Test(expected=IllegalStateException.class) + public void whenBuildingTypeWithCapSmallerThanZero_throwIllegalStateException(){ + @SuppressWarnings("unused") + VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("foo", -10).build(); + } + + @Test(expected=IllegalStateException.class) + public void whenBuildingTypeWithNullId_throwIllegalStateException(){ + @SuppressWarnings("unused") + VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance(null, 10).build(); + } + + @Test + public void whenSettingMaxVelocity_itShouldBeSetCorrectly(){ + VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("type", 10).setMaxVelocity(10).build(); + assertEquals(10,type.getMaxVelocity(),0.0); + } + + @Test(expected=IllegalStateException.class) + public void whenMaxVelocitySmallerThanZero_itShouldThrowException(){ + @SuppressWarnings("unused") + VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("type", 10).setMaxVelocity(-10).build(); + } + + @Test(expected=IllegalStateException.class) + public void whenFixedCostsSmallerThanZero_itShouldThrowException(){ + @SuppressWarnings("unused") + VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("type", 10).setFixedCost(-10).build(); + } + + public void whenSettingFixedCosts_itShouldBeSetCorrectly(){ + VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("type", 10).setFixedCost(10).build(); + assertEquals(10.0, type.getVehicleCostParams().fix,0.0); + } + + @Test(expected=IllegalStateException.class) + public void whenPerDistanceCostsSmallerThanZero_itShouldThrowException(){ + @SuppressWarnings("unused") + VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("type", 10).setCostPerDistance(-10).build(); + } + + public void whenSettingPerDistanceCosts_itShouldBeSetCorrectly(){ + VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("type", 10).setCostPerDistance(10).build(); + assertEquals(10.0, type.getVehicleCostParams().perDistanceUnit,0.0); + } + + @Test(expected=IllegalStateException.class) + public void whenPerTimeCostsSmallerThanZero_itShouldThrowException(){ + @SuppressWarnings("unused") + VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("type", 10).setCostPerTime(-10).build(); + } + + public void whenSettingPerTimeCosts_itShouldBeSetCorrectly(){ + VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("type", 10).setCostPerTime(10).build(); + assertEquals(10.0, type.getVehicleCostParams().perDistanceUnit,0.0); + } + + +}