mirror of
https://github.com/correl/mage.git
synced 2024-12-25 03:00:15 +00:00
Overflow check methods in CardUtil
This commit is contained in:
parent
86dd54e889
commit
ab3128975a
1 changed files with 20 additions and 0 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue