Skip to content

Commit

Permalink
fix: adding Quantity methods
Browse files Browse the repository at this point in the history
closes: fabric8io#5357

Signed-off-by: Steve Hawkins <shawkins@redhat.com>
  • Loading branch information
shawkins committed Jan 21, 2024
1 parent 08e4fa3 commit 41e5b75
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#### Improvements
* Fix #5701: Owner reference validity check regarding scope and namespace
* Fix #5353: added KubernetesClientBuilder.editOrNewConfig
* Fix #5357: adding additional Quantity methods

#### Dependency Upgrade

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.math.MathContext;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;

/**
* Quantity is fixed point representation of a number.
Expand All @@ -54,7 +55,7 @@
""
})
@Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = false, builderPackage = "io.fabric8.kubernetes.api.builder")
public class Quantity implements Serializable {
public class Quantity implements Serializable, Comparable<Quantity> {
private String amount;
private String format = "";
@JsonIgnore
Expand Down Expand Up @@ -261,8 +262,15 @@ public boolean equals(Object o) {
}

Quantity quantity = (Quantity) o;
return getAmountInBytes(this)
.compareTo(getAmountInBytes(quantity)) == 0;
return this.compareTo(quantity) == 0;
}

/**
* Compares the numerical amounts of these quantities.
*/
@Override
public int compareTo(Quantity o) {
return getNumericalAmount().compareTo(o.getNumericalAmount());
}

@Override
Expand Down Expand Up @@ -377,4 +385,34 @@ public Quantity deserialize(JsonParser jsonParser, DeserializationContext ctxt)

}

/**
* Add the provided quantity to the current value. If the current value is zero, the format of the quantity will be the format
* of y.
* @param y to add
* @return a new Quantity after y has been added
*/
public Quantity add(Quantity y) {
return op(y, BigDecimal::add);
}

/**
* Subtract the provided quantity from the current value. If the current value is zero, the format of the quantity will be the format
* of y.
* @param y to subtract
* @return a new Quantity after y has been subtracted
*/
public Quantity subtract(Quantity y) {
return op(y, BigDecimal::subtract);
}

Quantity op(Quantity y, BiFunction<BigDecimal, BigDecimal, BigDecimal> func) {
BigDecimal numericalAmount = this.getNumericalAmount();
numericalAmount = func.apply(numericalAmount, y.getNumericalAmount());
String format = this.format;
if (numericalAmount.signum() == 0) {
format = y.format;
}
return fromNumericalAmount(numericalAmount, format);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,14 @@ void testFromAmountInBytes(String amount) {
Quantity quantity = new Quantity(amount);
assertThat(quantity).isEqualTo(Quantity.fromNumericalAmount(quantity.getNumericalAmount(), quantity.getFormat()));
}

void testAdd() {
Quantity quantity = new Quantity("7Mi");
assertThat(quantity.add(new Quantity("6Mi"))).isEqualTo(new Quantity("1Mi"));
}

void testSubtract(String amount) {
Quantity quantity = new Quantity("0Mi");
assertThat(quantity.subtract(new Quantity("1Ki"))).isEqualTo(new Quantity("-1Ki"));
}
}

0 comments on commit 41e5b75

Please sign in to comment.