Java Code Examples for com.google.common.primitives.Ints.checkedCast()

The following are Jave code examples for showing how to use checkedCast() of the com.google.common.primitives.Ints class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: sstable-adaptor   File: LimitingRebufferer.java   Source Code and License Vote up 6 votes
@Override
public BufferHolder rebuffer(long position)
{
    bufferHolder = wrapped.rebuffer(position);
    buffer = bufferHolder.buffer();
    offset = bufferHolder.offset();
    int posInBuffer = Ints.checkedCast(position - offset);
    int remaining = buffer.limit() - posInBuffer;
    if (remaining == 0)
        return this;

    if (remaining > limitQuant)
    {
        buffer.limit(posInBuffer + limitQuant); // certainly below current limit
        remaining = limitQuant;
    }
    limiter.acquire(remaining);
    return this;
}
 
Example 2
Project: hadoop   File: TopConf.java   Source Code and License Vote up 6 votes
public TopConf(Configuration conf) {
  isEnabled = conf.getBoolean(DFSConfigKeys.NNTOP_ENABLED_KEY,
      DFSConfigKeys.NNTOP_ENABLED_DEFAULT);
  String[] periodsStr = conf.getTrimmedStrings(
      DFSConfigKeys.NNTOP_WINDOWS_MINUTES_KEY,
      DFSConfigKeys.NNTOP_WINDOWS_MINUTES_DEFAULT);
  nntopReportingPeriodsMs = new int[periodsStr.length];
  for (int i = 0; i < periodsStr.length; i++) {
    nntopReportingPeriodsMs[i] = Ints.checkedCast(
        TimeUnit.MINUTES.toMillis(Integer.parseInt(periodsStr[i])));
  }
  for (int aPeriodMs: nntopReportingPeriodsMs) {
    Preconditions.checkArgument(aPeriodMs >= TimeUnit.MINUTES.toMillis(1),
        "minimum reporting period is 1 min!");
  }
}
 
Example 3
Project: guava-mock   File: LoadingCacheSingleThreadBenchmark.java   Source Code and License Vote up 6 votes
@BeforeExperiment void setUp() {
  // random integers will be generated in this range, then raised to the
  // power of (1/concentration) and floor()ed
  max = Ints.checkedCast((long) Math.pow(distinctKeys, concentration));

  cache = CacheBuilder.newBuilder()
      .concurrencyLevel(segments)
      .maximumSize(maximumSize)
      .build(
          new CacheLoader<Integer, Integer>() {
            @Override public Integer load(Integer from) {
              return (int) misses.incrementAndGet();
            }
          });

  // To start, fill up the cache.
  // Each miss both increments the counter and causes the map to grow by one,
  // so until evictions begin, the size of the map is the greatest return
  // value seen so far
  while (cache.getUnchecked(nextRandomKey()) < maximumSize) {}

  requests.set(0);
  misses.set(0);
}
 
Example 4
Project: minebox   File: MineboxExport.java   Source Code and License Vote up 5 votes
@Override
public ByteBuffer read(final long offset, final int length) throws IOException {
    read.mark(length);
    final ByteBuffer origMessage = ByteBuffer.allocate(length);
    for (Integer bucketIndex : getBuckets(offset, length)) { //eventually make parallel
        Bucket bucket = getBucketFromIndex(bucketIndex);
        final long absoluteOffsetForThisBucket = Math.max(offset, bucket.getBaseOffset());
        final int lengthForBucket = Ints.checkedCast(Math.min(bucket.getUpperBound() + 1, offset + length) - absoluteOffsetForThisBucket); //todo this threw an exception
        final int dataOffset = Ints.checkedCast(Math.max(0, bucket.getBaseOffset() - offset));
        final ByteBuffer pseudoCopy = bufferForBucket(origMessage, lengthForBucket, dataOffset);

        bucket.getBytes(pseudoCopy, absoluteOffsetForThisBucket, lengthForBucket);
    }
    return origMessage;
}
 
Example 5
Project: tools   File: MineboxExport.java   Source Code and License Vote up 5 votes
@Override
public ByteBuffer read(final long offset, final int length) throws IOException {
    read.mark(length);
    final ByteBuffer origMessage = ByteBuffer.allocate(length);
    for (Integer bucketIndex : getBuckets(offset, length)) { //eventually make parallel
        Bucket bucket = getBucketFromIndex(bucketIndex);
        final long absoluteOffsetForThisBucket = Math.max(offset, bucket.getBaseOffset());
        final int lengthForBucket = Ints.checkedCast(Math.min(bucket.getUpperBound() + 1, offset + length) - absoluteOffsetForThisBucket); //todo this threw an exception
        final int dataOffset = Ints.checkedCast(Math.max(0, bucket.getBaseOffset() - offset));
        final ByteBuffer pseudoCopy = bufferForBucket(origMessage, lengthForBucket, dataOffset);

        bucket.getBytes(pseudoCopy, absoluteOffsetForThisBucket, lengthForBucket);
    }
    return origMessage;
}
 
Example 6
Project: talchain   File: QuotientFilter.java   Source Code and License Vote up 5 votes
long getElement(long idx) {
    long elt = 0;
    long bitpos = ELEMENT_BITS * idx;
    int tabpos = Ints.checkedCast(bitpos / 64);
    long slotpos = bitpos % 64;
    long spillbits = (slotpos + ELEMENT_BITS) - 64;
    elt = (table[tabpos] >>> slotpos) & ELEMENT_MASK;
    if (spillbits > 0) {
        ++tabpos;
        long x = table[tabpos] & LOW_MASK(spillbits);
        elt |= x << (ELEMENT_BITS - spillbits);
    }
    return elt;
}
 
Example 7
Project: talchain   File: QuotientFilter.java   Source Code and License Vote up 5 votes
void setElement(long idx, long elt) {
    long bitpos = ELEMENT_BITS * idx;
    int tabpos = Ints.checkedCast(bitpos / 64);
    long slotpos = bitpos % 64;
    long spillbits = (slotpos + ELEMENT_BITS) - 64;
    elt &= ELEMENT_MASK;
    table[tabpos] &= ~(ELEMENT_MASK << slotpos);
    table[tabpos] |= elt << slotpos;
    if (spillbits > 0) {
        ++tabpos;
        table[tabpos] &= ~LOW_MASK(spillbits);
        table[tabpos] |= elt >>> (ELEMENT_BITS - spillbits);
    }
}
 
Example 8
Project: AppCoins-ethereumj   File: QuotientFilter.java   Source Code and License Vote up 5 votes
void setElement(long idx, long elt) {
    long bitpos = ELEMENT_BITS * idx;
    int tabpos = Ints.checkedCast(bitpos / 64);
    long slotpos = bitpos % 64;
    long spillbits = (slotpos + ELEMENT_BITS) - 64;
    elt &= ELEMENT_MASK;
    table[tabpos] &= ~(ELEMENT_MASK << slotpos);
    table[tabpos] |= elt << slotpos;
    if (spillbits > 0) {
        ++tabpos;
        table[tabpos] &= ~LOW_MASK(spillbits);
        table[tabpos] |= elt >>> (ELEMENT_BITS - spillbits);
    }
}
 
Example 9
Project: guava-mock   File: BloomFilterTest.java   Source Code and License Vote up 5 votes
@AndroidIncompatible // slow
public void testBitSize() {
  double fpp = 0.03;
  for (int i = 1; i < 10000; i++) {
    long numBits = BloomFilter.optimalNumOfBits(i, fpp);
    int arraySize = Ints.checkedCast(LongMath.divide(numBits, 64, RoundingMode.CEILING));
    assertEquals(
        arraySize * Long.SIZE,
        BloomFilter.create(Funnels.unencodedCharsFunnel(), i, fpp).bitSize());
  }
}
 
Example 10
Project: NyaaCore   File: ExperienceUtils.java   Source Code and License Vote up 5 votes
/**
 * How much exp points at least needed to reach this level.
 * i.e. getLevel() = level && getExp() == 0
 */
public static int getExpForLevel(int level) {
    if (level < 0) throw new IllegalArgumentException();
    else if (level <= 16) return (level + 6) * level;
    else if (level < 32) return Ints.checkedCast(Math.round(2.5 * level * level - 40.5 * level + 360));
    else return Ints.checkedCast(Math.round(4.5 * level * level - 162.5 * level + 2220));
}
 
Example 11
Project: AppCoins-ethereumj   File: QuotientFilter.java   Source Code and License Vote up 5 votes
long getElement(long idx) {
    long elt = 0;
    long bitpos = ELEMENT_BITS * idx;
    int tabpos = Ints.checkedCast(bitpos / 64);
    long slotpos = bitpos % 64;
    long spillbits = (slotpos + ELEMENT_BITS) - 64;
    elt = (table[tabpos] >>> slotpos) & ELEMENT_MASK;
    if (spillbits > 0) {
        ++tabpos;
        long x = table[tabpos] & LOW_MASK(spillbits);
        elt |= x << (ELEMENT_BITS - spillbits);
    }
    return elt;
}
 
Example 12
Project: sstable-adaptor   File: UUIDGen.java   Source Code and License Vote up 4 votes
/**
 * @param uuid
 * @return seconds since Unix epoch
 */
public static int unixTimestampInSec(UUID uuid)
{
    return Ints.checkedCast(TimeUnit.MILLISECONDS.toSeconds(unixTimestamp(uuid)));
}
 
Example 13
Project: sstable-adaptor   File: ColumnIndex.java   Source Code and License Vote up 4 votes
private void addIndexBlock() throws IOException
{
    IndexInfo cIndexInfo = new IndexInfo(firstClustering,
                                         lastClustering,
                                         startPosition,
                                         currentPosition() - startPosition,
                                         openMarker);

    // indexOffsets is used for both shallow (ShallowIndexedEntry) and non-shallow IndexedEntry.
    // For shallow ones, we need it to serialize the offsts in finish().
    // For non-shallow ones, the offsts are passed into IndexedEntry, so we don't have to
    // calculate the offsets again.

    // indexOffsets contains the offsets of the serialized IndexInfo objects.
    // I.e. indexOffsets[0] is always 0 so we don't have to deal with a special handling
    // for index #0 and always subtracting 1 for the index (which could be error-prone).
    if (indexOffsets == null)
        indexOffsets = new int[10];
    else
    {
        if (columnIndexCount >= indexOffsets.length)
            indexOffsets = Arrays.copyOf(indexOffsets, indexOffsets.length + 10);

        //the 0th element is always 0
        if (columnIndexCount == 0)
        {
            indexOffsets[columnIndexCount] = 0;
        }
        else
        {
            indexOffsets[columnIndexCount] =
            buffer != null
            ? Ints.checkedCast(buffer.position())
            : indexSamplesSerializedSize;
        }
    }
    columnIndexCount++;

    // First, we collect the IndexInfo objects until we reach Config.column_index_cache_size_in_kb in an ArrayList.
    // When column_index_cache_size_in_kb is reached, we switch to byte-buffer mode.
    if (buffer == null)
    {
        indexSamplesSerializedSize += idxSerializer.serializedSize(cIndexInfo);
        if (indexSamplesSerializedSize + columnIndexCount * TypeSizes.sizeof(0) > DatabaseDescriptor.getColumnIndexCacheSize())
        {
            buffer = reuseOrAllocateBuffer();
            for (IndexInfo indexSample : indexSamples)
            {
                idxSerializer.serialize(indexSample, buffer);
            }
        }
        else
        {
            indexSamples.add(cIndexInfo);
        }
    }
    // don't put an else here...
    if (buffer != null)
    {
        idxSerializer.serialize(cIndexInfo, buffer);
    }

    firstClustering = null;
}
 
Example 14
Project: tools   File: MineboxExport.java   Source Code and License Vote up 4 votes
private int bucketFromOffset(long offset) {
    return Ints.checkedCast(offset / getBucketSize());
}
 
Example 15
Project: guava-mock   File: BloomFilterStrategies.java   Source Code and License Vote up 4 votes
BitArray(long bits) {
  this(new long[Ints.checkedCast(LongMath.divide(bits, 64, RoundingMode.CEILING))]);
}
 
Example 16
Project: spark_deep   File: TransportConf.java   Source Code and License Vote up 4 votes
/**
 * Maximum number of bytes to be encrypted at a time when SASL encryption is enabled.
 */
public int maxSaslEncryptedBlockSize() {
  return Ints.checkedCast(JavaUtils.byteStringAsBytes(
    conf.get("spark.network.sasl.maxEncryptedBlockSize", "64k")));
}
 
Example 17
Project: AppCoins-ethereumj   File: QuotientFilter.java   Source Code and License Vote up 4 votes
static int TABLE_SIZE(int quotientBits, int remainderBits) {
    long bits = (1 << quotientBits) * (remainderBits + 3);
    long longs = bits / 64;
    return Ints.checkedCast((bits % 64) > 0 ? (longs + 1) : longs);
}
 
Example 18
Project: hadoop   File: ShortCircuitShm.java   Source Code and License Vote up 2 votes
/**
 * Get the Slot index.
 *
 * @return      The index of this slot.
 */
public int getSlotIdx() {
  return Ints.checkedCast(
      (slotAddress - baseAddress) / BYTES_PER_SLOT);
}
 
Example 19
Project: graphiak   File: GraphiakInitializer.java   Source Code and License Vote up 2 votes
/**
 * Constructor
 *
 * @param store
 *            Metric store
 * @param maxLength
 *            maximum metric line length
 */
public GraphiakInitializer(@Nonnull final MetricStore store,
        final long maxLength) {
    this.store = Objects.requireNonNull(store);
    this.maxLength = Ints.checkedCast(maxLength);
}
 
Example 20
Project: spark_deep   File: TransportConf.java   Source Code and License Vote up 2 votes
/**
 * Minimum size of a block that we should start using memory map rather than reading in through
 * normal IO operations. This prevents Spark from memory mapping very small blocks. In general,
 * memory mapping has high overhead for blocks close to or below the page size of the OS.
 */
public int memoryMapBytes() {
  return Ints.checkedCast(JavaUtils.byteStringAsBytes(
    conf.get("spark.storage.memoryMapThreshold", "2m")));
}