Example usage for com.google.common.hash Hasher putDouble

List of usage examples for com.google.common.hash Hasher putDouble

Introduction

In this page you can find the example usage for com.google.common.hash Hasher putDouble.

Prototype

@Override
Hasher putDouble(double d);

Source Link

Document

Equivalent to putLong(Double.doubleToRawLongBits(d)) .

Usage

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

private static void populateHasher(final Object data, final Hasher h) {
    if (data != null) {
        if (data instanceof Number) {
            final Number d = (Number) data;
            if (data instanceof Integer) {
                h.putInt(d.intValue());/*from  ww w.j a  v  a  2  s.  co  m*/
            } else if (data instanceof Double) {
                h.putDouble(d.doubleValue());
            } else if (data instanceof Long) {
                h.putLong(d.longValue());
            } else if (data instanceof Float) {
                h.putFloat(d.floatValue());
            } else if (data instanceof Byte) {
                h.putByte(d.byteValue());
            } else if (data instanceof Short) {
                h.putShort(d.shortValue());
            } else {
                h.putInt(data.hashCode());
            }
        } else if (data instanceof CharSequence) {
            h.putString((CharSequence) data, CHARSET);
        } else if (data.getClass().isArray()) {
            final int size = Array.getLength(data);
            for (int i = 0; i < size; i++) {
                populateHasher(Array.get(data, i), h);
            }
        } else if (data instanceof Iterable) {
            for (final Object o : (Iterable<?>) data) {
                populateHasher(o, h);
            }
        } else {
            h.putInt(data.hashCode());
        }
    }
}

From source file:nextflow.util.CacheHelper.java

public static Hasher hasher(Hasher hasher, Object value, HashMode mode) {

    if (value == null)
        return hasher;

    if (value instanceof Boolean)
        return hasher.putBoolean((Boolean) value);

    if (value instanceof Short)
        return hasher.putShort((Short) value);

    if (value instanceof Integer)
        return hasher.putInt((Integer) value);

    if (value instanceof Long)
        return hasher.putLong((Long) value);

    if (value instanceof Float)
        return hasher.putFloat((Float) value);

    if (value instanceof Double)
        return hasher.putDouble((Double) value);

    if (value instanceof Number)
        // reduce all other number types (BigInteger, BigDecimal, AtomicXxx, etc) to string equivalent
        return hasher.putUnencodedChars(value.toString());

    if (value instanceof Character)
        return hasher.putChar((Character) value);

    if (value instanceof CharSequence)
        return hasher.putUnencodedChars((CharSequence) value);

    if (value instanceof Byte)
        return hasher.putByte((Byte) value);

    if (value instanceof byte[])
        return hasher.putBytes((byte[]) value);

    if (value instanceof Object[]) {
        for (Object item : ((Object[]) value))
            hasher = CacheHelper.hasher(hasher, item, mode);
        return hasher;
    }//from ww  w.ja v a  2  s .c  om

    if (value instanceof Map) {
        // note: should map be order invariant as Set ?
        for (Object item : ((Map) value).values())
            hasher = CacheHelper.hasher(hasher, item, mode);
        return hasher;
    }

    if (value instanceof Bag || value instanceof Set)
        return hashUnorderedCollection(hasher, (Collection) value, mode);

    if (value instanceof Collection) {
        for (Object item : ((Collection) value))
            hasher = CacheHelper.hasher(hasher, item, mode);
        return hasher;
    }

    if (value instanceof FileHolder)
        return CacheHelper.hasher(hasher, ((FileHolder) value).getSourceObj(), mode);

    if (value instanceof Path)
        return hashFile(hasher, (Path) value, mode);

    if (value instanceof java.io.File)
        return hashFile(hasher, (java.io.File) value, mode);

    if (value instanceof UUID) {
        UUID uuid = (UUID) value;
        return hasher.putLong(uuid.getMostSignificantBits()).putLong(uuid.getLeastSignificantBits());
    }

    log.debug("[WARN] Unknown hashing type: {} -- {}", value.getClass(), value);
    return hasher.putInt(value.hashCode());
}

From source file:io.druid.query.aggregation.cardinality.types.DoubleCardinalityAggregatorColumnSelectorStrategy.java

@Override
public void hashRow(BaseDoubleColumnValueSelector dimSelector, Hasher hasher) {
    hasher.putDouble(dimSelector.getDouble());
}

From source file:com.spotify.heroic.metric.Point.java

@Override
public void hash(final Hasher hasher) {
    hasher.putInt(MetricType.POINT.ordinal());
    hasher.putDouble(value);
}

From source file:org.apache.druid.query.aggregation.cardinality.types.DoubleCardinalityAggregatorColumnSelectorStrategy.java

@Override
public void hashRow(BaseDoubleColumnValueSelector selector, Hasher hasher) {
    if (NullHandling.replaceWithDefault() || !selector.isNull()) {
        hasher.putDouble(selector.getDouble());
    }/*from w ww  . j a  v a2  s .com*/
}

From source file:com.spotify.heroic.metric.Spread.java

@Override
public void hash(final Hasher hasher) {
    hasher.putInt(MetricType.SPREAD.ordinal());
    hasher.putLong(timestamp);/*  w  ww .  ja  v  a 2 s .  co m*/
    hasher.putLong(count);
    hasher.putDouble(sum);
    hasher.putDouble(sum2);
    hasher.putDouble(min);
    hasher.putDouble(max);
}

From source file:com.facebook.buck.util.hash.AppendingHasher.java

@Override
public Hasher putDouble(double d) {
    for (Hasher hasher : hashers) {
        hasher.putDouble(d);
    }//from   w  w w .  j a  v a 2 s  .  c om
    return this;
}

From source file:edu.harvard.hms.dbmi.bd2k.irct.join.HashJoinImpl.java

private HashCode hashResultSetRow(int[] columns, PrimitiveDataType[] columnDataTypes, ResultSet resultSet)
        throws ResultSetException {
    Hasher columnHash = hashFunction.newHasher();

    for (int columnI = 0; columnI < columns.length; columnI++) {
        int column = columns[columnI];
        switch (columnDataTypes[columnI].getName()) {
        case "boolean":
            columnHash.putBoolean(resultSet.getBoolean(column));
            break;
        case "byte":
            columnHash.putByte(resultSet.getByte(column));
            break;
        case "double":
            columnHash.putDouble(resultSet.getDouble(column));
            break;
        case "float":
            columnHash.putFloat(resultSet.getFloat(column));
            break;
        case "integer":
            columnHash.putInt(resultSet.getInt(column));
            break;
        case "long":
            columnHash.putLong(resultSet.getLong(column));
            break;
        default:/*  w  w  w .  jav  a 2s . c  om*/
            columnHash.putString(resultSet.getString(column), Charsets.UTF_8);
            break;
        }
    }

    return columnHash.hash();
}

From source file:com.facebook.buck.json.JsonObjectHashing.java

/**
 * Given a {@link Hasher} and a parsed BUCK file object, updates the Hasher
 * with the contents of the JSON object.
 *///from www .  j  a v  a  2  s. c o  m
public static void hashJsonObject(Hasher hasher, @Nullable Object obj) {
    if (obj instanceof Map) {
        Map<?, ?> map = (Map<?, ?>) obj;
        ImmutableSortedMap.Builder<String, Optional<Object>> sortedMapBuilder = ImmutableSortedMap
                .naturalOrder();
        for (Map.Entry<?, ?> entry : map.entrySet()) {
            Object key = entry.getKey();
            if (!(key instanceof String)) {
                throw new RuntimeException(String.format(
                        "Keys of JSON maps are expected to be strings. Actual type: %s, contents: %s",
                        key.getClass().getName(), key));
            }
            Object value = entry.getValue();
            if (value != null) {
                sortedMapBuilder.put((String) key, Optional.of(value));
            }
        }
        ImmutableSortedMap<String, Optional<Object>> sortedMap = sortedMapBuilder.build();
        hasher.putInt(HashedObjectType.MAP.ordinal());
        hasher.putInt(sortedMap.size());
        for (Map.Entry<String, Optional<Object>> entry : sortedMap.entrySet()) {
            hashJsonObject(hasher, entry.getKey());
            if (entry.getValue().isPresent()) {
                hashJsonObject(hasher, entry.getValue().get());
            } else {
                hashJsonObject(hasher, null);
            }
        }
    } else if (obj instanceof Collection) {
        Collection<?> collection = (Collection<?>) obj;
        hasher.putInt(HashedObjectType.LIST.ordinal());
        hasher.putInt(collection.size());
        for (Object collectionEntry : collection) {
            hashJsonObject(hasher, collectionEntry);
        }
    } else if (obj instanceof String) {
        hasher.putInt(HashedObjectType.STRING.ordinal());
        byte[] stringBytes = ((String) obj).getBytes(Charsets.UTF_8);
        hasher.putInt(stringBytes.length);
        hasher.putBytes(stringBytes);
    } else if (obj instanceof Boolean) {
        hasher.putInt(HashedObjectType.BOOLEAN.ordinal());
        hasher.putBoolean((boolean) obj);
    } else if (obj instanceof Number) {
        // This is gross, but it mimics the logic originally in RawParser.
        Number number = (Number) obj;
        if (number.longValue() == number.doubleValue()) {
            hasher.putInt(HashedObjectType.LONG.ordinal());
            hasher.putLong(number.longValue());
        } else {
            hasher.putInt(HashedObjectType.DOUBLE.ordinal());
            hasher.putDouble(number.doubleValue());
        }
    } else if (obj instanceof Void || obj == null) {
        hasher.putInt(HashedObjectType.NULL.ordinal());
    } else {
        throw new RuntimeException(String.format("Unsupported object %s (class %s)", obj, obj.getClass()));
    }
}