mirror of
https://github.com/correl/mage.git
synced 2024-12-26 03:00:11 +00:00
Included overflow check methods
This commit is contained in:
parent
29c9ce696d
commit
35bbe20b95
1 changed files with 21 additions and 0 deletions
|
@ -3000,4 +3000,25 @@ public abstract class GameImpl implements Game, Serializable {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public 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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public 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