Example usage for java.lang System identityHashCode

List of usage examples for java.lang System identityHashCode

Introduction

In this page you can find the example usage for java.lang System identityHashCode.

Prototype

@HotSpotIntrinsicCandidate
public static native int identityHashCode(Object x);

Source Link

Document

Returns the same hash code for the given object as would be returned by the default method hashCode(), whether or not the given object's class overrides hashCode().

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    File file1 = new File("a");
    File file2 = new File("a");
    File file3 = new File("b");

    int ihc1 = System.identityHashCode(file1);
    System.out.println(ihc1);//from w  ww. ja  v a  2 s .  c o m
    int ihc2 = System.identityHashCode(file2);
    System.out.println(ihc2);
    int ihc3 = System.identityHashCode(file3);
    System.out.println(ihc3);

}

From source file:Main.java

public static void main(String[] args) throws Exception {

    String s1 = "java2s";
    String s2 = "java2s.com";
    String s3 = "Java2s.com";

    // returns the HashCode
    int ret1 = System.identityHashCode(s1);
    System.out.println(ret1);/* w w w . ja  va2  s.co m*/

    // returns different HashCode for same filename
    int ret2 = System.identityHashCode(s2);
    System.out.println(ret2);

    // returns the HashCode    
    int ret3 = System.identityHashCode(s3);
    System.out.println(ret3);
}

From source file:Main.java

private static int listHashCode(Iterable<?> l) {
    int ret = 1;// www .  j  a v a  2s.  c o  m
    for (Object e : l) {
        // don't overflow stack if list contains itself -- substitute default hashcode
        int elementHash = e == l ? System.identityHashCode(l) : (e == null ? 0 : e.hashCode());
        ret = 31 * ret + elementHash;
    }
    return ret;
}

From source file:Main.java

public static String getHashCode(Object obj) {
    return "0x" + Integer.toHexString(System.identityHashCode(obj));
}

From source file:Main.java

public static void buildShortClassTag(Object cls, StringBuilder out) {
    if (cls == null) {
        out.append("null");
        return;/*w  ww. ja  va  2 s.  co  m*/
    }
    String simpleName = cls.getClass().getSimpleName();
    if (simpleName == null || simpleName.length() <= 0) {
        simpleName = cls.getClass().getName();
        int end = simpleName.lastIndexOf(46);
        if (end > 0) {
            simpleName = simpleName.substring(end + 1);
        }
    }
    out.append(simpleName);
    out.append('{');
    out.append(Integer.toHexString(System.identityHashCode(cls)));
}

From source file:Main.java

public static <T> Comparator<T> identityComparator() {
    return new Comparator<T>() {

        @Override/*from www .j  a v a  2s. c  o  m*/
        public int compare(T o1, T o2) {
            return System.identityHashCode(o1) - System.identityHashCode(o2);
        }
    };
}

From source file:Main.java

static Object[] createThreadInfo(Thread thread, Object lock, Thread lockOwner) {

    String lockName = null;//  w  ww. j  ava  2 s .com
    String lockOwnerName = null;

    if (lock != null) {
        lockName = lock.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(lock));
        if (lockOwner != null)
            lockOwnerName = lockOwner.getName();
    }

    return new Object[] { thread.getName(), thread.getState(), lockName, lockOwnerName };
}

From source file:Main.java

/**
 * Computes the hash code for a set per the contract defined by {@link Set#hashCode()}.
 * //w  ww  .  ja  v a2 s . c  o  m
 * @param set the set
 * @return the hash code for {@code list}
 */
public static int hashCode(Set<?> set) {
    int hashCode = 0;
    for (Object item : set) {
        if (item != null) {
            // don't overflow stack if set contains itself -- substitute default hashcode
            hashCode += item == set ? System.identityHashCode(set) : item.hashCode();
        }
    }
    return hashCode;
}

From source file:Main.java

/**
 * Returns an empty array of the specified type.  The intent is that
 * it will return the same empty array every time to avoid reallocation,
 * although this is not guaranteed.//from  w w w.j  a va  2s .  c o m
 */
public static <T> T[] emptyArray(Class kind) {
    if (kind == Object.class) {
        return (T[]) EMPTY;
    }

    int bucket = ((System.identityHashCode(kind) / 8) & 0x7FFFFFFF) % CACHE_SIZE;
    Object cache = sCache[bucket];

    if (cache == null || cache.getClass().getComponentType() != kind) {
        cache = Array.newInstance(kind, 0);
        sCache[bucket] = cache;

        // Log.e("cache", "new empty " + kind.getName() + " at " + bucket);
    }

    return (T[]) cache;
}

From source file:Main.java

/**
 * Returns an empty array of the specified type.  The intent is that
 * it will return the same empty array every time to avoid reallocation,
 * although this is not guaranteed.//from  w ww  . j  ava  2s  . c o m
 */
public static <T> T[] emptyArray(Class<T> kind) {
    if (kind == Object.class) {
        return (T[]) EMPTY;
    }

    int bucket = ((System.identityHashCode(kind) / 8) & 0x7FFFFFFF) % CACHE_SIZE;
    Object cache = sCache[bucket];

    if (cache == null || cache.getClass().getComponentType() != kind) {
        cache = Array.newInstance(kind, 0);
        sCache[bucket] = cache;

        // Log.e("cache", "new empty " + kind.getName() + " at " + bucket);
    }

    return (T[]) cache;
}