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:org.omnaest.utils.structure.iterator.IterableUtils.java

/**
 * Generates a {@link Object#hashCode()} for a given {@link Iterable} and its elements. The hash code respects the hash codes of
 * the element instances and their order.
 * //from w w  w .j  a  v  a2 s.c om
 * @param iterable
 * @return
 */
public static int hashCode(Iterable<?> iterable) {
    final int prime = 31;
    int result = 1;
    if (iterable != null) {
        for (Object object : iterable) {
            result = prime * result + ((object == null) ? 0 : object.hashCode());
        }
    }
    return result;
}

From source file:edu.brown.utils.CollectionUtil.java

/**
 * Return an ordered array of the hash codes for the given items Any null
 * item will have a null hash code//w w  w .  ja va  2  s . co m
 */
public static int[] hashCode(Iterable<?> items) {
    List<Integer> codes = new ArrayList<Integer>();
    for (Object o : items) {
        codes.add(o != null ? o.hashCode() : null);
    }
    return CollectionUtil.toIntArray(codes);
}

From source file:org.kuali.ext.mm.ObjectUtil.java

/**
 * compute the hash code for the given object from the given fields
 *
 * @param object the given object/*from ww w.ja  v  a 2  s.  c o m*/
 * @param keyFields the specified fields
 * @return the hash code for the given object from the given fields
 */
public static int generateHashCode(Object object, List<String> keyFields) {
    if (object == null) {
        return 0;
    }

    final int prime = 31;
    int result = 1;
    for (String propertyName : keyFields) {
        try {
            Object propertyValue = PropertyUtils.getProperty(object, propertyName);
            result = prime * result + ((propertyValue == null) ? 0 : propertyValue.hashCode());
        } catch (Exception e) {
            LOG.info(e);
        }
    }
    return result;
}

From source file:com.amazon.carbonado.layout.LayoutFactory.java

private static int annValueHashCode(Object value) {
    Class type = value.getClass();
    if (!type.isArray()) {
        if (value instanceof String || type.isPrimitive()) {
            return value.hashCode();
        } else if (value instanceof Class) {
            // Use name for stable hash code.
            return ((Class) value).getName().hashCode();
        } else if (value instanceof Enum) {
            // Use name for stable hash code.
            return ((Enum) value).name().hashCode();
        } else if (value instanceof Annotation) {
            return annHashCode((Annotation) value);
        } else {//  w  ww  . j av  a2 s  .com
            return value.hashCode();
        }
    } else if (type == byte[].class) {
        return Arrays.hashCode((byte[]) value);
    } else if (type == char[].class) {
        return Arrays.hashCode((char[]) value);
    } else if (type == double[].class) {
        return Arrays.hashCode((double[]) value);
    } else if (type == float[].class) {
        return Arrays.hashCode((float[]) value);
    } else if (type == int[].class) {
        return Arrays.hashCode((int[]) value);
    } else if (type == long[].class) {
        return Arrays.hashCode((long[]) value);
    } else if (type == short[].class) {
        return Arrays.hashCode((short[]) value);
    } else if (type == boolean[].class) {
        return Arrays.hashCode((boolean[]) value);
    } else {
        return Arrays.hashCode((Object[]) value);
    }
}

From source file:com.kangdainfo.common.util.BeanUtil.java

/** This method takes a JavaBean and generates a standard toString() type result for it.
 * @param o JavaBean object to stringinate
 * @return STRINGIATION! Stringingating the countryside. Stringinating all the peasants.
 *///from w  w  w.j  av  a2s.c o m
public static String beanToString(Object o) {
    StringBuffer result = new StringBuffer();

    if (o == null) {
        return "--- null";
    }

    result.append("--- begin");
    result.append(o.getClass().getName());
    result.append(" hash: ");
    result.append(o.hashCode());
    result.append("\r\n");

    try {
        PropertyDescriptor[] pds = Introspector.getBeanInfo(o.getClass()).getPropertyDescriptors();

        for (int pdi = 0; pdi < pds.length; pdi++) {
            try {
                result.append(
                        "Property: " + pds[pdi].getName() + " Value: " + pds[pdi].getReadMethod().invoke(o));
            } catch (IllegalAccessException iae) {
                result.append("Property: " + pds[pdi].getName() + " (Illegal Access to Value) ");
            } catch (InvocationTargetException iae) {
                result.append(
                        "Property: " + pds[pdi].getName() + " (InvocationTargetException) " + iae.toString());
            } catch (Exception e) {
                result.append("Property: " + pds[pdi].getName() + " (Other Exception )" + e.toString());
            }

            result.append("\r\n");
        }
    } catch (IntrospectionException ie) {
        result.append("Introspection Exception: " + ie.toString());
        result.append("\r\n");
    }

    result.append("--- end ");
    result.append(o.getClass().getName());
    result.append(" hash: ");
    result.append(o.hashCode());
    result.append("\n");

    return result.toString();
}

From source file:com.ironiacorp.statistics.r.AbstractRClient.java

/**
 * @param ob// w  w  w  .  java2s  .c  o m
 * @return
 */
public static String variableIdentityNumber(Object ob) {
    return Integer.toString(Math.abs(ob.hashCode() + 1)) + RandomStringUtils.randomAlphabetic(6);
}

From source file:org.jboss.errai.codegen.util.CDIAnnotationUtils.java

/**
 * Helper method for generating a hash code for a member of an annotation.
 *
 * @param name the name of the member//from w w w .ja  v a  2  s  .  c  om
 * @param value the value of the member
 * @return a hash code for this member
 */
private static int hashMember(String name, Object value) {
    final int part1 = name.hashCode() * 127;
    if (value.getClass().isArray()) {
        return part1 ^ arrayMemberHash(value.getClass().getComponentType(), value);
    }
    if (value instanceof Annotation) {
        return part1 ^ hashCode((Annotation) value);
    }
    return part1 ^ value.hashCode();
}

From source file:com.redhat.rhn.testing.TestUtils.java

/**
 * Used to test the equals contract on objects.
 * The contract as specified by java.lang.Object states that if A.equals(B) is true
 * then B.equals(A) is also true. It also specifies that if A.equals(B) is true
 * then A.hashCode() will equals B.hashCode()
 * @param o1 object1//from w ww.  ja  v a2 s .com
 * @param o2 object2
 * @return both objects equal
 */
public static boolean equalTest(Object o1, Object o2) {
    // both null
    if (o1 == null && o2 == null) {
        return true;
    }
    // just one null
    if (o1 == null || o2 == null) {
        return false;
    }

    if (o1.equals(o2) != o2.equals(o1)) {
        return false;
    }
    return o1.hashCode() == o2.hashCode();
}

From source file:org.jbpm.bpel.graph.exe.MockIntegrationService.java

private static String getIdString(Object objectWithId) {
    Class classWithId = objectWithId.getClass();
    // get the class name excluding the package
    String className = ClassUtils.getShortClassName(classWithId);
    // obtain the identifier getter method according to jBPM conventions
    String idString = "0";
    try {/*from  w w w .  j a  va  2s .c  o m*/
        Method idGetter = classWithId.getMethod("getId", null);
        idGetter.setAccessible(true);
        Long idWrapper = (Long) idGetter.invoke(objectWithId, null);
        long id = idWrapper.longValue();
        if (id != 0L) {
            idString = Long.toHexString(id);
        } else {
            // object is transient, fall back to hash code
            idString = Integer.toHexString(objectWithId.hashCode());
        }
    } catch (NoSuchMethodException e) {
        // no id getter, fall back to hash code
        idString = Integer.toHexString(objectWithId.hashCode());
    } catch (IllegalAccessException e) {
        // we made the getter accessible, should not happen
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        // strange a getter throws a checked exception
        e.printStackTrace();
    }
    return className + idString;
}

From source file:wicket.util.lang.Objects.java

/**
 * returns hashcode of the object by calling obj.hashcode(). safe to use
 * when obj is null.//from w  ww.  j  a v a 2s .com
 * 
 * @param obj
 * @return hashcode of the object or 0 if obj is null
 */
public static int hashCode(final Object... obj) {
    int result = 37;
    for (Object o : obj) {
        result = 37 * result + (o != null ? o.hashCode() : 0);
    }
    return result;
}