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:HashMap.java

/**
 * Returns the hashCode for a value.//from   w ww  . j  a va  2s  .  c  o m
 */
protected int valueHashCode(Object v) {
    return (v == null) ? 0 : v.hashCode();
}

From source file:org.springmodules.util.Objects.java

/**
 * <p>//from  w w  w .  ja v a2s  . com
 * Returns the value of <code>{@link Object#hashCode()}</code>. If the
 * object is an array, this method will delegate to any of the
 * <code>nullSafeHashCode</code> methods for arrays in this class.
 * </p>
 * 
 * <p>
 * If the object is <code>null</code>, this method returns 0.
 * </p>
 * 
 * @param obj
 *          the object whose hash value to compute.
 * @return the hash code of the given object.
 * @see #nullSafeHashCode(boolean[])
 * @see #nullSafeHashCode(byte[])
 * @see #nullSafeHashCode(char[])
 * @see #nullSafeHashCode(double[])
 * @see #nullSafeHashCode(float[])
 * @see #nullSafeHashCode(int[])
 * @see #nullSafeHashCode(long[])
 * @see #nullSafeHashCode(Object[])
 * @see #nullSafeHashCode(short[])
 */
public static int nullSafeHashCode(Object obj) {
    if (obj == null)
        return 0;

    if (obj instanceof boolean[]) {
        return nullSafeHashCode((boolean[]) obj);
    }
    if (obj instanceof byte[]) {
        return nullSafeHashCode((byte[]) obj);
    }
    if (obj instanceof char[]) {
        return nullSafeHashCode((char[]) obj);
    }
    if (obj instanceof double[]) {
        return nullSafeHashCode((double[]) obj);
    }
    if (obj instanceof float[]) {
        return nullSafeHashCode((float[]) obj);
    }
    if (obj instanceof int[]) {
        return nullSafeHashCode((int[]) obj);
    }
    if (obj instanceof long[]) {
        return nullSafeHashCode((long[]) obj);
    }
    if (obj instanceof short[]) {
        return nullSafeHashCode((short[]) obj);
    }
    if (obj instanceof Object[]) {
        return nullSafeHashCode((Object[]) obj);
    }

    return obj.hashCode();
}

From source file:com.qcadoo.model.internal.classconverter.ModelXmlToClassConverterTest.java

@Test
public void shouldHashCodeDoNotHaveCycleWithManyToManyFields() throws Exception {
    // given/*from  w w w .  ja  v a  2 s  . c  om*/
    Object firstEntity = classes.get(ClassNameUtils.getFullyQualifiedClassName("full", "firstEntity"))
            .newInstance();
    Object thirdEntity = classes.get(ClassNameUtils.getFullyQualifiedClassName("full", "thirdEntity"))
            .newInstance();

    BeanUtils.setProperty(firstEntity, "fieldManyToMany", Sets.newHashSet(thirdEntity));
    BeanUtils.setProperty(thirdEntity, "fieldManyToMany", Sets.newHashSet(firstEntity));

    // when & then
    try {
        firstEntity.hashCode();
        thirdEntity.hashCode();
    } catch (StackOverflowError t) {
        Assert.fail();
    }
}

From source file:org.jabsorb.ng.client.Client.java

/**
 * This method is public because of the inheritance from the
 * InvokationHandler -- <b>should never be called directly.</b>
 *///ww  w .j  a  v a  2 s  . c  o  m
@Override
public Object invoke(final Object proxyObj, final Method method, final Object[] args) throws Throwable {

    final String methodName = method.getName();
    if (methodName.equals("hashCode")) {
        return new Integer(System.identityHashCode(proxyObj));
    } else if (methodName.equals("equals")) {
        return (proxyObj == args[0] ? Boolean.TRUE : Boolean.FALSE);
    } else if (methodName.equals("toString")) {
        return proxyObj.getClass().getName() + '@' + Integer.toHexString(proxyObj.hashCode());
    }
    return invoke(pProxyMap.get(proxyObj), method.getName(), args, method.getReturnType());
}

From source file:org.red5.server.so.SharedObject.java

/**
 * Update hashes//from www .  j  a va 2  s . co  m
 */
private void updateHashes() {
    hashes.clear();
    for (String name : data.keySet()) {
        Object value = data.get(name);
        hashes.put(name, value != null ? value.hashCode() : 0);
    }
}

From source file:org.apache.axis.attachments.AttachmentsImpl.java

public Part createAttachmentPart(Object datahandler) throws org.apache.axis.AxisFault {

    // Searching for the same attachements
    Integer key = new Integer(datahandler.hashCode());
    if (stackDataHandler.containsKey(key)) {
        return (Part) stackDataHandler.get(key);
    }//from   w  w w .  j a  va 2 s .  c om

    multipart = null;

    dimemultipart = null;

    mergeinAttachments();

    if (!(datahandler instanceof javax.activation.DataHandler)) {
        throw new org.apache.axis.AxisFault(Messages.getMessage("unsupportedAttach",
                datahandler.getClass().getName(), javax.activation.DataHandler.class.getName()));
    }

    Part ret = new AttachmentPart((javax.activation.DataHandler) datahandler);

    addAttachmentPart(ret);

    // Store the current DataHandler with its key
    stackDataHandler.put(key, ret);

    return ret;
}

From source file:org.apache.struts2.portlet.PortletSessionMap.java

/**
 * @see java.util.Map#entrySet()/*from w ww . j  av  a 2  s  .  c o  m*/
 */
public Set entrySet() {
    synchronized (session) {
        if (entries == null) {
            entries = new HashSet<Object>();

            Enumeration enumeration = session.getAttributeNames();

            while (enumeration.hasMoreElements()) {
                final String key = enumeration.nextElement().toString();
                final Object value = session.getAttribute(key);
                entries.add(new Map.Entry() {
                    public boolean equals(Object obj) {
                        Map.Entry entry = (Map.Entry) obj;

                        return ((key == null) ? (entry.getKey() == null) : key.equals(entry.getKey()))
                                && ((value == null) ? (entry.getValue() == null)
                                        : value.equals(entry.getValue()));
                    }

                    public int hashCode() {
                        return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode());
                    }

                    public Object getKey() {
                        return key;
                    }

                    public Object getValue() {
                        return value;
                    }

                    public Object setValue(Object obj) {
                        session.setAttribute(key, obj);

                        return value;
                    }
                });
            }
        }
    }

    return entries;
}

From source file:com.m4rc310.cb.builders.ComponentBuilder.java

public void addTargets(Object target) {
    if (!targets.contains(target)) {
        LogServer.getInstance().info(target, "Adicionando Target: [{0}] ~> {1}", target.hashCode(), target);
        targets.add(target);//from  w ww. jav a  2  s  .com
    }
}

From source file:com.qcadoo.model.internal.classconverter.ModelXmlToClassConverterTest.java

@Test
public void shouldHaveHashCodeMethod() throws Exception {
    Object entity1 = classes.get(ClassNameUtils.getFullyQualifiedClassName("full", "firstEntity"))
            .newInstance();//ww w. ja va 2s  .com
    Object entity2 = classes.get(ClassNameUtils.getFullyQualifiedClassName("full", "firstEntity"))
            .newInstance();

    BeanUtils.setProperty(entity1, "fieldInteger", 13);
    BeanUtils.setProperty(entity2, "fieldInteger", 13);
    BeanUtils.setProperty(entity1, "fieldString", "Xxx");
    BeanUtils.setProperty(entity2, "fieldString", "Xxx");

    assertTrue(entity1.hashCode() == entity1.hashCode());
    assertTrue(entity1.hashCode() == entity2.hashCode());

    BeanUtils.setProperty(entity2, "fieldString", "Xxz");

    assertFalse(entity1.hashCode() == entity2.hashCode());
}

From source file:org.apache.crunch.Pair.java

private int cmp(Object lhs, Object rhs) {
    if (lhs == rhs) {
        return 0;
    } else if (lhs != null && Comparable.class.isAssignableFrom(lhs.getClass())) {
        return Ordering.natural().nullsLast().compare((Comparable) lhs, (Comparable) rhs);//(Comparable) lhs).compareTo(rhs);
    }/*from  ww  w  .  ja v  a 2 s .  c o m*/
    if (lhs == null) {
        return 1; // nulls last
    }
    if (rhs == null) {
        return -1; // nulls last
    }
    if (lhs.equals(rhs)) {
        return 0;
    }

    // Now we compare based on hash code. We already know that the two sides are not equal, so
    // if the hash codes are equal, we just use arbitrary (but consistent) ordering
    return ComparisonChain.start().compare(lhs.hashCode(), rhs.hashCode())
            .compare(lhs, rhs, Ordering.arbitrary()).result();
}