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.nuclos.common2.LangUtils.java

/**
 * Note that this method doesn't work for arrays currently.
 * @param o may be <code>null</code>
 * @return hash code for o, as in <code>Object.hashCode()</code>
 *///w ww .  ja v a2s . c o m
public static int hashCode(Object o) {
    return (o == null) ? 0 : o.hashCode();
}

From source file:com.evolveum.midpoint.util.MiscUtil.java

public static int unorderedCollectionHashcode(Collection collection) {
    // Stupid implmentation, just add all the hashcodes
    int hashcode = 0;
    for (Object item : collection) {
        hashcode += item.hashCode();
    }//from  w  ww. ja  va 2  s.  c  o  m
    return hashcode;
}

From source file:com.redhat.rhn.frontend.taglibs.list.ListTagHelper.java

/**
 * Returns the object id given an object
 * deals with selectable/identifiable objects
 * or uses hashcode for general/*from   w  w  w .j a v a 2s. com*/
 * @param current the current object
 * @return the id representing the object
 */
public static String getObjectId(Object current) {
    String id = null;
    if (current instanceof Selectable) {
        id = ((Selectable) current).getSelectionKey();
    } else if (current instanceof Identifiable) {
        id = String.valueOf(((Identifiable) current).getId());
    }
    if (id == null) {
        id = String.valueOf(current.hashCode());
    }
    return id;
}

From source file:jp.co.acroquest.endosnipe.common.logger.ENdoSnipeLogger.java

/**
 * ????<br />/*from w  w  w.  j  ava  2 s .  co  m*/
 * 
 * @param obj 
 * @return 
 */
public static String getObjectDescription(final Object obj) {
    if (obj != null) {
        return obj.getClass().getName() + "@" + Integer.toHexString(obj.hashCode());
    } else {
        return "NULL";
    }
}

From source file:och.util.Util.java

public static String toObjectString(Object ob) {
    return ob.getClass().getName() + "@" + Integer.toHexString(ob.hashCode());
}

From source file:org.decojer.cavaj.utils.SwitchTypes.java

/**
 * Is used for string-switches. Execute switch case BB to create the case value map: string to
 * BB./*from  w w w.  j  ava 2  s  . c o  m*/
 *
 * @param caseBb
 *            case BB
 * @param stringReg
 *            string register
 * @param hash
 *            hash for string
 * @param defaultCase
 *            default case
 * @param string2bb
 *            case value map: string to BB
 * @return {@code true} - success
 */
private static boolean executeBbStringHashCond(final BB caseBb, final int stringReg, final int hash,
        final BB defaultCase, final Map<String, BB> string2bb) {
    final Deque<Object> stack = Queues.newArrayDeque();
    String str = null;
    for (int i = 0; i < caseBb.getOps(); ++i) {
        final Op op = caseBb.getOp(i);
        switch (op.getOptype()) {
        case LOAD:
            stack.push(((LOAD) op).getReg());
            break;
        case PUSH:
            stack.push(((PUSH) op).getValue());
            break;
        case INVOKE:
            final M m = ((INVOKE) op).getM();
            if (!"equals".equals(m.getName()) || !"(Ljava/lang/Object;)Z".equals(m.getDescriptor())) {
                return false;
            }
            final Object value = stack.pop();
            if (!(value instanceof String)) {
                return false;
            }
            if (value.hashCode() != hash) {
                return false;
            }
            final Object reg = stack.pop();
            if ((Integer) reg != stringReg) {
                return false;
            }
            stack.push(true);
            str = (String) value;
            break;
        case JCND:
            final Object equalsResult = stack.pop();
            if (!(equalsResult instanceof Boolean)) {
                return false;
            }
            boolean dir = ((Boolean) equalsResult).booleanValue();
            if (((JCND) op).getCmpType() == CmpType.T_EQ) {
                dir = !dir;
            }
            string2bb.put(str, dir ? caseBb.getTrueSucc() : caseBb.getFalseSucc());
            final E out = dir ? caseBb.getFalseOut() : caseBb.getTrueOut();
            if (out == null) {
                assert false;
                return false;
            }
            if (out.getRelevantEnd() == defaultCase) {
                return true;
            }
            return executeBbStringHashCond(out.getEnd(), stringReg, hash, defaultCase, string2bb);
        default:
            return false;
        }
    }
    return false;
}

From source file:lirmm.inria.fr.math.TestUtils.java

/**
 * Verifies that serialization preserves equals and hashCode.
 * Serializes the object, then recovers it and checks equals and hash code.
 *
 * @param object  the object to serialize and recover
 *//* w ww .jav  a2s.  com*/
public static void checkSerializedEquality(Object object) {
    Object object2 = serializeAndRecover(object);
    Assert.assertEquals("Equals check", object, object2);
    Assert.assertEquals("HashCode check", object.hashCode(), object2.hashCode());
}

From source file:com.turbospaces.core.SpaceUtility.java

/**
 * wrap original key locker by applying load distribution hash function and make it more concurrent(parallel)
 * /*w ww  .j a v  a2s.c o  m*/
 * @return more concurrent parallel key locker
 */
public static KeyLocker parallelizedKeyLocker() {
    KeyLocker locker = new KeyLocker() {
        private final KeyLocker[] segments = new KeyLocker[1 << 8];
        private int mask;

        {
            for (int i = 0; i < segments.length; i++)
                segments[i] = new TransactionScopeKeyLocker();
            mask = segments.length - 1;
        }

        @Override
        public void writeUnlock(final EntryKeyLockQuard guard, final long transactionID) {
            segmentFor(guard.getKey()).writeUnlock(guard, transactionID);
        }

        @Override
        public EntryKeyLockQuard writeLock(final Object key, final long transactionID, final long timeout,
                final boolean strict) {
            return segmentFor(key).writeLock(key, transactionID, timeout, strict);
        }

        private KeyLocker segmentFor(final Object key) {
            return segments[(JVMUtil.jdkRehash(key.hashCode()) & Integer.MAX_VALUE) & mask];
        }
    };
    return locker;
}

From source file:Main.java

/**
 * Converts a given object to a form encoded map
 * @param objName Name of the object/* w w w. j a  va2s.  co m*/
 * @param obj The object to convert into a map
 * @param objectMap The object map to populate
 * @param processed List of objects hashCodes that are already parsed
 * @throws InvalidObjectException
 */
private static void objectToMap(String objName, Object obj, Map<String, Object> objectMap,
        HashSet<Integer> processed) throws InvalidObjectException {
    //null values need not to be processed
    if (obj == null)
        return;

    //wrapper types are autoboxed, so reference checking is not needed
    if (!isWrapperType(obj.getClass())) {
        //avoid infinite recursion
        if (processed.contains(obj.hashCode()))
            return;
        processed.add(obj.hashCode());
    }

    //process arrays
    if (obj instanceof Collection<?>) {
        //process array
        if ((objName == null) || (objName.isEmpty()))
            throw new InvalidObjectException("Object name cannot be empty");

        Collection<?> array = (Collection<?>) obj;
        //append all elements in the array into a string
        int index = 0;
        for (Object element : array) {
            //load key value pair
            String key = String.format("%s[%d]", objName, index++);
            loadKeyValuePairForEncoding(key, element, objectMap, processed);
        }
    } else if (obj instanceof Map) {
        //process map
        Map<?, ?> map = (Map<?, ?>) obj;
        //append all elements in the array into a string            
        for (Map.Entry<?, ?> pair : map.entrySet()) {
            String attribName = pair.getKey().toString();
            String key = attribName;
            if ((objName != null) && (!objName.isEmpty())) {
                key = String.format("%s[%s]", objName, attribName);
            }
            loadKeyValuePairForEncoding(key, pair.getValue(), objectMap, processed);
        }
    } else {
        //process objects
        // invoke getter methods
        Method[] methods = obj.getClass().getMethods();
        for (Method method : methods) {
            //is a getter?
            if ((method.getParameterTypes().length != 0) || (!method.getName().startsWith("get")))
                continue;

            //get json attribute name
            Annotation getterAnnotation = method.getAnnotation(JsonGetter.class);
            if (getterAnnotation == null)
                continue;

            //load key name
            String attribName = ((JsonGetter) getterAnnotation).value();
            String key = attribName;
            if ((objName != null) && (!objName.isEmpty())) {
                key = String.format("%s[%s]", objName, attribName);
            }

            try {
                //load key value pair
                Object value = method.invoke(obj);
                loadKeyValuePairForEncoding(key, value, objectMap, processed);
            } catch (Exception ex) {
            }
        }
        // load fields
        Field[] fields = obj.getClass().getFields();
        for (Field field : fields) {
            //load key name
            String attribName = field.getName();
            String key = attribName;
            if ((objName != null) && (!objName.isEmpty())) {
                key = String.format("%s[%s]", objName, attribName);
            }

            try {
                //load key value pair
                Object value = field.get(obj);
                loadKeyValuePairForEncoding(key, value, objectMap, processed);
            } catch (Exception ex) {
            }
        }
    }
}

From source file:com.mawujun.util.AnnotationUtils.java

/**
 * Helper method for generating a hash code for a member of an annotation.
 *
 * @param name the name of the member//w w w . ja v a  2s  .c o  m
 * @param value the value of the member
 * @return a hash code for this member
 */
private static int hashMember(String name, Object value) {
    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();
}