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.rapidcontext.app.ApplicationContext.java

/**
 * Finds the currently active call context for a thread id. The
 * thread id is identical to the hash code for the thread.
 *
 * @param threadId       the thread id to search for
 *
 * @return the call context found, or//from  w w  w .j  av a2  s. c  o m
 *         null if no context was active
 */
public CallContext findContext(int threadId) {
    Iterator iter;
    Object obj;

    synchronized (threadContext) {
        iter = threadContext.keySet().iterator();
        while (iter.hasNext()) {
            obj = iter.next();
            if (obj.hashCode() == threadId) {
                return (CallContext) threadContext.get(obj);
            }
        }
    }
    return null;
}

From source file:ed.net.httpclient.HttpConnection.java

public boolean equals(Object o) {
    return _id == o.hashCode();
}

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

@Test
public void shouldHashCodeDoNotHaveCycleWithHasManyFields() throws Exception {
    // given//  w w w . j  a va  2  s  . com
    Object firstEntity = classes.get(ClassNameUtils.getFullyQualifiedClassName("full", "firstEntity"))
            .newInstance();
    Object thirdEntity = classes.get(ClassNameUtils.getFullyQualifiedClassName("full", "thirdEntity"))
            .newInstance();

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

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

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

@Test
public void shouldHashCodeDoNotHaveCycleWithBelongsToAndManyToManyFields() throws Exception {
    // given//from w ww. j a v  a  2  s.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, "fieldManyToMany", Sets.newHashSet(firstEntity));

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

From source file:io.sweers.arraysetbackport.ArraySet.java

/**
 * Returns the index of a value in the set.
 *
 * @param key The value to search for./*from   w w  w .  ja v a2 s  .  c o  m*/
 * @return Returns the index of the value if it exists, else a negative integer.
 */
public int indexOf(Object key) {
    return key == null ? indexOfNull() : indexOf(key, key.hashCode());
}

From source file:HashCodeBuilder.java

/**
 * Append a hashCode for an Object./*from   w w w  .  ja v a 2s  .  c  o  m*/
 *
 * @param object  the object to add to the hashCode
 * @return this
 */
public HashCodeBuilder append(Object object) {
    if (object == null) {
        iTotal = iTotal * iConstant;

    } else {
        if (object.getClass().isArray() == false) {
            //the simple case, not an array, just the element 
            iTotal = iTotal * iConstant + object.hashCode();

        } else {
            //'Switch' on type of array, to dispatch to the correct handler
            // This handles multi dimensional arrays
            if (object instanceof long[]) {
                append((long[]) object);
            } else if (object instanceof int[]) {
                append((int[]) object);
            } else if (object instanceof short[]) {
                append((short[]) object);
            } else if (object instanceof char[]) {
                append((char[]) object);
            } else if (object instanceof byte[]) {
                append((byte[]) object);
            } else if (object instanceof double[]) {
                append((double[]) object);
            } else if (object instanceof float[]) {
                append((float[]) object);
            } else if (object instanceof boolean[]) {
                append((boolean[]) object);
            } else {
                // Not an array of primitives
                append((Object[]) object);
            }
        }
    }
    return this;
}

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

private void printDialog() {

    for (Field field : fields) {
        //        fields.stream().forEach((field) -> {
        Acomponent ac = field.getDeclaredAnnotation(Acomponent.class);
        AbstractComponetAdapter adapter = adapters.get(field);
        Object target = getTargetForField(field);
        Object containner = containers.get(target.hashCode());

        if (!ac.tabFor().isEmpty()) {
            Object jtp = getJTabbedPane(ac.tabFor());
            String text = getString(ac.text());
            MethodUtils.method(jtp, "addTab", String.class, Component.class).invoke(text,
                    adapter.getComponent());
            continue;
        }//from  ww w .j a  v  a  2s.c  om

        if (!ac.label().isEmpty()) {
            JLabel jLabel = gui.getJLabel(ac.label(), adapter.getComponent());
            MethodUtils.method(containner, "add", Component.class, Object.class).invoke(jLabel,
                    ac.layoutLabel());
            mapLabels.put(ac.ref(), jLabel);
        }

        MethodUtils.method(containner, "add", Component.class, Object.class).invoke(adapter.getComponent(),
                ac.layout());
    }
}

From source file:org.nanocontainer.avalon.AvalonUtilTestCase.java

public void testGetActivityLifecycleProxyHandlesEqualsAndHashCodeProperly() throws Exception {
    final Object obj = new Serializable() {
        public String toString() {
            return "blaat";
        }/*from   w ww. j ava  2 s  .  c om*/
    };
    final Object proxy = AvalonUtil.getActivityLifecycleProxy(obj);

    assertFalse(proxy.equals(obj));
    assertTrue(proxy.equals(proxy));

    assertTrue(proxy.hashCode() == proxy.hashCode());
    assertFalse(proxy.hashCode() == obj.hashCode());

    assertFalse("blaat".equals(proxy.toString()));
}

From source file:org.jgentleframework.core.interceptor.AnnotationAroundAdviceMethodInterceptor.java

/**
 * Hash code.//from   www  .  ja  va 2  s.  c o m
 * 
 * @param proxy
 *            the proxy
 * @return the int
 * @throws IllegalArgumentException
 *             the illegal argument exception
 * @throws IllegalAccessException
 *             the illegal access exception
 * @throws InvocationTargetException
 *             the invocation target exception
 */
protected int hashCode(Object proxy)
        throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {

    Method[] methods = targetClass.getDeclaredMethods();
    int hashCode = 0;
    for (Method method : methods) {
        Class<?> type = method.getReturnType();
        Object value = (Object) this.attributesMapping.get(method);
        if (value == null)
            value = method.getDefaultValue();
        // if type is primitive type
        if (type.isPrimitive()) {
            // //////////////////////////////////
            // //////////////////////////////////
            hashCode += 127 * method.getName().hashCode() ^ value.hashCode();
        } else if (type.isArray()) {
            Object array = method.invoke(proxy);
            Class<?> comtype = type.getComponentType();
            if (comtype == byte.class || comtype == Byte.class) {
                byte[] valueArr = new byte[Array.getLength(array)];
                for (int i = 0; i < valueArr.length; i++) {
                    valueArr[i] = Array.getByte(array, i);
                }
                hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr);
            } else if (comtype == char.class || comtype == Character.class) {
                char[] valueArr = new char[Array.getLength(array)];
                for (int i = 0; i < valueArr.length; i++) {
                    valueArr[i] = Array.getChar(array, i);
                }
                hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr);
            } else if (comtype == double.class || comtype == Double.class) {
                double[] valueArr = new double[Array.getLength(array)];
                for (int i = 0; i < valueArr.length; i++) {
                    valueArr[i] = Array.getDouble(array, i);
                }
                hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr);
            } else if (comtype == float.class || comtype == Float.class) {
                float[] valueArr = new float[Array.getLength(array)];
                for (int i = 0; i < valueArr.length; i++) {
                    valueArr[i] = Array.getFloat(array, i);
                }
                hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr);
            } else if (comtype == int.class || comtype == Integer.class) {
                int[] valueArr = new int[Array.getLength(array)];
                for (int i = 0; i < valueArr.length; i++) {
                    valueArr[i] = Array.getInt(array, i);
                }
                hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr);
            } else if (comtype == long.class || comtype == Long.class) {
                long[] valueArr = new long[Array.getLength(array)];
                for (int i = 0; i < valueArr.length; i++) {
                    valueArr[i] = Array.getLong(array, i);
                }
                hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr);
            } else if (comtype == short.class || comtype == Short.class) {
                short[] valueArr = new short[Array.getLength(array)];
                for (int i = 0; i < valueArr.length; i++) {
                    valueArr[i] = Array.getShort(array, i);
                }
                hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr);
            } else if (comtype == boolean.class || comtype == Boolean.class) {
                boolean[] valueArr = new boolean[Array.getLength(array)];
                for (int i = 0; i < valueArr.length; i++) {
                    valueArr[i] = Array.getBoolean(array, i);
                }
                hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr);
            } else {
                Object[] valueArr = new Object[Array.getLength(array)];
                for (int i = 0; i < valueArr.length; i++) {
                    valueArr[i] = Array.get(array, i);
                }
                hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr);
            }
        } else {
            // Object value = method.invoke(proxy);
            hashCode += 127 * method.getName().hashCode() ^ value.hashCode();
        }
    }
    return hashCode;
}

From source file:HashMap.java

/**
 * Returns the hashCode for a key.// w w w.j av  a  2  s.c  o  m
 */
protected int keyHashCode(Object k) {
    return (k == null) ? 0 : k.hashCode();
}