List of usage examples for com.google.common.math IntMath checkedAdd
public static int checkedAdd(int a, int b)
From source file:com.forerunnergames.tools.common.Maths.java
/** * Calculates the next higher multiple of the specified integer. * * For example, the next higher multiple of 4 from 10 is 12. * * @param n/*from w w w . ja va2 s . com*/ * The number to obtain the next higher multiple of, must be >= 0. * @param multiple * The desired multiple that n must be evenly divisible by, must be > 0. */ public static int nextHigherMultiple(final int n, final int multiple) { Arguments.checkIsNotNegative(n, "n"); Arguments.checkLowerInclusiveBound(multiple, 1, "multiple"); return IntMath.checkedMultiply(((IntMath.checkedAdd(n, (multiple - 1))) / multiple), multiple); }
From source file:com.tkmtwo.timex.WallClock.java
private static int sumFields(int hrs, int mins, int secs) { int sumSecs = 0; sumSecs = IntMath.checkedAdd(sumSecs, IntMath.checkedMultiply(hrs, SECONDS_PER_HOUR)); sumSecs = IntMath.checkedAdd(sumSecs, IntMath.checkedMultiply(mins, SECONDS_PER_MINUTE)); sumSecs = IntMath.checkedAdd(sumSecs, secs); return sumSecs; }
From source file:com.wandrell.tabletop.stats.controller.DefaultValueController.java
@Override public final void increaseValue() { Integer value;//from w w w. ja v a 2s.c o m if (getValue() != null) { try { value = IntMath.checkedAdd(getValue().getValue(), getStep()); } catch (final ArithmeticException exception) { value = Integer.MAX_VALUE; } if (value <= getUpperLimit()) { getValue().setValue(value); } } }
From source file:com.jivesoftware.os.lab.io.AppendableHeap.java
static public byte[] grow(byte[] src, int amount) { if (src == null) { return new byte[amount]; }/*from ww w . ja v a 2s. com*/ try { byte[] newSrc = new byte[IntMath.checkedAdd(src.length, amount)]; System.arraycopy(src, 0, newSrc, 0, src.length); return newSrc; } catch (ArithmeticException x) { System.out.println(src.length + " + " + amount + " overflows an int!"); throw x; } }
From source file:com.wandrell.tabletop.stats.controller.DefaultValueController.java
@Override public final Boolean isAbleToIncrease() { final Boolean result; Integer value;//from ww w .j ava 2s . com if (getValue() == null) { result = false; } else { try { value = IntMath.checkedAdd(getValue().getValue(), getStep()); } catch (final ArithmeticException exception) { value = Integer.MAX_VALUE; } result = (value <= getUpperLimit()); } return result; }
From source file:com.jivesoftware.os.amza.api.filer.HeapFiler.java
static final public byte[] grow(byte[] src, int amount) { if (amount < 0) { throw new IllegalArgumentException("amount must be greater than zero"); }//from w w w .j a va 2s. c o m if (src == null) { return new byte[amount]; } byte[] newSrc = new byte[IntMath.checkedAdd(src.length, amount)]; System.arraycopy(src, 0, newSrc, 0, src.length); return newSrc; }
From source file:edu.mit.streamjit.test.Datasets.java
public static <I> Input<I> nCopies(final int n, final Input<I> input) { //0 would be valid (an empty input), but would usually be a bug. checkArgument(n > 0, "%s must be nonnegative", n); if (n == 1)/*from w ww . j a va 2s. c o m*/ return input; return InputBufferFactory.wrap(new InputBufferFactory() { @Override public Buffer createReadableBuffer(final int readerMinSize) { final Buffer firstBuffer = InputBufferFactory.unwrap(input).createReadableBuffer(readerMinSize); if (firstBuffer instanceof PeekableBuffer) return new LoopedBufferBuffer(n, (PeekableBuffer) firstBuffer); return new AbstractReadOnlyBuffer() { private Buffer currentBuffer = firstBuffer; private final int bufferSize = currentBuffer.size(); private int copiesRemaining = n - 1; @Override public Object read() { if (currentBuffer.size() == 0 && copiesRemaining > 0) { currentBuffer = InputBufferFactory.unwrap(input).createReadableBuffer(readerMinSize); --copiesRemaining; } return currentBuffer.read(); } @Override public int size() { try { return IntMath.checkedAdd(currentBuffer.size(), IntMath.checkedMultiply(copiesRemaining, bufferSize)); } catch (ArithmeticException overflow) { return Integer.MAX_VALUE; } } }; } }); }
From source file:io.hops.metadata.HdfsVariables.java
private static CountersQueue.Counter incrementCounter(final Variable.Finder finder, final long increment) throws StorageException { return (CountersQueue.Counter) handleVariableWithWriteLock(new Handler() { @Override/* w w w. j a v a2s . c o m*/ public Object handle(VariableDataAccess<Variable, Variable.Finder> vd) throws StorageException { Variable variable = vd.getVariable(finder); if (variable instanceof IntVariable) { assert increment == (int) increment; int oldValue = ((IntVariable) vd.getVariable(finder)).getValue(); int newValue = IntMath.checkedAdd(oldValue, (int) increment); vd.setVariable(new IntVariable(finder, newValue)); return new CountersQueue.Counter(oldValue, newValue); } else if (variable instanceof LongVariable) { long oldValue = ((LongVariable) variable).getValue() == null ? 0 : ((LongVariable) variable).getValue(); long newValue = LongMath.checkedAdd(oldValue, increment); vd.setVariable(new LongVariable(finder, newValue)); return new CountersQueue.Counter(oldValue, newValue); } throw new IllegalStateException( "Cannot increment Variable of type " + variable.getClass().getSimpleName()); } }); }
From source file:com.wandrell.tabletop.stats.valuebox.AggregatedValueBox.java
/** * adds together all the {@code ValueBox} values and stores the resulting * value as the aggregated value./*from w w w. j a va2s. c o m*/ * <p> * This is to be used only when there is no other way to update the value, * as it will iterate over all {@code ValueBox} instances being kept on the * {@code AggregatedValueBox}. */ private final void generateValue() { final Integer old; // Old value, for the value change event ValueBox vhValue = null; // Iterated ValueBox old = aggregated; aggregated = 0; try { for (final ValueBox vh : getValueBoxesModifiable()) { vhValue = vh; // Tries to add the value aggregated = IntMath.checkedAdd(aggregated, vh.getValue()); } } catch (final ArithmeticException exception) { // Overflow if (vhValue == null) { aggregated = 0; } else if (vhValue.getValue() < 0) { // It was a substraction // The aggregated value is set to the minimum aggregated = Integer.MIN_VALUE; } else { // It was an addition // The aggregated value is set to the maximum aggregated = Integer.MAX_VALUE; } } fireValueChangedEvent(new ValueChangeEvent(this, old, aggregated)); }
From source file:com.google.errorprone.bugpatterns.ConstantOverflow.java
static Integer binop(Kind kind, int lhs, int rhs) { switch (kind) { case MULTIPLY: return IntMath.checkedMultiply(lhs, rhs); case DIVIDE:/* w w w. j a v a 2 s .co m*/ return lhs / rhs; case REMAINDER: return lhs % rhs; case PLUS: return IntMath.checkedAdd(lhs, rhs); case MINUS: return IntMath.checkedSubtract(lhs, rhs); case LEFT_SHIFT: return lhs << rhs; case RIGHT_SHIFT: return lhs >> rhs; case UNSIGNED_RIGHT_SHIFT: return lhs >>> rhs; case AND: return lhs & rhs; case XOR: return lhs ^ rhs; case OR: return lhs | rhs; default: return null; } }