Example usage for com.google.common.hash HashFunction hashObject

List of usage examples for com.google.common.hash HashFunction hashObject

Introduction

In this page you can find the example usage for com.google.common.hash HashFunction hashObject.

Prototype

<T> HashCode hashObject(T instance, Funnel<? super T> funnel);

Source Link

Document

Shortcut for newHasher().putObject(instance, funnel).hash() .

Usage

From source file:org.apache.niolex.common.guava.GuavaHashing.java

/**
 * @param args//from   w w w. j a  v  a2 s.  co m
 */
public static void main(String[] args) {
    // Common Hash
    HashFunction hf = Hashing.goodFastHash(32);
    HashCode code = hf.hashObject(new Person(1, "Jiyun", "Xie", 1984), PersonFunnel.INSTANCE);
    System.out.println("Code1 => " + code.asInt());
    code = hf.hashObject(new Person(1, "Jiyun", "Xie", 1985), PersonFunnel.INSTANCE);
    System.out.println("Code2 => " + code.asInt());
    // Consistent Hashing
    HashFunction hf2 = Hashing.goodFastHash(64);
    code = hf2.hashObject(new Person(1, "Jiyun", "Xie", 1984), PersonFunnel.INSTANCE);
    System.out.println("Code3 => " + code.asLong());
    long hash = code.asLong();
    int bucket = Hashing.consistentHash(code, 100);
    System.out.println("Bucket1 => " + bucket);
    bucket = Hashing.consistentHash(hash, 101);
    System.out.println("Bucket2 => " + bucket);
    for (int i = 0; i < 10; ++i) {
        System.out.println("HashTo5 => " + Hashing.consistentHash(i, 5));
        System.out.println("HashTo6 => " + Hashing.consistentHash(i, 6));
    }
    // BloomFilter
    BloomFilter<Person> friends = BloomFilter.create(PersonFunnel.INSTANCE, 500, 0.02);
    for (int i = 0; i < 500; ++i) {
        friends.put(new Person(i, "Jiyun", "Xie", 1984 + i));
    }
    int k = 0;
    for (int i = 0; i < 500; ++i) {
        if (!friends.mightContain(new Person(i, "Jiyun", "Xie", 1984 + i))) {
            System.out.println("Error1 => " + i);
            ++k;
        }
    }
    System.out.println("fnp => (should be 0)" + ((double) k / 500));
    k = 0;
    for (int i = 0; i < 1000; i += 2) {
        if (friends.mightContain(new Person(i, "Jiyun", "Xie", 1984 + i))) {
            //System.out.println("Error2 => " + i);
            ++k;
        }
    }
    System.out.println("fpp => " + ((double) k / 500));
}

From source file:com.google.caliper.model.Host.java

private void initHash(HashFunction hashFunction) {
    if (hash == 0) {
        this.hash = hashFunction.hashObject(this, HostFunnel.INSTANCE).asInt();
    }/*from  w  w  w  . j a va  2s  . c o m*/
}

From source file:com.streamsets.pipeline.stage.processor.fieldhasher.FieldHasherProcessor.java

private String generateHash(Record record, HashType hashType, Collection<String> fieldsToHash,
        boolean includeRecordHeader) throws StageException {
    try {// w  w  w . j a v  a 2s.co  m
        HashFunction hasher = HashingUtil.getHasher(hashType.getDigest());
        HashingUtil.RecordFunnel recordFunnel = HashingUtil.getRecordFunnel(fieldsToHash, includeRecordHeader);
        return hasher.hashObject(record, recordFunnel).toString();
    } catch (IllegalArgumentException e) {
        throw new StageException(Errors.HASH_00, hashType.getDigest(), e.toString(), e);
    }
}