From e05ed44193b424db1ecd7f1521b3157750e1e4ed Mon Sep 17 00:00:00 2001 From: "luke.cowan" Date: Mon, 22 Feb 2021 17:07:13 +0000 Subject: [PATCH 1/2] Added hashCode() --- src/main/java/edu/nps/moves/dis7/EntityID.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/edu/nps/moves/dis7/EntityID.java b/src/main/java/edu/nps/moves/dis7/EntityID.java index 7ccbe337..06aef300 100644 --- a/src/main/java/edu/nps/moves/dis7/EntityID.java +++ b/src/main/java/edu/nps/moves/dis7/EntityID.java @@ -1,6 +1,7 @@ package edu.nps.moves.dis7; import java.io.*; +import java.util.Objects; /** @@ -165,4 +166,10 @@ public boolean equalsImpl(Object obj) return ivarsEqual; } + + @Override + public int hashCode() + { + return Objects.hash(siteID, applicationID, entityID); + } } // end of class From 12c921028870016a6339a936e781ed980da808c5 Mon Sep 17 00:00:00 2001 From: "luke.cowan" Date: Tue, 23 Feb 2021 07:57:34 +0000 Subject: [PATCH 2/2] Added tests for hashCode() and equals --- .../java/edu/nps/moves/dis7/EntityIDTest.java | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/test/java/edu/nps/moves/dis7/EntityIDTest.java diff --git a/src/test/java/edu/nps/moves/dis7/EntityIDTest.java b/src/test/java/edu/nps/moves/dis7/EntityIDTest.java new file mode 100644 index 00000000..e5c0285a --- /dev/null +++ b/src/test/java/edu/nps/moves/dis7/EntityIDTest.java @@ -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 list = Arrays.asList(id1, id2, id3, id4); + + assertTrue(list.contains(id1)); + assertEquals(id2, list.get(list.indexOf(id2))); + + List 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 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); + } + }); + } +} \ No newline at end of file