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:org.apache.niolex.common.guava.GuavaHashing.java

/**
 * @param args/*from w w w . jav a2s  .c  o 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:org.onosproject.net.link.ProbedLinkProvider.java

/**
 * Build a stringified MAC address using the ClusterMetadata hash for uniqueness.
 * Form of MAC is "02:eb" followed by four bytes of clusterMetadata hash.
 *
 * @param cm cluster metadata// w w  w. j a v a 2  s .c o m
 * @return stringified mac address
 */
static String fingerprintMac(ClusterMetadata cm) {
    if (cm == null) {
        return DEFAULT_MAC;
    }

    HashFunction hf = Hashing.murmur3_32();
    HashCode hc = hf.newHasher().putObject(cm, ClusterMetadata.HASH_FUNNEL).hash();
    int unqf = hc.asInt();

    StringBuilder sb = new StringBuilder();
    sb.append("02:eb");
    for (int i = 0; i < 4; i++) {
        byte b = (byte) (unqf >> i * 8);
        sb.append(String.format(":%02X", b));
    }
    return sb.toString();
}

From source file:org.janusgraph.diskstorage.util.HashingUtil.java

public static final StaticBuffer hashPrefixKey(final HashLength hashPrefixLen, final StaticBuffer key) {
    final int prefixLen = hashPrefixLen.length();
    final StaticBuffer.Factory<HashCode> hashFactory;
    switch (hashPrefixLen) {
    case SHORT:/*ww  w  .  java 2s .  c o m*/
        hashFactory = SHORT_HASH_FACTORY;
        break;
    case LONG:
        hashFactory = LONG_HASH_FACTORY;
        break;
    default:
        throw new IllegalArgumentException("Unknown hash prefix: " + hashPrefixLen);
    }

    HashCode hashcode = key.as(hashFactory);
    WriteByteBuffer newKey = new WriteByteBuffer(prefixLen + key.length());
    assert prefixLen == 4 || prefixLen == 8;
    if (prefixLen == 4)
        newKey.putInt(hashcode.asInt());
    else
        newKey.putLong(hashcode.asLong());
    newKey.putBytes(key);
    return newKey.getStaticBuffer();
}

From source file:edu.umd.marbl.mhap.sketch.HashUtils.java

public final static int[] computeHashesIntInt(int obj, int numWords, int seed) {
    int[] hashes = new int[numWords];

    HashFunction hf = Hashing.murmur3_32(seed);

    for (int iter = 0; iter < numWords; iter++) {
        HashCode hc = hf.newHasher().putInt(obj).putInt(iter).hash();

        hashes[iter] = hc.asInt();
    }//from   w  w w.  j a v  a  2 s  .c o  m

    return hashes;
}

From source file:edu.umd.marbl.mhap.sketch.HashUtils.java

public final static int[] computeHashesIntLong(long obj, int numWords, int seed) {
    int[] hashes = new int[numWords];

    HashFunction hf = Hashing.murmur3_32(seed);

    for (int iter = 0; iter < numWords; iter++) {
        HashCode hc = hf.newHasher().putLong(obj).putInt(iter).hash();

        hashes[iter] = hc.asInt();
    }//from  w w w.  j av  a  2  s  .  c o  m

    return hashes;
}

From source file:edu.umd.marbl.mhap.sketch.HashUtils.java

public final static int[] computeHashesIntFloat(float obj, int numWords, int seed) {
    int[] hashes = new int[numWords];

    HashFunction hf = Hashing.murmur3_32(seed);

    for (int iter = 0; iter < numWords; iter++) {
        HashCode hc = hf.newHasher().putFloat(obj).putInt(iter).hash();

        hashes[iter] = hc.asInt();
    }// ww  w .  j a v  a2s.co m

    return hashes;
}

From source file:edu.umd.marbl.mhap.sketch.HashUtils.java

public final static int[] computeHashesIntDouble(double obj, int numWords, int seed) {
    int[] hashes = new int[numWords];

    HashFunction hf = Hashing.murmur3_32(seed);

    for (int iter = 0; iter < numWords; iter++) {
        HashCode hc = hf.newHasher().putDouble(obj).putInt(iter).hash();

        hashes[iter] = hc.asInt();
    }/*from www . j  a va2  s.  c  o m*/

    return hashes;
}

From source file:edu.umd.marbl.mhap.sketch.HashUtils.java

public static double[] randomStringGuassianVector(String str, int n, int seed) {
    int[] seeds = new int[4];
    for (int iter = 0; iter < 4; iter++) {
        HashFunction hf = Hashing.murmur3_32(seed * 4 + iter);
        HashCode hc = hf.newHasher().putUnencodedChars(str).hash();

        seeds[iter] = hc.asInt();
    }//from   ww  w  .  j a v a  2  s  .c o  m

    //now generate the guassian
    MersenneTwisterFast rand = new MersenneTwisterFast(seeds);

    double[] vec = new double[n];
    for (int iter = 0; iter < n; iter++) {
        vec[iter] = rand.nextGaussian();
    }

    //normalize
    double norm = BasicMath.norm(vec);
    if (norm < 1.0e-10)
        return vec;

    return BasicMath.mult(vec, 1.0 / norm);
}

From source file:edu.umd.marbl.mhap.sketch.HashUtils.java

public final static int[] computeHashesIntString(String obj, int numWords, int seed) {
    int[] hashes = new int[numWords];

    HashFunction hf = Hashing.murmur3_32(seed);

    for (int iter = 0; iter < numWords; iter++) {
        HashCode hc = hf.newHasher().putUnencodedChars(obj).putInt(iter).hash();

        hashes[iter] = hc.asInt();
    }/* w  w w.  j  a  v  a  2s  . c  o m*/

    return hashes;
}

From source file:edu.umd.marbl.mhap.sketch.HashUtils.java

public final static int[] computeSequenceHashes(final String seq, final int nGramSize) {
    HashFunction hf = Hashing.murmur3_32(0);

    int[] hashes = new int[seq.length() - nGramSize + 1];
    for (int iter = 0; iter < hashes.length; iter++) {
        HashCode hc = hf.newHasher().putUnencodedChars(seq.substring(iter, iter + nGramSize)).hash();
        hashes[iter] = hc.asInt();
    }//from   w  w  w  .java2s. c om

    return hashes;
}