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
Experience pair programming with AI  Get Codota for Java
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
Experience pair programming with AI  Get Codota for Java
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
Experience pair programming with AI  Get Codota for Java
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
Experience pair programming with AI  Get Codota for Java
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
Experience pair programming with AI  Get Codota for Java
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
Experience pair programming with AI  Get Codota for Java
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
Experience pair programming with AI  Get Codota for Java
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
Experience pair programming with AI  Get Codota for Java
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
Experience pair programming with AI  Get Codota for Java
See Code Examples for Guava HashFunction Methods: