Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added hashCode() #79

Merged
merged 2 commits into from
Feb 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/main/java/edu/nps/moves/dis7/EntityID.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.nps.moves.dis7;

import java.io.*;
import java.util.Objects;


/**
Expand Down Expand Up @@ -165,4 +166,10 @@ public boolean equalsImpl(Object obj)

return ivarsEqual;
}

@Override
public int hashCode()
{
return Objects.hash(siteID, applicationID, entityID);
}
} // end of class
90 changes: 90 additions & 0 deletions src/test/java/edu/nps/moves/dis7/EntityIDTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package edu.nps.moves.dis7;


import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.junit.Assert.*;


public class EntityIDTest
{
EntityID id1;
EntityID id2;
EntityID id3;
EntityID id4;

@Before
public void setUp()
throws Exception
{
id1 = new EntityID();
id1.setEntityID(1);
id1.setApplicationID(2);
id1.setSiteID(3);
// id1 and id2 have the same entityID, applicationID, siteID
id2 = new EntityID();
id2.setEntityID(1);
id2.setApplicationID(2);
id2.setSiteID(3);

id3 = new EntityID();
id3.setEntityID(1);
id3.setApplicationID(2);
id3.setSiteID(4); // different site id

id4 = new EntityID();
id4.setEntityID(1);
id4.setApplicationID(2);
id4.setSiteID(5); // different site id
}

@Test
public void EntityIDEquals_test()
{
assertEquals(id1, id2);
assertNotEquals(id1, id3);
assertNotEquals(id3, id4);
}

@Test
public void EntityID_Lists_tests()
{
List<EntityID> list = Arrays.asList(id1, id2, id3, id4);

assertTrue(list.contains(id1));
assertEquals(id2, list.get(list.indexOf(id2)));

List<EntityID> collect = list.stream().filter(e -> e.getSiteID() == 3).collect(Collectors.toList());
assertEquals(2, collect.size());
assertTrue(collect.contains(id1));
}

@Test
public void EntityID_Map_tests()
{
Map<EntityID, String> map = new HashMap<>();
map.put(id1, "id1");
map.put(id2, "id2"); // id1 and id2 have the same entity, application, and site id. Therefore "id2" replaces "id1"
map.put(id3, "id3");
map.put(id4, "id4");

assertEquals(3, map.size());
assertEquals("id3", map.get(id3));
assertTrue(map.containsKey(id2));
assertTrue(map.containsValue("id3"));

map.forEach((entityID, s) -> {
if (entityID.equals(id4)) {
assertTrue(true);
}
});
}
}