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

generated hashcode and equals in core.problem.vehicle.VehicleImpl such

that two vehicles are equal if they have the same id and if their types
are equal
This commit is contained in:
Stefan Schroeder 2014-06-26 10:58:42 +02:00
parent 2a36ed42cf
commit be5918a67d
3 changed files with 74 additions and 2 deletions

View file

@ -330,5 +330,45 @@ public class VehicleImpl implements Vehicle {
public Coordinate getEndLocationCoordinate() { public Coordinate getEndLocationCoordinate() {
return this.endLocationCoord; return this.endLocationCoord;
} }
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((type == null) ? 0 : type.hashCode());
return result;
}
/**
* Two vehicles are equal if they have the same id and if their types are equal.
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
VehicleImpl other = (VehicleImpl) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (type == null) {
if (other.type != null)
return false;
} else if (!type.equals(other.type))
return false;
return true;
}
} }

View file

@ -171,5 +171,21 @@ public class VehicleImplTest {
assertTrue(true); assertTrue(true);
} }
@Test
public void whenTwoVehiclesHaveTheSameId_theyShouldBeEqual(){
Vehicle v = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setEndLocationId("start").setReturnToDepot(false).build();
Vehicle v2 = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setEndLocationId("start").setReturnToDepot(false).build();
assertTrue(v.equals(v2));
}
@Test
public void whenTwoVehiclesHaveTheSameIdButDiffType_theyShouldNotBeEqual(){
VehicleTypeImpl type1 = VehicleTypeImpl.Builder.newInstance("type").build();
Vehicle v = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setType(type1).setEndLocationId("start").setReturnToDepot(false).build();
PenaltyVehicleType penType = new PenaltyVehicleType(type1);
Vehicle v2 = VehicleImpl.Builder.newInstance("v").setType(penType).setStartLocationId("start").setEndLocationId("start").setReturnToDepot(false).build();
assertTrue(!v.equals(v2));
}
} }

View file

@ -115,9 +115,25 @@ public class VehicleTypeImplTest {
VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("type").setCostPerTime(-10).build(); VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("type").setCostPerTime(-10).build();
} }
@Test
public void whenSettingPerTimeCosts_itShouldBeSetCorrectly(){ public void whenSettingPerTimeCosts_itShouldBeSetCorrectly(){
VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("type").setCostPerTime(10).build(); VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("type").setCostPerTime(10).build();
assertEquals(10.0, type.getVehicleCostParams().perDistanceUnit,0.0); assertEquals(10.0, type.getVehicleCostParams().perTimeUnit,0.0);
}
@Test
public void whenHavingTwoTypesWithTheSameId_theyShouldBeEqual(){
VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("type").setCostPerTime(10).build();
VehicleTypeImpl type2 = VehicleTypeImpl.Builder.newInstance("type").setCostPerTime(10).build();
assertTrue(type.equals(type2));
}
@Test
public void whenHavingTwoTypesWithTheSameIdButDiffClass_theyShouldNotBeEqual(){
VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("type").setCostPerTime(10).build();
VehicleTypeImpl type2 = VehicleTypeImpl.Builder.newInstance("type").setCostPerTime(10).build();
PenaltyVehicleType penType = new PenaltyVehicleType(type2);
assertTrue(!type.equals(penType));
} }