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.t2framework.commons.util.ArrayMap.java

 private final Entry<K, V> removeMap(Object key) {
   int hashCode = 0;
   int index = 0;
   if (key != null) {
      hashCode = key.hashCode();
      index = (hashCode & 0x7FFFFFFF) % mapTable.length;
      for (Entry<K, V> e = mapTable[index], prev = null; e != null; prev = e, e = e.next_) {
         if ((e.hashCode_ == hashCode) && key.equals(e.key_)) {
            if (prev != null) {
               prev.next_ = e.next_;//ww  w  .j av a 2 s .c o m
            } else {
               mapTable[index] = e.next_;
            }
            return e;
         }
      }
   } else {
      for (Entry<K, V> e = mapTable[index], prev = null; e != null; prev = e, e = e.next_) {
         if ((e.hashCode_ == hashCode) && e.key_ == null) {
            if (prev != null) {
               prev.next_ = e.next_;
            } else {
               mapTable[index] = e.next_;
            }
            return e;
         }
      }
   }
   return null;
}

From source file:org.dcm4che3.conf.core.misc.DeepEquals.java

/**
 * Get a deterministic hashCode (int) value for an Object, regardless of
 * when it was created or where it was loaded into memory.  The problem
 * with java.lang.Object.hashCode() is that it essentially relies on
 * memory location of an object (what identity it was assigned), whereas
 * this method will produce the same hashCode for any object graph, regardless
 * of how many times it is created.<br/><br/>
 *
 * This method will handle cycles correctly (A->B->C->A).  In this case,
 * Starting with object A, B, or C would yield the same hashCode.  If an
 * object encountered (root, suboject, etc.) has a hashCode() method on it
 * (that is not Object.hashCode()), that hashCode() method will be called
 * and it will stop traversal on that branch.
 * @param obj Object who hashCode is desired.
 * @return the 'deep' hashCode value for the passed in object.
 *///from  ww  w . j a  va2  s .  c  om
public static int deepHashCode(Object obj) {
    Set visited = new HashSet();
    LinkedList<Object> stack = new LinkedList<Object>();
    stack.addFirst(obj);
    int hash = 0;

    while (!stack.isEmpty()) {
        obj = stack.removeFirst();
        if (obj == null || visited.contains(obj)) {
            continue;
        }

        visited.add(obj);

        if (obj.getClass().isArray()) {
            int len = Array.getLength(obj);
            for (int i = 0; i < len; i++) {
                stack.addFirst(Array.get(obj, i));
            }
            continue;
        }

        if (obj instanceof Collection) {
            stack.addAll(0, (Collection) obj);
            continue;
        }

        if (obj instanceof Map) {
            stack.addAll(0, ((Map) obj).keySet());
            stack.addAll(0, ((Map) obj).values());
            continue;
        }

        if (hasCustomHashCode(obj.getClass())) { // A real hashCode() method exists, call it.
            hash += obj.hashCode();
            continue;
        }

        Collection<Field> fields = getDeepDeclaredFields(obj.getClass());
        for (Field field : fields) {
            try {
                stack.addFirst(field.get(obj));
            } catch (Exception ignored) {
            }
        }
    }
    return hash;
}

From source file:com.evolveum.midpoint.prism.xnode.PrimitiveXNode.java

/**
 * The basic idea of equals() is://from w w w  .  ja va  2 s  .c  o  m
 *  - if parsed, compare the value;
 *  - if unparsed, compare getStringValue()
 * Therefore the hashcode is generated based on value (if parsed) or getStringValue() (if unparsed).
 */
@Override
public int hashCode() {
    Object objectToHash;
    if (isParsed()) {
        objectToHash = value;
    } else {
        // TODO consider problem with namespaces (if string value is QName/ItemPath its meaning can depend on namespace declarations that are placed outside the element)
        objectToHash = getStringValue();
    }
    return objectToHash != null ? objectToHash.hashCode() : 0;
}

From source file:org.jolokia.converter.json.ObjectToJsonConverter.java

private String checkForLimits(Object pValue, ObjectSerializationContext pStackContext) {
    if (pValue != null) {
        if (pStackContext.maxDepthReached()) {
            // We use its string representation.
            return pValue.toString();
        }/*from   w w w . java 2 s  . c  o  m*/
        if (pStackContext.alreadyVisited(pValue)) {
            return "[Reference " + pValue.getClass().getName() + "@" + Integer.toHexString(pValue.hashCode())
                    + "]";
        }
    }
    if (pStackContext.maxObjectsExceeded()) {
        return "[Object limit exceeded]";
    }
    return null;
}

From source file:org.apache.ojb.broker.Identity.java

/**
 * {@inheritDoc}// ww  w. j  a  va2s. co  m
 */
public int hashCode() {
    /*
    arminw:
    identity is quasi immutable (toplevel class and PK fields
    never change), thus we can note hashCode
    */
    if (m_hashCode == null) {
        int iTotal = isTransient;
        Object obj;
        for (int i = 0; i < m_pkValues.length; i++) {
            obj = m_pkValues[i];
            if (obj instanceof byte[]) {
                iTotal = iTotal * iConstant + ((byte[]) obj).length;
            } else {
                iTotal = iTotal * iConstant + (obj != null ? obj.hashCode() : 0);
            }
        }
        iTotal = iTotal * iConstant + m_objectsTopLevelClass.hashCode();
        m_hashCode = new Integer(iTotal);
    }
    return m_hashCode.intValue();
}

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

/**
 * Returns a Set of attributes from the portlet request.
 *
 * @return a Set of attributes from the portlet request.
 *///from w w w. j  a  va 2s.co  m
public Set entrySet() {
    if (entries == null) {
        entries = new HashSet<Object>();

        Enumeration enumeration = request.getAttributeNames();

        while (enumeration.hasMoreElements()) {
            final String key = enumeration.nextElement().toString();
            final Object value = request.getAttribute(key);
            entries.add(new Entry() {
                public boolean equals(Object obj) {
                    Entry entry = (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) {
                    request.setAttribute(key, obj);

                    return value;
                }
            });
        }
    }

    return entries;
}

From source file:org.marketcetera.util.test.EqualityAssert.java

/**
 * Asserts that the given target object implements equality
 * correctly. If the assertion does not hold, the {@link
 * AssertionError} thrown starts with the given message, which may
 * be null if no such custom message prefix is desired.
 *
 * @param message The message./*from  www  .jav a  2s. c  o  m*/
 * @param o The target object.
 * @param oEqual Another object that is equal to (but not the same
 * as) the target object.
 * @param osUnequal Objects that are all unequal to the target
 * object. It may be null or contain null elements.
 */

public static void assertEquality(String message, Object o, Object oEqual, Object... osUnequal) {
    String content = null;
    if (!o.equals(o)) {
        content = "'" + o + "' unequal to self"; //$NON-NLS-1$ //$NON-NLS-2$
    } else if (!o.equals(oEqual)) {
        content = "'" + o + "' unequal to '" + //$NON-NLS-1$ //$NON-NLS-2$
                oEqual + "'"; //$NON-NLS-1$
    } else if (!oEqual.equals(o)) {
        content = "'" + oEqual + "' unequal to '" + //$NON-NLS-1$ //$NON-NLS-2$
                o + "'"; //$NON-NLS-1$
    } else if (oEqual == o) {
        content = "'" + oEqual + "' same as '" + //$NON-NLS-1$ //$NON-NLS-2$
                o + "'"; //$NON-NLS-1$
    } else if (osUnequal != null) {
        for (Object oUnequal : osUnequal) {
            if (o.equals(oUnequal)) {
                content = "'" + o + "' equal to '" + //$NON-NLS-1$ //$NON-NLS-2$
                        oUnequal + "'"; //$NON-NLS-1$ 
                break;
            }
            if ((oUnequal != null) && (oUnequal.equals(o))) {
                content = "'" + oUnequal + //$NON-NLS-1$ 
                        "' equal to '" + o + "'"; //$NON-NLS-1$ //$NON-NLS-2$
                break;
            }
        }
    }
    if (content == null) {
        if (o.equals(null)) {
            content = "'" + o + "' equal to null"; //$NON-NLS-1$ //$NON-NLS-2$
        } else if (o.equals(NumberUtils.INTEGER_ZERO)) {
            content = "'" + o + "' equal to zero"; //$NON-NLS-1$ //$NON-NLS-2$
        } else if (o.hashCode() != oEqual.hashCode()) {
            content = "'" + o + //$NON-NLS-1$
                    "' hash code unequal to copy's '" + //$NON-NLS-1$
                    oEqual + "'"; //$NON-NLS-1$
        }
    }
    if (content == null) {
        return;
    }
    if (message != null) {
        content = message + " " + content; //$NON-NLS-1$
    }
    fail(content);
}

From source file:cz.lbenda.dataman.db.RowDesc.java

@Override
public int hashCode() {
    if (id != null) {
        return id.hashCode();
    }/*from  w ww.  ja v  a  2 s . c o  m*/
    if (oldValues == null) {
        return 0;
    }
    int result = 1;
    int primeI = 0;
    for (Object v : oldValues) {
        if (primeI >= PRIMES.length) {
            primeI = 0;
        }
        result *= PRIMES[primeI];
        if (v != null) {
            result += v.hashCode();
        }
    }
    return result;
}

From source file:org.structr.core.property.Property.java

@Override
public boolean equals(final Object o) {

    if (o instanceof PropertyKey) {

        return o.hashCode() == hashCode();
    }/*w w  w .j  ava2  s  .  c  om*/

    return false;
}

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

@Test
public void shouldHashCodeDoNotHaveCycleWithBelongsToFields() throws Exception {
    // given// w  w  w  .  ja  va2s .  c  o m
    Object firstEntity = classes.get(ClassNameUtils.getFullyQualifiedClassName("full", "firstEntity"))
            .newInstance();
    Object thirdEntity = classes.get(ClassNameUtils.getFullyQualifiedClassName("full", "thirdEntity"))
            .newInstance();

    BeanUtils.setProperty(firstEntity, "fieldThirdEntity", thirdEntity);
    BeanUtils.setProperty(thirdEntity, "fieldFirstEntity", firstEntity);

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