Skip to content

Commit

Permalink
[FLINK-13982][core] Introduce arithmetic operations (add, subtract, m…
Browse files Browse the repository at this point in the history
…ultiply) for MemorySize.
  • Loading branch information
xintongsong authored and tillrohrmann committed Oct 14, 2019
1 parent 2f944ba commit 9b14f93
Showing 1 changed file with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.apache.flink.annotation.PublicEvolving;

import java.math.BigDecimal;
import java.util.Locale;

import static org.apache.flink.configuration.MemorySize.MemoryUnit.BYTES;
Expand Down Expand Up @@ -115,6 +116,28 @@ public String toString() {
return bytes + " bytes";
}

// ------------------------------------------------------------------------
// Calculations
// ------------------------------------------------------------------------

public MemorySize add(MemorySize that) {
return new MemorySize(Math.addExact(this.bytes, that.bytes));
}

public MemorySize subtract(MemorySize that) {
return new MemorySize(Math.subtractExact(this.bytes, that.bytes));
}

public MemorySize multiply(double multiplier) {
checkArgument(multiplier >= 0, "multiplier must be >= 0");

BigDecimal product = BigDecimal.valueOf(this.bytes).multiply(BigDecimal.valueOf(multiplier));
if (product.compareTo(BigDecimal.valueOf(Long.MAX_VALUE)) > 0) {
throw new ArithmeticException("long overflow");
}
return new MemorySize(product.longValue());
}

// ------------------------------------------------------------------------
// Parsing
// ------------------------------------------------------------------------
Expand Down

0 comments on commit 9b14f93

Please sign in to comment.