Example usage for com.google.common.hash HashCode asInt

List of usage examples for com.google.common.hash HashCode asInt

Introduction

In this page you can find the example usage for com.google.common.hash HashCode asInt.

Prototype

@CheckReturnValue
public abstract int asInt();

Source Link

Document

Returns the first four bytes of #asBytes() this hashcode's bytes , converted to an int value in little-endian order.

Usage

From source file:spimedb.util.bloom.UnBloomFilter.java

/**
 * Returns whether the given elemtn has been previously seen by this filter. That is, if a byte
 * buffer with the same bytes as elem has been passed to to this method before.
 * <p>/*from www  .ja  v  a  2 s.c  o m*/
 * This method may return false when it has seen an element before. This occurs if the element passed in
 * hashes to the same index in the underlying array as another element previously checked. On the
 * flip side, this method will never return true incorrectly.
 *
 * @param element The byte array that may have been previously seen.
 * @return Whether the element is contained in the OoaBFilter.
 */
public boolean containsOrAdd(@NotNull X element) {
    HashCode code = HASH_FUNC.hashBytes(asBytes.apply(element));

    Object prev = array.getAndSet(code.asInt() & sizeMask, element);

    total++;
    if ((prev == null || !element.equals(prev))) {
        hit++;
        return false;
    }

    return true;

}

From source file:it.unibo.alchemist.model.implementations.molecules.SimpleMolecule.java

private void initHash() {
    if (hash == null) {
        final HashCode hashCode = Hashing.murmur3_128().hashString(n, StandardCharsets.UTF_8);
        hash32 = hashCode.asInt();
        hash64 = hashCode.asLong();/*from  www .j  av a2s.  c  om*/
        hash = hashCode.asBytes();
    }
}

From source file:com.spotify.folsom.ketama.Continuum.java

private TreeMap<Integer, RawMemcacheClient> buildRing(final Collection<AddressAndClient> clients) {

    final TreeMap<Integer, RawMemcacheClient> r = Maps.newTreeMap();
    for (final AddressAndClient client : clients) {
        final String address = client.getAddress().toString();

        byte[] hash = addressToBytes(address);
        for (int i = 0; i < VNODE_RATIO; i++) {
            final HashCode hashCode = Hasher.hash(hash);
            hash = hashCode.asBytes();/*from w  ww  . j  av a 2 s .c  o m*/
            r.put(hashCode.asInt(), client.getClient());
        }
    }
    return r;
}

From source file:org.danilopianini.lang.HashString.java

private void computeHashes() {
    final HashCode hashCode = HASHF.hashBytes(s.getBytes(CHARSET));
    hash32 = hashCode.asInt();
    hash = hashCode.asBytes();/*from w w w  . j av a 2  s  .  c  o  m*/
}

From source file:graphene.util.ooab.OoaBFilter.java

/**
 * Returns whether the given elemtn has been previously seen by this filter.
 * That is, if a byte buffer with the same bytes as elem has been passed to
 * to this method before.//from w  w w . j  ava  2s . co  m
 * 
 * This method may return false when it has seen an element before. This
 * occurs if the element passed in hashes to the same index in the
 * underlying array as another element previously checked. On the flip side,
 * this method will never return true incorrectly.
 * 
 * @param element
 *            The byte array that may have been previously seen.
 * @return Whether the element is contained in the OoaBFilter.
 */
public boolean containsAndAdd(Element element) {
    ByteBuffer eBytes = element.getByteBuffer();
    HashCode code = HASH_FUNC.hashBytes(eBytes.array());
    int index = code.asInt() & sizeMask;

    boolean seen = true;
    ByteBuffer buffer = array[index];

    synchronized (buffer) {
        if (!buffer.equals(eBytes)) {
            seen = false;
            buffer.put(eBytes);
            buffer.rewind();
        }
    }

    return seen;
}

From source file:org.rf.ide.core.executor.ArgumentsFile.java

File writeToTemporaryOrUseAlreadyExisting() throws IOException {
    final String content = generateContent();
    // it's deprecated although we don't need security here, so md5 is fine
    @SuppressWarnings("deprecation")
    final HashFunction md5Hasher = Hashing.md5();
    final HashCode hash = md5Hasher.hashString(content, Charsets.UTF_8);

    final String fileName = "args_" + Strings.padStart(Integer.toHexString(hash.asInt()), 8, '0') + ".arg";
    final Path dir = RobotRuntimeEnvironment.createTemporaryDirectory();

    for (final File existingArgFile : dir.toFile().listFiles((d, name) -> name.equals(fileName))) {
        final HashCode candidateHash = md5Hasher
                .hashString(Files.asCharSource(existingArgFile, Charsets.UTF_8).read(), Charsets.UTF_8);
        if (hash.equals(candidateHash)) {
            return existingArgFile;
        }/*  w ww .  j av  a 2s.  c  o  m*/
    }

    final File filePath = dir.resolve(fileName).toFile();
    writeTo(filePath);
    return filePath;
}

From source file:spimedb.util.bloom.OoaBFilter.java

/**
 * Returns whether the given elemtn has been previously seen by this filter. That is, if a byte
 * buffer with the same bytes as elem has been passed to to this method before.
 *
 * This method may return false when it has seen an element before. This occurs if the element passed in
 * hashes to the same index in the underlying array as another element previously checked. On the
 * flip side, this method will never return true incorrectly.
 *
 * @param element The byte array that may have been previously seen.
 * @return Whether the element is contained in the OoaBFilter.
 *///w ww  . j a v  a2 s.co m
public boolean containsAndAdd(Element element) {
    ByteBuffer eBytes = element.getByteBuffer();
    HashCode code = HASH_FUNC.hashBytes(eBytes.array(), eBytes.arrayOffset() + eBytes.position(),
            eBytes.remaining());
    int index = code.asInt() & sizeMask;

    boolean seen = true;
    ByteBuffer buffer = array[index];

    synchronized (buffer) {
        if (!buffer.equals(eBytes)) {
            seen = false;
            buffer.put(eBytes);
            buffer.rewind();
        }
    }

    return seen;
}

From source file:org.tzi.use.uml.ocl.type.MessageType.java

private int generateHash() {
    HashFunction hf = Hashing.md5();//from w w  w.ja  v a2  s . c  o  m
    HashCode hc = hf.newHasher().putBoolean(isReferencingSignal())
            .putString(isReferencingOperation() ? this.referredOperation.name() : "", Charsets.UTF_8)
            .putString(isReferencingSignal() ? this.referredSignal.name() : "", Charsets.UTF_8).hash();

    return hc.asInt();
}

From source file:com.google.cloud.hadoop.gcsio.testing.InMemoryObjectEntry.java

public InMemoryObjectEntry(String bucketName, String objectName, long createTimeMillis, String contentType,
        Map<String, byte[]> metadata) {
    // Override close() to commit its completed byte array into completedContents to reflect
    // the behavior that any readable contents are only well-defined if the writeStream is closed.
    writeStream = new ByteArrayOutputStream() {
        @Override/*  www .j a va 2  s .  c  o  m*/
        public synchronized void close() throws IOException {
            synchronized (InMemoryObjectEntry.this) {
                completedContents = toByteArray();
                HashCode md5 = Hashing.md5().hashBytes(completedContents);
                HashCode crc32c = Hashing.crc32c().hashBytes(completedContents);
                writeStream = null;
                writeChannel = null;
                info = new GoogleCloudStorageItemInfo(info.getResourceId(), info.getCreationTime(),
                        completedContents.length, null, null, info.getContentType(), info.getMetadata(),
                        info.getContentGeneration(), 0L,
                        new VerificationAttributes(md5.asBytes(), Ints.toByteArray(crc32c.asInt())));
            }
        }
    };

    // We have to delegate because we can't extend from the inner class returned by,
    // Channels.newChannel, and the default version doesn't enforce ClosedChannelException
    // when trying to write to a closed channel; probably because it relies on the underlying
    // output stream throwing when closed. The ByteArrayOutputStream doesn't enforce throwing
    // when closed, so we have to manually do it.
    WritableByteChannel delegateWriteChannel = Channels.newChannel(writeStream);
    writeChannel = new WritableByteChannelImpl(delegateWriteChannel);

    // Size 0 initially because this object exists, but contains no data.
    info = new GoogleCloudStorageItemInfo(new StorageResourceId(bucketName, objectName), createTimeMillis, 0,
            null, null, contentType, ImmutableMap.copyOf(metadata), createTimeMillis, 0L);
}

From source file:lbaas.util.HashAlgorithm.java

public int hash(Object key) {
    HashCode hashCode;
    HashFunction hashAlgorithm;//from w w  w  .j ava2 s .c om
    switch (hashType) {
    case MD5:
        hashAlgorithm = Hashing.md5();
        break;
    case MURMUR3_32:
        hashAlgorithm = Hashing.murmur3_32();
        break;
    case SHA256:
        hashAlgorithm = Hashing.sha256();
        break;
    case SIP24:
        hashAlgorithm = Hashing.sipHash24();
        break;
    default:
        hashAlgorithm = Hashing.sipHash24();
        break;
    }
    if (key instanceof String) {
        hashCode = hashAlgorithm.newHasher().putString((String) key, Charsets.UTF_8).hash();
    } else if (key instanceof Long) {
        hashCode = hashAlgorithm.newHasher().putLong((Long) key).hash();
    } else {
        hashCode = hashAlgorithm.newHasher().hash();
    }
    return hashCode.asInt();
}