class UserConverter implements ToServerConverter<ClientUser, User, ToServerConversionOptions>, ToClientConverter<User, ClientUser, ToClientConversionOptions> {

    @Override
    public ClientUser toClient(User user, ToClientConversionOptions options) {
        ClientUser clientUser = new ClientUser();

        clientUser.setFullName(user.getFullName());
        clientUser.setEmailAddress(user.getEmailAddress());
        clientUser.setExternallyDefined(user.isExternallyDefined());
        clientUser.setEnabled(user.isEnabled());
        clientUser.setPreviousPasswordChangeTime(user.getPreviousPasswordChangeTime());
        clientUser.setTenantId(user.getTenantId());
        clientUser.setUsername(user.getUsername());

        if (user.getRoles() != null){
            Set<ClientRole> roleSet = new HashSet<ClientRole>();
            // core class uses raw Set. Cast is safe
            @SuppressWarnings("unchecked")
            final Set<Role> roles = (Set<Role>) user.getRoles();
            for (Role role: roles){
                roleSet.add(roleConverter.toClient(role, null));
            }
            clientUser.setRoleSet(roleSet);
        }
        return clientUser;
    }

}