mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
added method to invert capacity
This commit is contained in:
parent
1b3af07e45
commit
1423c90407
2 changed files with 35 additions and 11 deletions
|
|
@ -38,20 +38,34 @@ public class Capacity {
|
|||
* @return new capacity
|
||||
* @throws NullPointerException if one of the args is null
|
||||
* @throws IllegalStateException if number of capacityDimensions of cap1 and cap2 are different (i.e. <code>cap1.getNuOfDimension() != cap2.getNuOfDimension()</code>).
|
||||
* @throws IllegalStateException if one of the capacityDimenstions has a negative value after subtracting
|
||||
*
|
||||
*/
|
||||
public static Capacity subtract(Capacity cap, Capacity cap2substract){
|
||||
if(cap==null || cap2substract==null) throw new NullPointerException("arguments must not be null");
|
||||
Capacity.Builder capacityBuilder= Capacity.Builder.newInstance();
|
||||
for(int i=0;i<Math.max(cap.getNuOfDimensions(),cap2substract.getNuOfDimensions());i++){
|
||||
int dimValue = cap.get(i)-cap2substract.get(i);
|
||||
if(dimValue<0) throw new IllegalStateException("this must not be. dimension " + i + " has a negative value after subtracting");
|
||||
capacityBuilder.addDimension(i, dimValue);
|
||||
}
|
||||
return capacityBuilder.build();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the inverted capacity, i.e. it multiplies all capacity dimensions with -1.
|
||||
*
|
||||
* @param cap
|
||||
* @return inverted capacity
|
||||
* @throws NullPointerException if one of the args is null
|
||||
*/
|
||||
public static Capacity invert(Capacity cap2invert){
|
||||
if(cap2invert==null) throw new NullPointerException("arguments must not be null");
|
||||
Capacity.Builder capacityBuilder= Capacity.Builder.newInstance();
|
||||
for(int i=0;i<cap2invert.getNuOfDimensions();i++){
|
||||
int dimValue = cap2invert.get(i)*-1;
|
||||
capacityBuilder.addDimension(i, dimValue);
|
||||
}
|
||||
return capacityBuilder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes a deep copy of Capacity.
|
||||
|
|
@ -94,13 +108,11 @@ public class Capacity {
|
|||
* <p>Note that it automatically resizes dimensions according to index, i.e. if index=7 there are 8 dimensions.
|
||||
* New dimensions then are initialized with 0
|
||||
*
|
||||
* @throw IllegalStateException if dimValue < 0
|
||||
* @param index
|
||||
* @param dimValue
|
||||
* @return
|
||||
*/
|
||||
public Builder addDimension(int index, int dimValue){
|
||||
if(dimValue<0) throw new IllegalStateException("dimValue can never be negative");
|
||||
if(index < dimensions.length){
|
||||
dimensions[index] = dimValue;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -176,12 +176,13 @@ public class CapacityTest {
|
|||
assertEquals(1, result.get(2));
|
||||
}
|
||||
|
||||
@Test(expected=IllegalStateException.class)
|
||||
public void whenSubtractingTwoCapacitiesWithDifferentNuOfDimensions_itShouldThrowException(){
|
||||
@Test
|
||||
public void whenSubtractingTwoCapacitiesWithDifferentNuOfDimensions_itShouldSubtractCorrectly(){
|
||||
Capacity cap1 = Capacity.Builder.newInstance().addDimension(0, 1).addDimension(1, 2).build();
|
||||
Capacity cap2 = Capacity.Builder.newInstance().addDimension(0, 2).build();
|
||||
@SuppressWarnings("unused")
|
||||
Capacity result = Capacity.subtract(cap2, cap1);
|
||||
assertEquals(1,result.get(0));
|
||||
assertEquals(-2,result.get(1));
|
||||
}
|
||||
|
||||
@Test(expected=NullPointerException.class)
|
||||
|
|
@ -191,12 +192,14 @@ public class CapacityTest {
|
|||
Capacity result = Capacity.subtract(cap1, null);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalStateException.class)
|
||||
public void whenSubtractingBiggerFromLower_itShouldReturnException(){
|
||||
@Test
|
||||
public void whenSubtractingBiggerFromLower_itShouldSubtractCorrectly(){
|
||||
Capacity cap1 = Capacity.Builder.newInstance().addDimension(0, 1).addDimension(1, 2).addDimension(2, 3).build();
|
||||
Capacity cap2 = Capacity.Builder.newInstance().addDimension(0, 2).addDimension(1, 3).addDimension(2, 4).build();
|
||||
@SuppressWarnings("unused")
|
||||
Capacity result = Capacity.subtract(cap1, cap2);
|
||||
assertEquals(-1,result.get(0));
|
||||
assertEquals(-1,result.get(1));
|
||||
assertEquals(-1,result.get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -255,4 +258,13 @@ public class CapacityTest {
|
|||
assertFalse(wheelChair_plus_passenger.isLessOrEqual(cap1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInvertingCap_itShouldBeDoneCorrectly(){
|
||||
Capacity cap = Capacity.Builder.newInstance().addDimension(0, 2).addDimension(1, 3).addDimension(2, 4).build();
|
||||
Capacity inverted = Capacity.invert(cap);
|
||||
assertEquals(-2,inverted.get(0));
|
||||
assertEquals(-3,inverted.get(1));
|
||||
assertEquals(-4,inverted.get(2));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue