Guava HashFunction - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Guava HashFunction class.

This code example shows how to use the following methods:newHasher
public class HashOutputStream extends OutputStream {
 
    private Hasher hasher;
 
    public HashOutputStream(HashFunction hf) {
        hasher = hf.newHasher();
    } 
 
    @Override 
    public void write(int b) throws IOException {
        hasher.putInt(b);
    } 
 
    public HashCode getHashCode() {
        return hasher.hash();
    } 
 
} 
5
CodeRank
This code example shows how to use the following methods:newHasher
            .withNoArguments() 
            .will(returnValue(this.mockHasher.proxy()));
        this.abstractChecksumProvider = new AbstractChecksumProvider(
                (HashFunction) mockHashFunction.proxy());
    } 
 
    @Test
    public void testPutBytesWithByteArray() { 
        this.mockHasher.expects(once()).method("putBytes");
        Object actual = this.abstractChecksumProvider.putBytes("abc".getBytes());
        Assert.assertTrue(actual instanceof AbstractChecksumProvider);
    } 
 
    @Test
    public void testPutBytesWithByteBuffer() { 
        this.mockHasher.expects(once()).method("putBytes");
        Object actual = this.abstractChecksumProvider.putBytes(
                ByteBuffer.wrap("abc".getBytes()));
        Assert.assertTrue(actual instanceof AbstractChecksumProvider);
    } 
5
CodeRank
This code example shows how to use the following methods:hashString
		float hue = floatFromText(text, 0f, 1f);
		return HSBtoColor(hue, SATURATION, LIGHTNESS);
	} 
 
	private static float floatFromText(String text, float low, float high) {
		int hash = HASH_FUNCTION.hashString(text, Charsets.UTF_8).asInt();
		 
		final float old_min = Integer.MIN_VALUE;
		final float old_max = Integer.MAX_VALUE;
		float hue = ((hash - old_min) / (old_max - old_min)) * (high - low);
		 
		// Return limited in range (low, high) 
		return Math.max(low, Math.min(hue, high));
	} 
 
	/** 
	 * Adapted from Color.java in Android 
	 *  
	 * See https://github.com/android/platform_frameworks_base/blob/master/graphics/java/android/graphics/Color.java 
	 */ 
5
CodeRank
This code example shows how to use the following methods:newHasher
     * Protected ctor. There should not be an instance of this class. 
     * @param hashFunction 
     */ 
    protected AbstractChecksumProvider(HashFunction hashFunction) {
        this.hashFunction = hashFunction;
        this.hasher = this.hashFunction.newHasher();
    } 
 
    /** 
     * @see ChecksumProvider#putBytes(byte[]) 
     */ 
    public ChecksumProvider putBytes(byte[] byteArray) {
        return this.putBytes(byteArray, 0, byteArray.length);
    } 
 
    /** 
     * @see ChecksumProvider#putBytes(byte[], int, int) 
     */ 
    public ChecksumProvider putBytes(byte[] byteArray, int offset, int length) {
        this.verifyState(this.hashBytes, this.hashString);
5
CodeRank
This code example shows how to use the following methods:hashBytes
   * 
   * @param data data to hash 
   * @return 128 bits of hash result 
   */ 
  public byte[] hash(byte[] data) {
    HashCode hashCode = byteArrayHasher.hashBytes(data);
 
    return hashCode.asBytes();
  } 
 
  public long hashToLong(byte[] data) {
    HashCode hashCode = byteArrayHasher.hashBytes(data);
 
    return hashCode.asLong();
  } 
 
 
  /** 
   * A special version for long integers 
   * 
5
CodeRank
This code example shows how to use the following methods:hashBytes
key.indexTo(depth);
        int startPosition = key.getIndex();
        key.indexTo(depth + 1);
        int endPosition = key.getIndex();
        return hashFunction.hashBytes(key.getEncodedBytes(), startPosition, endPosition - startPosition).asLong();
    } 
 
    // Object state 
 
    private final HashFunction hashFunction = Hashing.goodFastHash(64); // Because we're returning longs
} 
5
CodeRank
This code example shows how to use the following methods:newHasher
     * 
     * @param hashFunction 
     */ 
    public Anonymizer(final String hashFunction) {
        hf = HASH_FUNCTIONS.get(hashFunction);
        hasher = hf.newHasher();
    } 
 
    /** 
     * 
     * @param ipAddress 
     * @param userAgent 
     * @return 
     */ 
    public final String generateHash(final String ipAddress, final String userAgent) {
        HashCode hc = this.hasher.
            putString(ipAddress).
            putString(userAgent).
            hash(); 
        return hc.toString();
4
CodeRank
This code example shows how to use the following methods:newHasher
     * Create a description writer for a particular hash function. 
     * @param func The hash function. 
     * @return A description writer that computes a hash using {@code func}. 
     */ 
    public static HashDescriptionWriter hashWriter(HashFunction func) {
        return new HashDescriptionWriter(func.newHasher());
    } 
 
    /** 
     * Construct a new description writer that outputs a string. 
     * @return A string description writer. 
     */ 
    public static StringDescriptionWriter stringWriter() { 
        return new StringDescriptionWriter(); 
    } 
 
    /** 
     * Get a default describer.  This describer uses the following algorithm: 
     * <ol> 
     * <li>If the object is {@link Describable}, call its its {@link Describable#describeTo(DescriptionWriter)} method.</li> 
3
CodeRank
This code example shows how to use the following methods:newHasher
This code example shows how to use the following methods:newHasher
    /** 
     * Repository@revision should be sufficient to uniquely identify a revision 
     * @return  a loosely constructed hashcode converted to String 
     */ 
    public String id() {
        return hf.newHasher()
                .putUnencodedChars(repository)
                .putLong(revision)
                .hash()
                .toString();
    } 
 
 
} 
4
CodeRank
This code example shows how to use the following methods:newHasher
        reset(); 
    } 
 
    @Override 
    public void reset() { 
        this.hasher = hf.newHasher();
    } 
 
    @Override 
    public void update(byte b) {
        hasher.putByte(b);
    } 
 
    @Override 
    public void update(byte[] array) {
        hasher.putBytes(array);
    } 
 
    @Override 
    public void update(ByteBuffer buffer) {
4
CodeRank
This code example shows how to use the following methods:hashLong
   * If you need to keep shorter hashes around (e.g., for storage), make sure to drop bits from the 
   * most significant side, as the hyperloglog implementation uses bits from least significant 
   * to most significant. 
   */ 
  public static long computeHash(long value) {
    return HASH.hashLong(value).asLong();
  } 
} 
4
CodeRank
This code example shows how to use the following methods:newHasher
			add(node);
		} 
	} 
 
	public boolean add(N node) { 
		ring.put(hashFunction.newHasher().putObject(node, nodeFunnel).hash().asLong(), node); 
		return true; 
	} 
 
	public boolean remove(N node) {
		return node == ring.remove(hashFunction.newHasher().putObject(node, nodeFunnel).hash().asLong()); 
	} 
 
	public N get(K key) { 
		Long hash = hashFunction.newHasher().putObject(key, keyFunnel).hash().asLong();
		if (!ring.containsKey(hash)) {
			SortedMap<Long, N> tailMap = ring.tailMap(hash);
			hash = tailMap.isEmpty() ? ring.firstKey() : tailMap.firstKey();
		} 
		return ring.get(hash);
4
CodeRank
This code example shows how to use the following methods:hashString
    } 
 
    @Override 
    public int hashCode() 
    { 
        return HASH_FUNCTION.hashString(_dn + _delegationId, UTF_8).asInt();
    } 
 
    @Override 
    public boolean equals(Object otherRaw) 
    { 
        if (otherRaw == this) {
            return true; 
        } 
 
        if(!(otherRaw instanceof DelegationIdentity)) { 
            return false; 
        } 
 
        DelegationIdentity other = (DelegationIdentity) otherRaw;
4
CodeRank
This code example shows how to use the following methods:newHasher
This code example shows how to use the following methods:hashString
     * @param strToHash 
     * @return the {@code ObjectId} generated from the string 
     */ 
    public static ObjectId forString(final String strToHash) {
        Preconditions.checkNotNull(strToHash);
        HashCode hashCode = HASH_FUNCTION.hashString(strToHash, Charset.forName("UTF-8"));
        return new ObjectId(hashCode.asBytes(), false);
    } 
 
    /** 
     * Returns the value of this ObjectId's internal hash at the given index without having to go 
     * through {@link #getRawValue()} and hence create excessive defensive copies of the byte array. 
     *  
     * @param index the index of the byte inside this objectid's internal hash to return 
     * @return the byte at the given index as an integer 
     */ 
    public int byteN(int index) {
        int b = this.hashCode[index] & 0xFF;
        return b;
    } 
3
CodeRank
This code example shows how to use the following methods:hashString
This code example shows how to use the following methods:hashString
        return publicKey.getEncrypter().apply(
                BitVectors.concatenate( searchHash, BitVectors.randomVector( searchHash.size() ) ) );
    } 
 
    public BitVector hash( String term ) {
        BitVector hash = BitVectors.fromBytes( hashBits, hf.hashString( term, Charsets.UTF_8 ).asBytes() );
        BitVector halfHash = hash.partFromTo( 0, 63 );
        halfHash.xor( hash.partFromTo( 64, 127 ) );
        return halfHash;
    } 
 
    @JsonProperty( LEFT_SQUARING_MATRIX )
    public EnhancedBitMatrix getLeftSquaringMatrix() { 
        return leftSquaringMatrix;
    } 
 
    @JsonProperty( RIGHT_SQUARING_MATRIX )
    public EnhancedBitMatrix getRightSquaringMatrix() { 
        return rightSquaringMatrix;
    } 
3
CodeRank
This code example shows how to use the following methods:hashInt
        this.every = every;
    } 
 
    @Override 
    public boolean apply(final Instance<AttributesMap> instance) {
        int hc = hashFunction.hashInt(instance.getAttributes().hashCode()).asInt();
        return Math.abs(hc) % every == 0;
    } 
} 
4
CodeRank
This code example shows how to use the following methods:hashBytes
   * 
   * @param id The byte array that may have been previously seen. 
   * @return Whether the byte array is contained in the ByteArrayFilter. 
   */ 
  public boolean containsAndAdd(byte[] id) {
    HashCode code = HASH_FUNC.hashBytes(id);
    int index = Math.abs(code.asInt()) & sizeMask;
    byte[] oldId = array.getAndSet(index, id);
    return Arrays.equals(id, oldId);
  } 
 
  /** 
   * Returns the size of the underlying array. Welp. 
   * 
   * @return The size of the underlying array. 
   */ 
  public int getSize() { 
    return array.length();
  } 
} 
4
CodeRank
This code example shows how to use the following methods:hashUnencodedChars
public class HashMD5UDF extends UDF { 
	private HashFunction hash = Hashing.md5();
 
 
	public Long evaluate( String str) {
		HashCode hc = hash.hashUnencodedChars( str);
 
		return hc.asLong();
	} 
} 
4
CodeRank
This code example shows how to use the following methods:hashUnencodedChars
	public void addHash( long hash) {
		addHashItem( hash, Long.toString( hash));
	} 
	 
	public void addItem( String str) {
		HashCode hc = HASH.hashUnencodedChars( str);
		this.addHashItem( hc.asLong(), str);
	} 
	 
	public List<String> getMinHashItems() {
	  return new ArrayList(this.sortedMap.values());
	} 
	 
	public SortedMap<Long,String> getHashItemMap() {
		return this.sortedMap;
	} 
	 
	public List<Long> getMinHashes() {
	   return new ArrayList( this.sortedMap.keySet());
	} 
3
CodeRank
See Code Examples for Guava HashFunction Methods: