Example usage for java.lang Object hashCode

List of usage examples for java.lang Object hashCode

Introduction

In this page you can find the example usage for java.lang Object hashCode.

Prototype

@HotSpotIntrinsicCandidate
public native int hashCode();

Source Link

Document

Returns a hash code value for the object.

Usage

From source file:com.egt.core.aplicacion.Bitacora.java

private static String getFirmaMetodo(Object objeto, String metodo, int argumentos) {
    //      String string = objeto == null ? "" : objeto.getClass().getName();
    String string = objeto == null ? "" : objeto.getClass().getSimpleName() + "#" + objeto.hashCode();
    return getFirmaMetodo(string, metodo, argumentos);
}

From source file:com.xwtec.xwserver.util.json.util.JSONUtils.java

/**
 * Returns the hashcode of value.<br>
 * If null it will return JSONNull.getInstance().hashCode().<br>
 * If value is JSON, JSONFunction or String, value.hashCode is returned,
 * otherwise the value is transformed to a String an its hashcode is
 * returned./*from w w w  .  jav  a2  s  .  co  m*/
 */
public static int hashCode(Object value) {
    if (value == null) {
        return JSONNull.getInstance().hashCode();
    } else if (value instanceof JSON || value instanceof String || value instanceof JSONFunction) {
        return value.hashCode();
    } else {
        return String.valueOf(value).hashCode();
    }
}

From source file:com.google.code.simplestuff.bean.SimpleBean.java

/**
 * /*from  www. j a va2 s  .  c o  m*/
 * Returns the hashCode basing considering only the {@link BusinessField}
 * annotated fields.
 * 
 * @param bean The bean.
 * @return The hashCode result.
 * @throws IllegalArgumentException If the bean is not a Business Object.
 */
public static int hashCode(Object bean) {

    if (bean == null) {
        throw new IllegalArgumentException("The bean passed is null!!!");
    }

    BusinessObjectDescriptor businessObjectInfo = BusinessObjectContext
            .getBusinessObjectDescriptor(bean.getClass());

    // We don't need here a not null check since by contract the
    // getBusinessObjectDescriptor method always returns an abject.
    if (businessObjectInfo.getNearestBusinessObjectClass() == null) {
        return bean.hashCode();
        // throw new IllegalArgumentException(
        // "The bean passed is not annotated in the hierarchy as Business Object!!!");
    }

    Collection<Field> annotatedField = businessObjectInfo.getAnnotatedFields();

    HashCodeBuilder builder = new HashCodeBuilder();
    for (Field field : annotatedField) {
        field.setAccessible(true);
        final BusinessField fieldAnnotation = field.getAnnotation(BusinessField.class);
        if ((fieldAnnotation != null) && (fieldAnnotation.includeInHashCode())) {
            try {
                // Vincenzo Vitale(vita) May 23, 2007 2:39:26 PM: some
                // date implementations give not equals values...
                if ((ClassUtils.isAssignable(field.getType(), Date.class))
                        || (ClassUtils.isAssignable(field.getType(), Calendar.class))) {
                    Object fieldValue = PropertyUtils.getProperty(bean, field.getName());
                    if (fieldValue != null) {
                        builder.append(DateUtils.round(fieldValue, Calendar.MILLISECOND));
                    }

                } else {
                    builder.append(PropertyUtils.getProperty(bean, field.getName()));
                }
            } catch (IllegalAccessException e) {
                if (log.isDebugEnabled()) {
                    log.debug("IllegalAccessException exception when calculating the hashcode of class"
                            + bean.getClass().toString(), e);
                }
            } catch (InvocationTargetException e) {
                if (log.isDebugEnabled()) {
                    log.debug("InvocationTargetException exception when calculating the hashcode of class"
                            + bean.getClass().toString(), e);
                }
            } catch (NoSuchMethodException e) {
                if (log.isDebugEnabled()) {
                    log.debug("NoSuchMethodException exception when calculating the hashcode of class"
                            + bean.getClass().toString(), e);
                }
            }
        }
    }

    return builder.toHashCode();

}

From source file:Main.java

/**
 * Returns o.toString() unless it throws an exception (which causes it to be
 * stored in unrenderableClasses) or already was present in
 * unrenderableClasses. If so, the same string is returned as would have
 * been returned by Object.toString(). Arrays get special treatment as they
 * don't have usable toString methods.//from  w ww.j a v  a2s . co m
 * 
 * @param o
 *            incoming object to render.
 * @return
 */

public static String render(Object o) {
    if (o == null) {
        return String.valueOf(o);
    }
    Class<?> objectClass = o.getClass();

    if (unrenderableClasses.containsKey(objectClass) == false) {
        try {
            if (objectClass.isArray()) {
                return renderArray(o, objectClass).toString();
            } else {
                return o.toString();
            }
        } catch (Exception e) {
            Long now = new Long(System.currentTimeMillis());

            System.err.println(
                    "Disabling exception throwing class " + objectClass.getName() + ", " + e.getMessage());

            unrenderableClasses.put(objectClass, now);
        }
    }
    String name = o.getClass().getName();
    return name + "@" + Integer.toHexString(o.hashCode());
}

From source file:org.apache.hadoop.hive.ql.udf.UDFDefaultSampleHashFn.java

public int evaluate(Object o) {
    return o.hashCode();
}

From source file:de.huberlin.cuneiform.dag.Invocation.java

private static long add(long a, Object b) {

    long hash;//from   w w w . j  a va  2s .  c  o  m

    hash = a;
    hash = (hash + (abs(b.hashCode()) % PRIME)) % PRIME;

    return hash;
}

From source file:org.apache.hadoop.hive.ql.udf.UDFDefaultSampleHashFn.java

public int evaluate(Object o1, Object o2) {
    return o1.hashCode() ^ o2.hashCode();
}

From source file:org.grouchotools.jsrules.util.JsonBean.java

@Override
public boolean equals(Object that) {
    return that.hashCode() == this.hashCode() && that.getClass() == this.getClass();
}

From source file:SoftHashMap.java

/**
 * Returns a hash value for the specified object.
 * In addition to the object's own hashCode, this method applies a
 * "supplemental hash function," which defends against poor quality hash
 * functions.//from  w  ww .  j  a  v  a 2  s .  co  m
 * This is critical because HashMap uses power-of two length hash tables.
 * <p>
 * The shift distances in this function were chosen as the result of an
 * automated search over the entire four-dimensional search space.
 */
static int hash(Object x) {
    int h = x.hashCode();

    h += ~(h << 9);
    h ^= (h >>> 14);
    h += (h << 4);
    h ^= (h >>> 10);

    return h;
}

From source file:com.google.gwt.emultest.java.util.ComparatorTest.java

/**
 * Compares returns reverse hash order./*from  w w w  .j  a v a 2 s  .c om*/
 */
@Override
public int compare(Object arg0, Object arg1) {
    int a = arg0.hashCode();
    int b = arg1.hashCode();
    int accum;
    if (a == b) {
        accum = 0;
    } else if (a > b) {
        accum = 1;
    } else {
        accum = -1;
    }
    return accum;
}