From ab3128975acb158ec1b3b27e7e115ce73295a858 Mon Sep 17 00:00:00 2001 From: Zzooouhh Date: Sat, 23 Dec 2017 23:01:35 +0100 Subject: [PATCH] Overflow check methods in CardUtil --- Mage/src/main/java/mage/util/CardUtil.java | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Mage/src/main/java/mage/util/CardUtil.java b/Mage/src/main/java/mage/util/CardUtil.java index 301a26e83f..bdc5e2a1b5 100644 --- a/Mage/src/main/java/mage/util/CardUtil.java +++ b/Mage/src/main/java/mage/util/CardUtil.java @@ -509,4 +509,24 @@ public final class CardUtil { } } + public static int addWithOverflowCheck(int base, int increment) { + long result = ((long) base) + increment; + if (result > Integer.MAX_VALUE) { + return Integer.MAX_VALUE; + } else if (result < Integer.MIN_VALUE) { + return Integer.MIN_VALUE; + } + return base + increment; + } + + public static int subtractWithOverflowCheck(int base, int decrement) { + long result = ((long) base) - decrement; + if (result > Integer.MAX_VALUE) { + return Integer.MAX_VALUE; + } else if (result < Integer.MIN_VALUE) { + return Integer.MIN_VALUE; + } + return base - decrement; + } + }