List of usage examples for com.google.common.math LongMath checkedAdd
@GwtIncompatible("TODO") public static long checkedAdd(long a, long b)
From source file:org.sfs.math.Rounding.java
public static long up(long num, int factor) { if (num <= factor) { return factor; } else if (num % factor == 0) { return num; } else {//w w w. j a v a2 s. c om return LongMath.checkedAdd(num / factor * factor, factor); } }
From source file:org.apache.impala.util.MathUtil.java
public static long saturatingAdd(long a, long b) { try {/*from ww w. j a v a2 s . c om*/ return LongMath.checkedAdd(a, b); } catch (ArithmeticException e) { return a < 0 ? Long.MIN_VALUE : Long.MAX_VALUE; } }
From source file:org.sfs.util.Buffers.java
public static Iterable<Positional<Buffer>> partition(final Positional<Buffer> input, final int size) { return () -> new Iterator<Positional<Buffer>>() { final long position = input.getPosition(); final Buffer src = input.getValue(); private long offset = position; private Iterator<Buffer> delegate = Buffers.partition(src, size).iterator(); @Override/*from w w w .j a v a 2s . c om*/ public boolean hasNext() { return delegate.hasNext(); } @Override public Positional<Buffer> next() { Buffer buffer = delegate.next(); Positional<Buffer> mapped = new Positional<>(offset, buffer); offset = LongMath.checkedAdd(offset, size); return mapped; } @Override public void remove() { throw new UnsupportedOperationException(); } }; }
From source file:org.apache.phoenix.util.SequenceUtil.java
/** * @return true if we limit of a sequence has been reached. *//*from w w w .j ava2s . c o m*/ public static boolean checkIfLimitReached(long currentValue, long minValue, long maxValue, long incrementBy, long cacheSize, long numToAllocate) { long nextValue = 0; boolean increasingSeq = incrementBy > 0 ? true : false; // advance currentValue while checking for overflow try { long incrementValue; if (isBulkAllocation(numToAllocate)) { // For bulk allocation we increment independent of cache size incrementValue = LongMath.checkedMultiply(incrementBy, numToAllocate); } else { incrementValue = LongMath.checkedMultiply(incrementBy, cacheSize); } nextValue = LongMath.checkedAdd(currentValue, incrementValue); } catch (ArithmeticException e) { return true; } // check if limit was reached if ((increasingSeq && nextValue > maxValue) || (!increasingSeq && nextValue < minValue)) { return true; } return false; }
From source file:co.cask.cdap.internal.app.runtime.flow.FlowletProcessEntry.java
static <T> FlowletProcessEntry<T> create(ProcessSpecification<T> processSpec) { long nextDeque; try {// w w w . j av a 2 s. co m nextDeque = LongMath.checkedAdd(System.nanoTime(), processSpec.getInitialCallDelay()); } catch (ArithmeticException e) { // in case System.nanoTime + initialCallDelay > MAX_VALUE, we simply set nextDeque to MAX_VALUE // so that the flowlet never performs a deque instead of dequing immediately. nextDeque = Long.MAX_VALUE; } return new FlowletProcessEntry<>(processSpec, null, nextDeque); }
From source file:com.github.autermann.wps.streaming.example.AddAlgorithm.java
@Execute public void execute() { //WPS does not support BigInteger/BigDecimal... result = LongMath.checkedAdd(a, b); }
From source file:org.sfs.io.LimitedReadStream.java
@Override public LimitedReadStream handler(final Handler<Buffer> handler) { if (handler != null) { Handler<Buffer> wrapper = event -> { bytesRead = LongMath.checkedAdd(bytesRead, event.length()); Preconditions.checkState(bytesRead <= length, "Read %s, max is %s", bytesRead, length); handler.handle(event);//w w w .j av a 2 s .co m }; delegate.handler(wrapper); } else { delegate.handler(null); } return this; }
From source file:com.android.tradefed.util.TimeVal.java
/** * Parses the string as a hierarchical time value * <p />/*from w w w . j av a 2s .c o m*/ * The default unit is millis. The parser will accept {@code s} for seconds (1000 millis), * {@code m} for minutes (60 seconds), {@code h} for hours (60 minutes), or {@code d} for days * (24 hours). * <p /> * Units may be mixed and matched, so long as each unit appears at most once, and so long as * all units which do appear are listed in decreasing order of scale. So, for instance, * {@code h} may only appear before {@code m}, and may only appear after {@code d}. As a * specific example, "1d2h3m4s5ms" would be a valid time value, as would "4" or "4ms". All * embedded whitespace is discarded. * <p /> * Do note that this method rejects overflows. So the output number is guaranteed to be * non-negative, and to fit within the {@code long} type. */ public static long fromString(String value) throws NumberFormatException { if (value == null) throw new NumberFormatException("value is null"); try { value = value.replaceAll("\\s+", ""); Matcher m = TIME_PATTERN.matcher(value); if (m.matches()) { // This works by, essentially, modifying the units of timeValue, from the // largest supported unit, until we've dropped down to millis. long timeValue = 0; timeValue = val(m.group("d")); // 1 day == 24 hours timeValue = LongMath.checkedMultiply(timeValue, 24); timeValue = LongMath.checkedAdd(timeValue, val(m.group("h"))); // 1 hour == 60 minutes timeValue = LongMath.checkedMultiply(timeValue, 60); timeValue = LongMath.checkedAdd(timeValue, val(m.group("m"))); // 1 hour == 60 seconds timeValue = LongMath.checkedMultiply(timeValue, 60); timeValue = LongMath.checkedAdd(timeValue, val(m.group("s"))); // 1 second == 1000 millis timeValue = LongMath.checkedMultiply(timeValue, 1000); timeValue = LongMath.checkedAdd(timeValue, val(m.group("ms"))); return timeValue; } } catch (ArithmeticException e) { throw new NumberFormatException( String.format("Failed to parse value %s as a time value: %s", value, e.getMessage())); } throw new NumberFormatException(String.format("Failed to parse value %s as a time value", value)); }
From source file:org.sfs.encryption.impl.SAES256v01.java
@Override public long encryptOutputSize(long size) { try {/*from w w w . ja v a 2s. c o m*/ return LongMath.checkedAdd(size, MAC_SIZE_BYTES); } catch (ArithmeticException e) { // do nothing } return -1; }
From source file:org.neoscoinj.utils.Fiat.java
public Fiat add(final Fiat value) { checkArgument(value.currencyCode.equals(currencyCode)); return new Fiat(currencyCode, LongMath.checkedAdd(this.value, value.value)); }