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

public V remove(Object key) {
    Entry<K, V>[] tab = this.table;

    if (key != null) {
        int hash = key.hashCode();
        int index = (hash & 0x7fffffff) % tab.length;

        for (Entry<K, V> e = tab[index], prev = null; e != null; e = e.next) {
            V entryValue = e.get();//from   w  ww  .  j  a  v  a 2s. c o  m

            if (entryValue == null) {
                // Clean up after a cleared Reference.
                this.modCount++;
                if (prev != null) {
                    prev.next = e.next;
                } else {
                    tab[index] = e.next;
                }
                this.count--;
            } else if (e.hash == hash && key.equals(e.key)) {
                this.modCount++;
                if (prev != null) {
                    prev.next = e.next;
                } else {
                    tab[index] = e.next;
                }
                this.count--;

                e.setValue(null);
                return (entryValue == KeyFactory.NULL) ? null : entryValue;
            } else {
                prev = e;
            }
        }
    } else {
        for (Entry<K, V> e = tab[0], prev = null; e != null; e = e.next) {
            V entryValue = e.get();

            if (entryValue == null) {
                // Clean up after a cleared Reference.
                this.modCount++;
                if (prev != null) {
                    prev.next = e.next;
                } else {
                    tab[0] = e.next;
                }
                this.count--;
            } else if (e.key == null) {
                this.modCount++;
                if (prev != null) {
                    prev.next = e.next;
                } else {
                    tab[0] = e.next;
                }
                this.count--;

                e.setValue(null);
                return (entryValue == KeyFactory.NULL) ? null : entryValue;
            } else {
                prev = e;
            }
        }
    }

    return null;
}

From source file:org.openspaces.admin.internal.admin.DefaultAdmin.java

@Override
public void pushEvent(Object listener, Runnable notifier) {
    assertStateChangesPermitted();//from  ww w.j  a  va2s  .co m
    synchronized (DefaultAdmin.this) {
        eventsQueue[Math.abs(listener.hashCode() % eventsExecutorServices.length)]
                .add(toLoggerRunnable(notifier));
    }
}

From source file:org.openspaces.admin.internal.admin.DefaultAdmin.java

@Override
public void pushEventAsFirst(Object listener, Runnable notifier) {
    assertStateChangesPermitted();/*from w ww.j a va2 s.  c o m*/
    synchronized (DefaultAdmin.this) {
        eventsQueue[Math.abs(listener.hashCode() % eventsExecutorServices.length)]
                .addFirst(toLoggerRunnable(notifier));
    }
}

From source file:org.openspaces.admin.internal.admin.DefaultAdmin.java

@Override
public void pushScheduleMonitorCorrelatedEvent(Object listener, Runnable notifier) {
    assertStateChangesPermitted();/*  w w  w  .j  a  v a  2  s  .c om*/
    synchronized (DefaultAdmin.this) {
        scheduleMonitorCorrelatedEventsQueue[Math.abs(listener.hashCode() % eventsExecutorServices.length)]
                .add(toLoggerRunnable(notifier));
    }
}

From source file:SoftValuedHashMap.java

public Set<Map.Entry<K, V>> entrySet() {
    if (this.entrySet == null) {
        this.entrySet = new AbstractSet<Map.Entry<K, V>>() {
            public Iterator iterator() {
                return createHashIterator(WeakIdentityMap.ENTRIES);
            }//from   w w  w .j  a v  a 2s . c  o  m

            public boolean contains(Object o) {
                if (!(o instanceof Map.Entry)) {
                    return false;
                }
                Map.Entry entry = (Map.Entry) o;
                Object key = entry.getKey();

                Entry[] tab = ReferencedValueHashMap.this.table;
                int hash = key == null ? 0 : key.hashCode();
                int index = (hash & 0x7fffffff) % tab.length;

                for (Entry e = tab[index], prev = null; e != null; e = e.next) {
                    Object entryValue = e.get();

                    if (entryValue == null) {
                        // Clean up after a cleared Reference.
                        ReferencedValueHashMap.this.modCount++;
                        if (prev != null) {
                            prev.next = e.next;
                        } else {
                            tab[index] = e.next;
                        }
                        ReferencedValueHashMap.this.count--;
                    } else if (e.hash == hash && e.equals(entry)) {
                        return true;
                    } else {
                        prev = e;
                    }
                }

                return false;
            }

            public boolean remove(Object o) {
                if (!(o instanceof Map.Entry)) {
                    return false;
                }
                Map.Entry entry = (Map.Entry) o;
                Object key = entry.getKey();
                Entry[] tab = ReferencedValueHashMap.this.table;
                int hash = key == null ? 0 : key.hashCode();
                int index = (hash & 0x7fffffff) % tab.length;

                for (Entry e = tab[index], prev = null; e != null; e = e.next) {
                    Object entryValue = e.get();

                    if (entryValue == null) {
                        // Clean up after a cleared Reference.
                        ReferencedValueHashMap.this.modCount++;
                        if (prev != null) {
                            prev.next = e.next;
                        } else {
                            tab[index] = e.next;
                        }
                        ReferencedValueHashMap.this.count--;
                    } else if (e.hash == hash && e.equals(entry)) {
                        ReferencedValueHashMap.this.modCount++;
                        if (prev != null) {
                            prev.next = e.next;
                        } else {
                            tab[index] = e.next;
                        }
                        ReferencedValueHashMap.this.count--;

                        e.setValue(null);
                        return true;
                    } else {
                        prev = e;
                    }
                }
                return false;
            }

            public int size() {
                return ReferencedValueHashMap.this.count;
            }

            public void clear() {
                ReferencedValueHashMap.this.clear();
            }

            public String toString() {
                return WeakIdentityMap.toString(this);
            }
        };
    }

    return this.entrySet;
}

From source file:com.google.gwt.emultest.java.util.TreeMapTest.java

/**
 * Verify entry to be immutable and to have correct values of {@code Map.Entry#toString()}
 * and {@code Map.Entry#hashCode()}.//from www  .ja va 2  s . c o  m
 */
@SuppressWarnings("unchecked")
private static void verifyEntry(Entry entry) {
    try {
        entry.setValue(new Object());
        fail("should throw UnsupportedOperationException");
    } catch (UnsupportedOperationException expected) {
    }
    Object key = entry.getKey();
    Object value = entry.getValue();
    int expectedHashCode = (key == null ? 0 : key.hashCode()) ^ (value == null ? 0 : value.hashCode());
    assertEquals(expectedHashCode, entry.hashCode());
    assertEquals(key + "=" + value, entry.toString());
}

From source file:com.xwtec.xwserver.util.json.JSONObject.java

public int hashCode() {
    int hashcode = 19;
    if (isNullObject()) {
        return hashcode + JSONNull.getInstance().hashCode();
    }/*from   ww w . ja va 2  s .c om*/
    for (Iterator entries = properties.entrySet().iterator(); entries.hasNext();) {
        Map.Entry entry = (Map.Entry) entries.next();
        Object key = entry.getKey();
        Object value = entry.getValue();
        hashcode += key.hashCode() + JSONUtils.hashCode(value);
    }
    return hashcode;
}

From source file:org.openspaces.admin.internal.admin.DefaultAdmin.java

@Override
public void raiseEvent(Object listener, Runnable notifier) {
    synchronized (DefaultAdmin.this) {
        // even though we are taking a lock we are not calling #assertStateChangesPermitted()
        // this is ok since submit() is non-blocking.
        eventsExecutorServices[Math.abs(listener.hashCode() % eventsExecutorServices.length)]
                .submit(toLoggerRunnable(notifier));
    }//from   ww  w  .  ja  v  a 2  s.co m
}

From source file:com.gs.collections.impl.test.Verify.java

/**
 * Assert that {@code objectA} and {@code objectB} are equal (via the {@link Object#equals(Object)} method,
 * and that they both return the same {@link Object#hashCode()}.
 *//*from w  w w .ja va2  s  . c o  m*/
public static void assertEqualsAndHashCode(String itemNames, Object objectA, Object objectB) {
    try {
        if (objectA == null || objectB == null) {
            Assert.fail("Neither item should be null: <" + objectA + "> <" + objectB + '>');
        }

        Assert.assertFalse("Neither item should equal null", objectA.equals(null));
        Assert.assertFalse("Neither item should equal null", objectB.equals(null));
        Verify.assertNotEquals("Neither item should equal new Object()", objectA.equals(new Object()));
        Verify.assertNotEquals("Neither item should equal new Object()", objectB.equals(new Object()));
        Assert.assertEquals("Expected " + itemNames + " to be equal.", objectA, objectA);
        Assert.assertEquals("Expected " + itemNames + " to be equal.", objectB, objectB);
        Assert.assertEquals("Expected " + itemNames + " to be equal.", objectA, objectB);
        Assert.assertEquals("Expected " + itemNames + " to be equal.", objectB, objectA);
        Assert.assertEquals("Expected " + itemNames + " to have the same hashCode().", objectA.hashCode(),
                objectB.hashCode());
    } catch (AssertionError e) {
        Verify.throwMangledException(e);
    }
}

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

private void buildAllComponents() {
    for (Map.Entry<Field, AbstractComponetAdapter> entrySet : adapters.entrySet()) {
        Field field = entrySet.getKey();

        Acomponent ac = field.getDeclaredAnnotation(Acomponent.class);

        AbstractComponetAdapter adapter = entrySet.getValue();
        Object target = getTargetForField(field);

        adapter.setComponentsBuilder(this);
        adapter.build(field, target);//from   ww w .j a v  a 2s. c o m

        Adialog adialog = objectAnnotated.getClass().getDeclaredAnnotation(Adialog.class);

        if (adialog.debug()) {
            try {
                Object component = adapter.getComponent();

                StringBuilder sgroup = new StringBuilder();
                for (String group : ac.groups()) {
                    sgroup.append("[ ");
                    sgroup.append(group);
                    sgroup.append(" ]");
                }

                String toolTipText = String.format("Name: %s - ToolTipText: %s - Groups: %s", ac.ref(),
                        ac.toolTipText(), sgroup);
                MethodUtils.method(component, "setToolTipText", String.class).invoke(toolTipText);
            } catch (Exception e) {
                infoError(e);
            }
        } else {
            Object component = adapter.getComponent();
            MethodUtils.method(component, "setToolTipText", String.class).invoke(getString(ac.toolTipText()));

        }

        try {
            Object value = field.get(target);
            adapter.update(field, value);

            if (ac.type().equals(EnumComponentType.PANEL)) {

                Object component = adapter.getComponent();

                //                    Object[] components = ((JPanel) component).getComponents();
                //                    
                //                    for (Object c : components) {
                //                        boolean bvalue = (boolean) MethodUtils.method(c, "isEnabled").invoke();
                //                        mc.put(c, bvalue);
                //                    }
                PropertyChangeListener pcl = new PropertyChangeListener() {
                    Map<Object, Boolean> mc = new HashMap<>();

                    @Override
                    public void propertyChange(PropertyChangeEvent evt) {
                        Object[] components = ((JPanel) component).getComponents();

                        if (evt.getPropertyName().equals("enabled")) {

                            boolean enable = (boolean) evt.getNewValue();

                            for (Object com : components) {
                                if (!mc.containsKey(com)) {
                                    boolean bvalue = (boolean) MethodUtils.method(com, "isEnabled").invoke();
                                    mc.put(com, bvalue);
                                }

                                if (enable) {
                                    MethodUtils.method(com, "setEnabled", boolean.class).invoke(mc.get(com));
                                } else {
                                    MethodUtils.method(com, "setEnabled", boolean.class).invoke(enable);
                                }
                            }
                        }
                    }
                };

                MethodUtils.method(component, "addPropertyChangeListener", PropertyChangeListener.class)
                        .invoke(pcl);
                putContainer(value.hashCode(), component);
            }
        } catch (IllegalArgumentException | IllegalAccessException e) {
            infoError(e);
            throw new UnsupportedOperationException(e);
        }
    }
}