O2Principal implements Principal { @Override public boolean equals(final Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } return safeCast(o, O2Principal.class) .filter(p -> Objects.equal(getScheme(), p.getScheme())) .filter(p -> Objects.equal(getContext(), p.getContext())) .filter(p -> Objects.equal(getOAuthToken(), p.getOAuthToken())) .isPresent(); } O2Principal();  O2Principal(final Client client);  O2Principal(final OAuthToken token); private  O2Principal(final Client client, final OAuthToken token); OAuthToken getOAuthToken(); @Override String getName(); @Override boolean implies(final Subject subject); String getScheme(); Client getContext(); O2Principal merge(final Principal newPrincipal); @Override boolean equals(final Object o); @Override int hashCode();  }
@Test public void testEquality() { Client c1 = new Client(); c1.setId(IdUtil.next()); c1.setClientSecret("secret"); OAuthToken t1 = new OAuthToken(); t1.setId(IdUtil.next()); t1.setClient(c1); O2Principal p1 = new O2Principal(c1); O2Principal pSame = new O2Principal(c1); O2Principal p1Token = new O2Principal(t1); Client c2 = new Client(); c2.setId(IdUtil.next()); OAuthToken t2 = new OAuthToken(); t2.setId(IdUtil.next()); t2.setClient(c2); O2Principal p2 = new O2Principal(c2); O2Principal p2Token = new O2Principal(t2); Client c3 = new Client(); c3.setId(IdUtil.next()); O2Principal p3 = new O2Principal(c3); assertFalse(p1.equals(new Object())); assertFalse(p1.equals(null)); assertFalse(p1.equals(p2)); assertFalse(p1.equals(p2)); assertFalse(p1.equals(p3)); assertFalse(p2.equals(p3)); assertFalse(p1.equals(p1Token)); assertFalse(p2.equals(p2Token)); assertFalse(p1Token.equals(p2Token)); assertTrue(p1.equals(p1)); assertTrue(p1.equals(pSame)); }