UserKey implements Serializable { @Override public boolean equals(Object o) { if (this == o) { return true; } if (o instanceof UserKey) { UserKey key = (UserKey) o; boolean result = true; for (UserSelection sel: this.selection) { switch (sel) { case LOGIN: case EMAIL: result &= key.pi.getUsername() == null ? false : key.pi.getUsername().equals(pi.getUsername()); break; case IP: result &= key.pi.getRemoteAddress() == null ? false : key.pi.getRemoteAddress().equals(pi.getRemoteAddress()); break; default: result = false; break; } } return result; } return false; } UserKey(ProcessingInformation pi, UserSelection[] selection); ProcessingInformation getPi(); @Override boolean equals(Object o); @Override String toString();  }
@Test public void equalsTest() { ProcessingInformation pi = new ProcessingInformation("Request"); pi.setRemoteAddress("123.12.122.33"); pi.setUsername("user1"); ProcessingInformation pi1 = new ProcessingInformation("Request"); pi1.setRemoteAddress("99.99.99.123"); pi1.setUsername("user1"); ProcessingInformation pi2 = new ProcessingInformation("Request"); pi2.setRemoteAddress("99.99.99.123"); pi2.setUsername("user2"); ProcessingInformation pi3 = new ProcessingInformation("Request"); UserKey uk = new UserKey(pi, new UserSelection[]{UserSelection.LOGIN}); UserKey uk1 = new UserKey(pi1, new UserSelection[]{UserSelection.IP}); UserKey uk2 = new UserKey(pi2, new UserSelection[]{UserSelection.LOGIN, UserSelection.IP}); UserKey uk3 = new UserKey(pi3, new UserSelection[]{UserSelection.LOGIN, UserSelection.IP}); Assert.assertTrue(uk.equals(uk1)); Assert.assertFalse(uk.equals(uk2)); Assert.assertTrue(uk1.equals(uk2)); Assert.assertFalse(uk1.equals(uk)); Assert.assertFalse(uk2.equals(uk)); Assert.assertTrue(uk2.equals(uk2)); Assert.assertFalse(uk3.equals(uk)); Assert.assertTrue(uk3.equals(uk3)); Assert.assertFalse(uk2.equals(uk3)); }