Example usage for com.google.common.math LongMath checkedAdd

List of usage examples for com.google.common.math LongMath checkedAdd

Introduction

In this page you can find the example usage for com.google.common.math LongMath checkedAdd.

Prototype

@GwtIncompatible("TODO")
public static long checkedAdd(long a, long b) 

Source Link

Document

Returns the sum of a and b , provided it does not overflow.

Usage

From source file:com.mygeopay.core.coins.Value.java

public Value add(final Value value) {
    checkArgument(type.equals(value.type), "Cannot add a different type");
    return new Value(this.type, LongMath.checkedAdd(this.value, value.value));
}

From source file:com.mygeopay.core.coins.Value.java

public Value add(final Coin value) {
    return new Value(this.type, LongMath.checkedAdd(this.value, value.value));
}

From source file:com.matthewmitchell.nubitsj.core.Coin.java

public Coin add(final Coin value) {
    return new Coin(LongMath.checkedAdd(this.value, value.value));
}

From source file:com.coinomi.core.coins.Value.java

public Value add(final long value) {
    return new Value(this.type, LongMath.checkedAdd(this.value, value));
}

From source file:io.opencensus.common.Timestamp.java

private static Timestamp ofEpochSecond(long epochSecond, long nanoAdjustment) {
    long secs = LongMath.checkedAdd(epochSecond, floorDiv(nanoAdjustment, NANOS_PER_SECOND));
    int nos = (int) floorMod(nanoAdjustment, NANOS_PER_SECOND);
    return create(secs, nos);
}

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 .  ja va 2s . 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:io.opencensus.common.Timestamp.java

private Timestamp plus(long secondsToAdd, long nanosToAdd) {
    if ((secondsToAdd | nanosToAdd) == 0) {
        return this;
    }//w w w  .ja v a 2 s.  c om
    long epochSec = LongMath.checkedAdd(getSeconds(), secondsToAdd);
    epochSec = LongMath.checkedAdd(epochSec, nanosToAdd / NANOS_PER_SECOND);
    nanosToAdd = nanosToAdd % NANOS_PER_SECOND;
    long nanoAdjustment = getNanos() + nanosToAdd; // safe int + NANOS_PER_SECOND
    return ofEpochSecond(epochSec, nanoAdjustment);
}

From source file:org.sfs.filesystem.volume.VolumeV1.java

@Override
public Observable<TransientXVolume> volumeInfo(SfsVertx vertx) {
    Context context = vertx.getOrCreateContext();
    return Defer.aVoid().doOnNext(aVoid -> {
        checkStarted();/*from   ww w .j  a  v  a  2s . c o  m*/
    }).flatMap(aVoid -> RxHelper.executeBlocking(context, vertx.getBackgroundPool(), () -> {

        try {
            FileStore fileStore = Files.getFileStore(basePath);

            long usableSpace = fileStore.getUsableSpace();
            long actualUsableSpace;
            try {
                actualUsableSpace = LongMath.checkedAdd(indexFileAllocator.getBytesFree(usableSpace),
                        dataFileAllocator.getBytesFree(usableSpace));
            } catch (ArithmeticException e) {
                actualUsableSpace = usableSpace;
            }

            TransientXAllocatedFile indexFileInfo = new TransientXAllocatedFile()
                    .setFile(indexFilePath.toString()).setFileSizeBytes(Files.size(indexFilePath))
                    .setBytesFree(indexFileAllocator.getBytesFree(usableSpace))
                    .setFreeRangeCount(indexFileAllocator.getNumberOfFreeRanges())
                    .setLockCount(indexFile.getLockCount())
                    .setWriteQueueBytesPending(indexFile.getWriteQueueSize())
                    .setWriteQueueBytesFull(indexFile.getWriteQueueMaxWrites())
                    .setWriteQueueBytesDrained(indexFile.getWriteQueueLowWater());

            TransientXAllocatedFile dataFileInfo = new TransientXAllocatedFile()
                    .setFile(dataFilePath.toString()).setFileSizeBytes(Files.size(dataFilePath))
                    .setBytesFree(dataFileAllocator.getBytesFree(usableSpace))
                    .setFreeRangeCount(dataFileAllocator.getNumberOfFreeRanges())
                    .setLockCount(blobFile.getLockCount())
                    .setWriteQueueBytesPending(blobFile.getWriteQueueSize())
                    .setWriteQueueBytesFull(blobFile.getWriteQueueMaxWrites())
                    .setWriteQueueBytesDrained(blobFile.getWriteQueueLowWater());

            TransientXFileSystem fileSystemInfo = new TransientXFileSystem().setDevice(fileStore.name())
                    .setPath(basePath.toString()).setTotalSpace(fileStore.getTotalSpace())
                    .setUnallocatedSpace(fileStore.getUnallocatedSpace()).setUsableSpace(usableSpace)
                    .setType(fileStore.type()).setPartition(basePath.getRoot().toString());

            TransientXVolume volumeInfo = new TransientXVolume().setId(volumeId).setIndexFile(indexFileInfo)
                    .setDataFile(dataFileInfo).setFileSystem(fileSystemInfo).setUsableSpace(actualUsableSpace)
                    .setStatus(volumeState.get());

            return volumeInfo;

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }));
}

From source file:io.hops.metadata.HdfsVariables.java

public static Long incrementMisReplicatedIndex(final int increment) throws IOException {
    return (Long) new LightWeightRequestHandler(HDFSOperationType.INCREMENT_MIS_REPLICATED_FILES_INDEX) {
        @Override//from   w ww .  ja  v  a2  s. com
        public Object performTask() throws IOException {

            return handleVariableWithWriteLock(new Handler() {
                @Override
                public Object handle(VariableDataAccess<Variable, Variable.Finder> vd) throws StorageException {
                    LongVariable var = (LongVariable) vd.getVariable(Variable.Finder.MisReplicatedFilesIndex);
                    long oldValue = var == null ? 0 : var.getValue();
                    long newValue = increment == 0 ? 0 : LongMath.checkedAdd(oldValue, increment);
                    vd.setVariable(new LongVariable(Variable.Finder.MisReplicatedFilesIndex, newValue));
                    return newValue;
                }
            });
        }
    }.handle();
}

From source file:com.duprasville.guava.probably.BloomFilter.java

/**
 * Adds the specified element to this filter. A return value of {@code true} ensures that {@link
 * #contains(Object)} given {@code e} will also return {@code true}.
 *
 * @param e element to be added to this filter
 * @return always {@code true} as {@code com.google.common.hash.BloomFilter} cannot fail to add an
 * object//from ww w .ja  v a2  s .  c  o  m
 * @throws NullPointerException if the specified element is null
 * @see #contains(Object)
 * @see #addAll(Collection)
 * @see #addAll(ProbabilisticFilter)
 * @see <a target="guavadoc" href="http://google.github.io/guava/releases/snapshot/api/docs/com/google/common/hash/BloomFilter.html#put(T)">com.google.common.hash.BloomFilter#put(T)</a>
 */
public boolean add(E e) {
    checkNotNull(e);
    delegate.put(e);
    size = LongMath.checkedAdd(size, 1L);
    return true;
}