com.bazaarvoice.commons.data.model.AbstractRoleBasedUser Maven / Gradle / Ivy
package com.bazaarvoice.commons.data.model;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Set;
public abstract class AbstractRoleBasedUser, L extends UserRole> extends AbstractUser implements RoleBasedUser {
private Set _roles;
@Override
public Set getRoles() {
return _roles;
}
public void setRoles(Collection roles) {
_roles = roles != null ? new LinkedHashSet(roles) : null;
}
public U roles(L... roles) {
setRoles(Arrays.asList(roles));
//noinspection unchecked
return (U) this;
}
public U roles(Collection roles) {
setRoles(roles);
//noinspection unchecked
return (U) this;
}
@Override
public boolean hasRoles() {
return _roles != null && !_roles.isEmpty();
}
@Override
public boolean isInRole(L... roles) {
if (_roles == null) {
return false;
}
for (L role : roles) {
if (_roles.contains(role)) {
return true;
}
}
return false;
}
public U addRoles(L... roles) {
addRoles(Arrays.asList(roles));
//noinspection unchecked
return (U) this;
}
public U addRoles(Collection roles) {
if (_roles == null) {
_roles = new LinkedHashSet(roles);
} else {
_roles.addAll(roles);
}
//noinspection unchecked
return (U) this;
}
public U removeRoles(L... roles) {
removeRoles(Arrays.asList(roles));
//noinspection unchecked
return (U) this;
}
public U removeRoles(Collection roles) {
if (_roles != null) {
_roles.removeAll(roles);
}
//noinspection unchecked
return (U) this;
}
@Override
public String toString() {
return super.toString() +
"[roles=" + _roles + "]";
}
}