Example usage for com.google.common.primitives Ints saturatedCast

List of usage examples for com.google.common.primitives Ints saturatedCast

Introduction

In this page you can find the example usage for com.google.common.primitives Ints saturatedCast.

Prototype

public static int saturatedCast(long value) 

Source Link

Document

Returns the int nearest in value to value .

Usage

From source file:net.awairo.mcmod.common.v1.util.LimitedNumber.java

/**
 * ??????????Integer????./*from  ww w .j  a  v a  2s .c  o m*/
 * 
 * @return Integer?{@link LimitedNumber}
 */
public static Builder<Integer> ofInteger(final Prop prop) {
    return new Builder<Integer>() {
        {
            current(prop.getInt());
            min(Integer.MIN_VALUE);
            max(Integer.MAX_VALUE);
            step(1);
        }

        @Override
        protected LimitedNumber<Integer> newInstance(Integer current, Integer min, Integer max, Integer step) {
            return new LimitedNumber<Integer>(current, min, max, step) {
                @Override
                protected Integer incrementValue(Integer current, Integer step) {
                    return Ints.saturatedCast(current.longValue() + step.longValue());
                }

                @Override
                protected Integer decrementValue(Integer current, Integer step) {
                    return Ints.saturatedCast(current.longValue() - step.longValue());
                }

                @Override
                protected void updated() {
                    prop.set(current());
                }
            };
        }
    };
}

From source file:net.derquinse.bocas.EntryWeigher.java

@Override
public int weigh(Object key, MemoryByteSource value) {
    return Ints.saturatedCast(value.size() + 16);
}

From source file:org.graylog2.dashboards.widgets.WidgetCacheTime.java

@Inject
public WidgetCacheTime(@Named("dashboard_widget_default_cache_time") Duration defaultCacheTime,
        @Assisted int cacheTime) {
    this.cacheTime = cacheTime < 1 ? Ints.saturatedCast(defaultCacheTime.toSeconds()) : cacheTime;
}

From source file:com.google.gerrit.server.index.IndexedChangeQuery.java

@VisibleForTesting
static QueryOptions convertOptions(QueryOptions opts) {
    // Increase the limit rather than skipping, since we don't know how many
    // skipped results would have been filtered out by the enclosing AndSource.
    int backendLimit = opts.config().maxLimit();
    int limit = Ints.saturatedCast((long) opts.limit() + opts.start());
    limit = Math.min(limit, backendLimit);
    return QueryOptions.create(opts.config(), 0, limit, opts.fields());
}

From source file:org.apache.cassandra.io.util.MemoryInputStream.java

public MemoryInputStream(Memory mem) {
    this(mem, Ints.saturatedCast(mem.size));
}

From source file:org.glowroot.central.repo.Common.java

static int getAdjustedTTL(int ttl, long captureTime, Clock clock) {
    if (ttl == 0) {
        return 0;
    }/*from ww  w  .  ja v  a2s  .c  om*/
    long captureTimeAgoSeconds = MILLISECONDS.toSeconds(clock.currentTimeMillis() - captureTime);
    // need saturated cast because captureTimeAgoSeconds may be negative
    int adjustedTTL = Ints.saturatedCast(ttl - captureTimeAgoSeconds);
    // max is a safety guard
    return Math.max(adjustedTTL, 60);
}

From source file:com.google.gerrit.server.index.QueryOptions.java

public QueryOptions convertForBackend() {
    // Increase the limit rather than skipping, since we don't know how many
    // skipped results would have been filtered out by the enclosing AndSource.
    int backendLimit = config().maxLimit();
    int limit = Ints.saturatedCast((long) limit() + start());
    limit = Math.min(limit, backendLimit);
    return create(config(), 0, limit, fields());
}

From source file:net.slashies.phpBridge.ClassHierarchyCollection.java

@Override
public int size() {
    int s = size;
    if (s < 0)
        size = s = Ints.saturatedCast(stream().count());
    return s;
}

From source file:org.apache.cassandra.io.util.MemoryInputStream.java

@Override
protected void reBuffer() throws IOException {
    if (offset - mem.peer >= mem.size())
        return;/*from  w ww  . ja v  a2 s.c  om*/

    buffer = getByteBuffer(offset, Math.min(bufferSize, Ints.saturatedCast(memRemaining())));
    offset += buffer.capacity();
}

From source file:io.prestosql.jdbc.PrestoArray.java

@Override
public Object getArray(long index, int count) throws SQLException {
    int arrayOffset = Ints.saturatedCast(index - 1);
    if (index < 1 || count < 0 || (arrayOffset + count) > array.length) {
        throw new SQLException("Index out of bounds");
    }/*from   www .  ja v  a 2s  . co  m*/
    return Arrays.copyOfRange(array, arrayOffset, arrayOffset + count);
}