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

/**
 * Returns the hash code value for this List.
 * <p>/*w  w  w .jav a 2  s.c  o  m*/
 * This implementation uses exactly the code that is used to define the List
 * hash function in the documentation for List.hashCode.
 */
public int hashCode() {
    int hashCode = 1;
    Iterator i = iterator();
    while (i.hasNext()) {
        Object obj = i.next();
        hashCode = 31 * hashCode + (obj == null ? 0 : obj.hashCode());
    }
    return hashCode;
}

From source file:CopyOnWriteArrayList.java

/**
 * Returns the hash code value for this list.
 *
 * <p>This implementation uses the definition in {@link List#hashCode}.
 *
 * @return the hash code value for this list
 *//*from w  w w  .java 2s .  co  m*/
public int hashCode() {
    int hashCode = 1;
    Object[] elements = getArray();
    int len = elements.length;
    for (int i = 0; i < len; ++i) {
        Object obj = elements[i];
        hashCode = 31 * hashCode + (obj == null ? 0 : obj.hashCode());
    }
    return hashCode;
}

From source file:org.dishevelled.multimap.impl.AbstractHashedMap.java

/**
 * Gets the hash code for the key specified.
 * This implementation uses the additional hashing routine from JDK1.4.
 * Subclasses can override this to return alternate hash codes.
 *
 * @param key the key to get a hash code for
 * @return the hash code/*www  .ja  va 2  s .c  o  m*/
 */
protected int hash(Object key) {
    // same as JDK 1.4
    int h = key.hashCode();
    h += ~(h << 9);
    h ^= (h >>> 14);
    h += (h << 4);
    h ^= (h >>> 10);
    return h;
}

From source file:FastArrayList.java

/**
 * Return the hash code value for this list.  This implementation uses
 * exactly the code that is used to define the list hash function in the
 * documentation for the <code>List.hashCode</code> method.
 *//*from   w ww  .j  av a2s  . c  o  m*/
public int hashCode() {

    if (fast) {
        int hashCode = 1;
        java.util.Iterator i = list.iterator();
        while (i.hasNext()) {
            Object o = i.next();
            hashCode = 31 * hashCode + (o == null ? 0 : o.hashCode());
        }
        return (hashCode);
    } else {
        synchronized (list) {
            int hashCode = 1;
            java.util.Iterator i = list.iterator();
            while (i.hasNext()) {
                Object o = i.next();
                hashCode = 31 * hashCode + (o == null ? 0 : o.hashCode());
            }
            return (hashCode);
        }
    }

}

From source file:com.metaparadigm.jsonrpc.JSONRPCBridge.java

/**
 * Registers an object to export all instance methods and static methods.
 * /*from  w  ww . jav a2s  .  c  o m*/
 * The JSONBridge will export all instance methods and static methods of the
 * particular object under the name passed in as a key.
 * <p />
 * This will make available all methods of the object as
 * <code>&lt;key&gt;.&lt;methodnames&gt;</code> to JSON-RPC clients.
 * 
 * @param key
 *            The named prefix to export the object as
 * @param o
 *            The object instance to be called upon
 */
public void registerObject(Object key, Object o) {
    Class clazz = o.getClass();
    ObjectInstance oi = new ObjectInstance(o);
    synchronized (objectMap) {
        objectMap.put(key, oi);
    }
    if (debug)
        log.info("registered object " + o.hashCode() + " of class " + clazz.getName() + " as " + key);
}

From source file:com.metaparadigm.jsonrpc.JSONRPCBridge.java

/**
 * Registers an object to export all instance methods defined by
 * interfaceClass.//ww  w .  j  ava2 s.co m
 * 
 * The JSONBridge will export all instance methods defined by interfaceClass
 * of the particular object under the name passed in as a key.
 * 
 * This will make available these methods of the object as
 * <code>&lt;key&gt;.&lt;methodnames&gt;</code> to JSON-RPC clients.
 * 
 * @param key
 *            The named prefix to export the object as
 * @param o
 *            The object instance to be called upon
 * @param interfaceClass
 *            The type that this object should be registered as.
 * 
 * This can be used to restrict the exported methods to the methods defined
 * in a specific superclass or interface.
 */
public void registerObject(Object key, Object o, Class interfaceClass) {
    ObjectInstance inst = new ObjectInstance(o, interfaceClass);
    synchronized (objectMap) {
        objectMap.put(key, inst);
    }
    if (debug) {
        log.info("registered object " + o.hashCode() + " of class " + interfaceClass.getName() + " as " + key);
    }
}

From source file:org.openmrs.module.reportingcompatibility.reporting.export.DataExportFunctions.java

/**
 * @return results of o.hashCode()/*w ww.ja v a 2 s .c o m*/
 */
public Integer hashCode(Object o) {
    if (o == null)
        return null;
    return o.hashCode();
}

From source file:eu.crisis_economics.abm.model.ModelUtils.java

/**
  * Get a list of subconfiguration objects for a class instance (<code>X</code>). This 
  * method will do the following://from  w  ww .j  a  v a 2 s  . c o m
  * 
  * <ul>
  *   <li> Identify any fields in the input <code>X</code> which carry the
  *        <code>@Parameter</code> and <code>@Submodel</code> annotations.
  *   <li> If <code>X</code> is an instance of {@link ComponentConfiguration}, and
  *        the ID of the accompanying <code>@Parameter</code> annotation is nonempty,
  *        set the scope string of the field to the parameter ID.
  *   <li> Add a reference to the field to a list <code>L</code>.
  *   <li> Repeat the above steps depth-first recursively for each field in <code>X</code>.
  *   <li> Return <code>L</code>
  * </ul>
  * 
  * @param on <br>
  *        The class instance <code>X</code> to search.
  */
public static List<ComponentConfiguration> getSubconfigurators(final Object on) {
    final Class<?> objClass = Preconditions.checkNotNull(on).getClass();
    final List<ComponentConfiguration> result = new ArrayList<ComponentConfiguration>();
    for (Class<?> typeToSearch = objClass; typeToSearch != null; typeToSearch = typeToSearch.getSuperclass()) {
        for (final Field field : typeToSearch.getDeclaredFields()) {
            if (!ComponentConfiguration.class.isAssignableFrom(field.getType()))
                continue;
            field.setAccessible(true);
            try {
                Annotation drilldownAnnotation = null, modelParameterAnnotation = null;
                for (final Annotation element : field.getAnnotations()) {
                    if (element.annotationType().getName() == Submodel.class.getName()) { // Proxies
                        drilldownAnnotation = element;
                        continue;
                    } else if (element.annotationType().getName() == Parameter.class.getName()) { // Proxies
                        modelParameterAnnotation = element;
                        continue;
                    } else
                        continue;
                }
                if (modelParameterAnnotation != null && drilldownAnnotation != null) {
                    final Object value = field.get(on);
                    if (value == null)
                        throw new IllegalStateException(on.getClass().getSimpleName()
                                + ": the value of a configurator member field" + " in "
                                + (on.getClass().getSimpleName() + on.hashCode()) + " is null.");
                    result.add((ComponentConfiguration) value);
                    final boolean isScoped = (Boolean) modelParameterAnnotation.annotationType()
                            .getMethod("Scoped").invoke(modelParameterAnnotation);
                    if (!isScoped)
                        continue;
                    final String id = (String) modelParameterAnnotation.annotationType().getMethod("ID")
                            .invoke(modelParameterAnnotation);
                    if (id.isEmpty())
                        continue;
                    ((ComponentConfiguration) value).setScope(id);
                }
            } catch (final SecurityException e) {
                throw new IllegalStateException(on.getClass().getSimpleName()
                        + ": a security exception was raised when testing a field with name " + field.getName()
                        + " for model parameter annotations. Details follow: " + e.getMessage() + ".");
            } catch (final IllegalArgumentException e) {
                throw new IllegalStateException(on.getClass().getSimpleName()
                        + "search: an illegal argument exception was raised when testing a field with name "
                        + field.getName() + " for model parameter annotations. Details follow: "
                        + e.getMessage() + ".");
            } catch (final IllegalAccessException e) {
                throw new IllegalStateException(on.getClass().getSimpleName()
                        + "search: a security exception was raised when testing a field with name "
                        + field.getName() + " for model parameter annotations. Details follow: "
                        + e.getMessage() + ".");
            } catch (final InvocationTargetException e) {
                throw new IllegalStateException(on.getClass().getSimpleName()
                        + "search: an invokation target exception was raised when testing a field with"
                        + " name " + field.getName() + " for model parameter annotations. Details follow: "
                        + e.getMessage() + ".");
            } catch (final NoSuchMethodException e) {
                throw new IllegalStateException(on.getClass().getSimpleName()
                        + "search: a missing-method exception was raised when testing a field with name "
                        + field.getName() + " for model parameter annotations. Details follow: "
                        + e.getMessage() + ".");
            }
        }
    }
    return result;
}

From source file:org.openfaces.component.table.impl.TableDataModel.java

private static boolean checkSerializableEqualsAndHashcode(Object rowKey) {
    if (!(rowKey instanceof Serializable))
        return false;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Object deserializedRowKey;
    try {/*from   w  w  w.  j  a  v  a2s  .  c o  m*/
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(rowKey);
        oos.close();
        byte[] serializedObject = baos.toByteArray();
        ByteArrayInputStream bais = new ByteArrayInputStream(serializedObject);
        ObjectInputStream ois = new ObjectInputStream(bais);
        deserializedRowKey = ois.readObject();
        bais.close();
    } catch (IOException e) {
        throw new RuntimeException(
                "The rowData or rowKey object is marked as Serializable, but can't be serialized: "
                        + rowKey.getClass().getName()
                        + " ; check that all object's fields are also Serializable",
                e);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
    boolean equalsValid = deserializedRowKey.equals(rowKey);
    boolean hashCodeValid = deserializedRowKey.hashCode() == rowKey.hashCode();
    boolean result = equalsValid && hashCodeValid;
    return result;
}

From source file:org.apache.geode.pdx.internal.PdxInstanceImpl.java

@Override
public int hashCode() {
    if (this.cachedHashCode != UNUSED_HASH_CODE) {
        // Already computed.
        return this.cachedHashCode;
    }/* w w w  .ja v a2 s.c o m*/
    PdxReaderImpl ur = getUnmodifiableReader();

    // Compute hash code.
    Collection<PdxField> fields = ur.getPdxType().getSortedIdentityFields();
    int hashCode = 1;
    for (PdxField ft : fields) {
        switch (ft.getFieldType()) {
        case CHAR:
        case BOOLEAN:
        case BYTE:
        case SHORT:
        case INT:
        case LONG:
        case DATE:
        case FLOAT:
        case DOUBLE:
        case STRING:
        case BOOLEAN_ARRAY:
        case CHAR_ARRAY:
        case BYTE_ARRAY:
        case SHORT_ARRAY:
        case INT_ARRAY:
        case LONG_ARRAY:
        case FLOAT_ARRAY:
        case DOUBLE_ARRAY:
        case STRING_ARRAY:
        case ARRAY_OF_BYTE_ARRAYS: {
            ByteSource buffer = ur.getRaw(ft);
            if (!buffer.equals(ByteSourceFactory.create(ft.getFieldType().getDefaultBytes()))) {
                hashCode = hashCode * 31 + buffer.hashCode();
            }
            break;
        }
        case OBJECT_ARRAY: {
            Object[] oArray = ur.readObjectArray(ft);
            if (oArray != null) {
                // default value of null does not modify hashCode.
                hashCode = hashCode * 31 + Arrays.deepHashCode(oArray);
            }
            break;
        }
        case OBJECT: {
            Object objectValue = ur.readObject(ft);
            if (objectValue == null) {
                // default value of null does not modify hashCode.
            } else if (objectValue.getClass().isArray()) {
                Class<?> myComponentType = objectValue.getClass().getComponentType();
                if (myComponentType.isPrimitive()) {
                    ByteSource buffer = getRaw(ft);
                    hashCode = hashCode * 31 + buffer.hashCode();
                } else {
                    hashCode = hashCode * 31 + Arrays.deepHashCode((Object[]) objectValue);
                }
            } else {
                hashCode = hashCode * 31 + objectValue.hashCode();
            }
            break;
        }
        default:
            throw new InternalGemFireException("Unhandled field type " + ft.getFieldType());
        }
    }
    int result = (hashCode == UNUSED_HASH_CODE) ? (hashCode + 1) : hashCode;
    this.cachedHashCode = result;
    return result;
}