AuthorizableFacade implements Authorizable { @SuppressWarnings("WeakerAccess") public static @Nullable Authorizable ensureBestWrapper(final @Nullable Authorizable authorizable) { if (authorizable == null) { return null; } else if (authorizable instanceof AuthorizableFacade) { return authorizable; } else if (authorizable instanceof User) { return new UserFacade((User) authorizable); } else if (authorizable instanceof Group) { return new GroupFacade((Group) authorizable); } else { return new AuthorizableFacade<>(authorizable); } } @SuppressWarnings("WeakerAccess")  AuthorizableFacade(final @NotNull A delegate); @SuppressWarnings("WeakerAccess") static @Nullable Authorizable ensureBestWrapper(final @Nullable Authorizable authorizable); @Override String getID(); @Override boolean isGroup(); @Override Principal getPrincipal(); @Override Iterator<String> getPropertyNames(); @Override Iterator<String> getPropertyNames(final String relPath); @Override boolean hasProperty(final String relPath); @Override Value[] getProperty(final String relPath); @Override String getPath(); @Override Iterator<Group> declaredMemberOf(); @Override Iterator<Group> memberOf(); @Override final void setProperty(final String relPath, final Value value); @Override final void setProperty(final String relPath, final Value[] value); @Override final void remove(); @Override final boolean removeProperty(final String relPath);  }
@Test public void testEnsureBestWrapper() { assertNull("just null", AuthorizableFacade.ensureBestWrapper(null)); Authorizable justDelegate = mock(Authorizable.class); AuthorizableFacade<Authorizable> justFacade = getFacade(justDelegate); assertSame("same facade", justFacade, AuthorizableFacade.ensureBestWrapper(justFacade)); assertTrue("is facade", AuthorizableFacade.ensureBestWrapper(justDelegate) instanceof AuthorizableFacade); User userDelegate = mock(User.class); assertTrue("is facade", AuthorizableFacade.ensureBestWrapper(userDelegate) instanceof UserFacade); Group groupDelegate = mock(Group.class); assertTrue("is facade", AuthorizableFacade.ensureBestWrapper(groupDelegate) instanceof GroupFacade); SuperAuthz superDelegate = mock(SuperAuthz.class); assertTrue("is facade", AuthorizableFacade.ensureBestWrapper(superDelegate) instanceof AuthorizableFacade); }