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:se.vgregion.portal.innovatinosslussen.domain.TypesBeanTest.java

void doGetterSetterValuesMatch(Object o) throws IllegalAccessException, InstantiationException {
    BeanMap bm = new BeanMap(o);

    final String javaLangPackageName = String.class.getPackage().getName();

    for (Object key : bm.keySet()) {
        String name = (String) key;

        if ("ideaContentPrivate".equals(name) || "ideaPerson".equals(name)
                || "ideaContentPublic".equals(name)) {
            continue;
        }/*  w  w  w . j ava2s.  c  om*/

        if (bm.getWriteMethod(name) != null) {
            if (bm.getType(name).equals(String.class)) {
                bm.put(name, name);
                Assert.assertTrue(name == bm.get(name));
            } else {
                Class clazz = bm.getType(name);

                if (!clazz.getName().startsWith(javaLangPackageName) && !clazz.isEnum()) {
                    Object value = defaultPrim.get(clazz);
                    if (value == null) {
                        value = clazz.newInstance();
                    }
                    bm.put(name, value);
                    Assert.assertTrue("1, " + o.getClass() + "." + key, value.equals(bm.get(name)));
                    Assert.assertTrue("2, " + o.getClass() + "." + key,
                            value.hashCode() == bm.get(name).hashCode());
                }
            }
        }

    }
}

From source file:ssc.Sensor.java

/** 
 * Indicates whether given object is equal to this one.
 * /*www  . j  a  va 2 s.  c  om*/
 * @param obj Object with which to compare
 * 
 * @return <code>true</code> if equal
 */
@Override
public boolean equals(Object obj) {

    if (obj == null || obj.getClass() != getClass()) {

        return false;
    }

    return this.hashCode() == obj.hashCode();

}

From source file:WeakIdentityMap.java

static int hashCode(Object a) {
    if (a == null) {
        return -1;
    }//from w ww.j  a v a2  s. c  om
    if (!a.getClass().isArray()) {
        return a.hashCode();
    }
    if (a instanceof Object[]) {
        return hashCode((Object[]) a);
    } else if (a instanceof int[]) {
        return hashCode((int[]) a);
    } else if (a instanceof float[]) {
        return hashCode((float[]) a);
    } else if (a instanceof long[]) {
        return hashCode((long[]) a);
    } else if (a instanceof double[]) {
        return hashCode((double[]) a);
    } else if (a instanceof byte[]) {
        return hashCode((byte[]) a);
    } else if (a instanceof char[]) {
        return hashCode((char[]) a);
    } else if (a instanceof boolean[]) {
        return hashCode((boolean[]) a);
    } else if (a instanceof short[]) {
        return hashCode((short[]) a);
    } else {
        int hash = a.getClass().hashCode();
        return hash == 0 ? -1 : hash;
    }
}

From source file:edu.cornell.mannlib.vitro.webapp.beans.BaseResourceBean.java

@Override
public boolean equals(Object obj) {
    if (obj == null)
        return false;
    else if (obj instanceof BaseResourceBean) {
        String thisURI = this.getURI();
        String thatURI = ((BaseResourceBean) obj).getURI();
        if (thisURI != null && thatURI != null) {
            return thisURI.equals(thatURI);
        }//www .  j a  va 2  s .  com
    }
    return obj.hashCode() == this.hashCode();
}

From source file:org.elasticsearch.common.collect.CopyOnWriteHashMap.java

@Override
public V get(Object key) {
    Preconditions.checkArgument(key != null, "Null keys are not supported");
    final int hash = key.hashCode();
    return root.get(key, hash);
}

From source file:com.zacwolf.commons.email.Email.java

@Override
public boolean equals(Object e) {
    if ((e instanceof EmailTemplate) || (e instanceof EmailFromTemplate))
        return this.hashCode() == e.hashCode();

    return false;
}

From source file:com.xpn.xwiki.objects.BaseProperty.java

/**
 * {@inheritDoc}//from  w  w  w .ja va2  s  . c om
 * 
 * @see com.xpn.xwiki.objects.BaseElement#equals(java.lang.Object)
 */
@Override
public boolean equals(Object el) {
    // Same Java object, they sure are equal
    if (this == el) {
        return true;
    }

    // I hate this.. needed for hibernate to find the object
    // when loading the collections..
    if ((this.object == null) || ((BaseProperty) el).getObject() == null) {
        return (hashCode() == el.hashCode());
    }

    if (!super.equals(el)) {
        return false;
    }

    return (getId() == ((BaseProperty) el).getId());
}

From source file:org.elasticsearch.common.collect.CopyOnWriteHashMap.java

/**
 * Remove the given key from this map. The current hash table is not modified.
 *//*from  w w  w. j a va2 s.c o m*/
public CopyOnWriteHashMap<K, V> copyAndRemove(Object key) {
    Preconditions.checkArgument(key != null, "Null keys are not supported");
    final int hash = key.hashCode();
    final InnerNode<K, V> newRoot = root.remove(key, hash);
    if (root == newRoot) {
        return this;
    } else {
        return new CopyOnWriteHashMap<>(newRoot, size - 1);
    }
}

From source file:org.eclipse.wb.tests.designer.rcp.model.rcp.ViewPartTest.java

/**
 * Test for {@link IViewSite} implementation.
 */// ww w . j  ava 2 s .co m
public void test_IViewSite() throws Exception {
    ViewPartInfo part = parseJavaInfo("import org.eclipse.ui.*;", "import org.eclipse.ui.part.*;",
            "public class Test extends ViewPart {", "  public static final String ID = 'some.view.Identifier';",
            "  public Test() {", "  }", "  public void createPartControl(Composite parent) {",
            "    Composite container = new Composite(parent, SWT.NULL);", "  }", "  public void setFocus() {",
            "  }", "}");
    part.refresh();
    assertHierarchy("{this: org.eclipse.ui.part.ViewPart} {this} {}",
            "  {parameter} {parent} {/new Composite(parent, SWT.NULL)/}",
            "    {implicit-layout: org.eclipse.swt.layout.FillLayout} {implicit-layout} {}",
            "    {new: org.eclipse.swt.widgets.Composite} {local-unique: container} {/new Composite(parent, SWT.NULL)/}",
            "      {implicit-layout: absolute} {implicit-layout} {}");
    //
    Object viewSite = ReflectionUtils.invokeMethod(part.getObject(), "getViewSite()");
    try {
        ReflectionUtils.invokeMethod(viewSite, "getShell()");
        fail();
    } catch (NotImplementedException e) {
    }
    assertEquals("IViewSite_stub", viewSite.toString());
    assertEquals(0, viewSite.hashCode());
    assertEquals("some.view.Identifier", ReflectionUtils.invokeMethod(viewSite, "getId()"));
    assertEquals(null, ReflectionUtils.invokeMethod(viewSite, "getSecondaryId()"));
    {
        Object window = ReflectionUtils.invokeMethod(viewSite, "getWorkbenchWindow()");
        assertSame(DesignerPlugin.getActiveWorkbenchWindow(), window);
    }
}

From source file:org.sakuli.services.forwarder.gearman.GearmanResultServiceImplTest.java

@Test
public void testSaveAllResultsJobSubmitFailedAddCacheResults() throws Exception {
    when(properties.getServiceType()).thenReturn("passive");
    final String queueName = "check_results";
    when(properties.getServerQueue()).thenReturn(queueName);
    final String host = "99.99.99.20";
    when(properties.getServerHost()).thenReturn(host);
    final int port = 4730;
    when(properties.getServerPort()).thenReturn(port);
    when(properties.getNagiosHost()).thenReturn("win7sakuli");
    when(properties.isCacheEnabled()).thenReturn(true);

    GearmanClient gearmanClient = mock(GearmanClientImpl.class);
    doReturn(gearmanClient).when(testling).getGearmanClient();
    GearmanJobServerConnection connection = mock(GearmanJobServerConnection.class);
    doReturn(connection).when(testling).getGearmanConnection(host, port);
    GearmanJob job = mock(GearmanJob.class);
    doReturn(job).when(testling).creatJob(any(NagiosCheckResult.class));
    when(gearmanClient.addJobServer(connection)).thenReturn(true);
    when(gearmanClient.submit(job)).thenThrow(new SakuliRuntimeException("Something went wrong!"));

    NagiosCheckResult mockedResult1 = Mockito.mock(NagiosCheckResult.class);
    NagiosCheckResult mockedResult2 = Mockito.mock(NagiosCheckResult.class);
    when(gearmanCacheService.getCachedResults()).thenReturn(Arrays.asList(mockedResult1, mockedResult2));

    NagiosCheckResult newResult = new NagiosCachedCheckResult(queueName,
            "sakuli_demo22__2015_03_07_12_59_00_00", testResult);
    when(checkResultBuilder.build()).thenReturn(newResult);

    doAnswer(invocationOnMock -> {/*from w w w  . j a  v a2s.co  m*/
        List<NagiosCheckResult> results = ((List) invocationOnMock.getArguments()[0]);
        assertEquals(results.size(), 3L);

        assertEquals(results.get(0), newResult);
        assertEquals(results.get(1), mockedResult1);
        assertEquals(results.get(2), mockedResult2);
        return null;
    }).when(gearmanCacheService).cacheResults(anyList());
    StringBuilder sendOrder = new StringBuilder();
    doAnswer(invocationOnMock -> {
        Object result = invocationOnMock.getArguments()[1];
        Assert.assertTrue(result instanceof NagiosCheckResult);
        sendOrder.append(result.hashCode());
        return invocationOnMock.callRealMethod();

    }).when(testling).sendResult(any(), any());

    testling.saveAllResults();

    //checks
    assertEquals(sendOrder.toString(),
            "" + mockedResult2.hashCode() + mockedResult1.hashCode() + newResult.hashCode());
    verify(gearmanCacheService).cacheResults(anyList());
    verify(gearmanCacheService).getCachedResults();
    verify(exceptionHandler, times(3)).handleException(any(Throwable.class), eq(true));
    verify(testling).getGearmanClient();
    verify(testling).getGearmanConnection(host, port);
    verify(gearmanClient).addJobServer(connection);
    verify(gearmanClient).shutdown();
}