package hirondelle.fish.all.preferences;
import java.util.*;
import java.util.regex.*;
import hirondelle.web4j.model.ModelCtorException;
import hirondelle.web4j.model.ModelUtil;
import hirondelle.web4j.model.Check;
import hirondelle.web4j.security.SafeText;
import static hirondelle.web4j.util.Consts.FAILS;
public final class Preferences {
public Preferences(SafeText aUserName, Locale aLocale) throws ModelCtorException {
fUserName = aUserName;
fLocale = aLocale;
validateState();
}
public SafeText getUserName() { return fUserName; }
public Locale getLocale() { return fLocale; }
public @Override String toString() {
return ModelUtil.toStringFor(this);
}
public @Override boolean equals( Object aThat ) {
Boolean result = ModelUtil.quickEquals(this, aThat);
if ( result == null ){
Preferences that = (Preferences) aThat;
result = ModelUtil.equalsFor(this.getSignificantFields(), that.getSignificantFields());
}
return result;
}
public @Override int hashCode() {
if ( fHashCode == 0 ) {
fHashCode = ModelUtil.hashCodeFor(getSignificantFields());
}
return fHashCode;
}
private final SafeText fUserName;
private final Locale fLocale;
private int fHashCode;
private static final Pattern ACCEPTED_PATTERN = Pattern.compile("(?:\\S){6,50}");
private void validateState() throws ModelCtorException {
ModelCtorException ex = new ModelCtorException();
if ( FAILS == Check.required(fUserName, Check.pattern(ACCEPTED_PATTERN)) ) {
ex.add("Name is required, 6..50 chars, no spaces.");
}
if ( FAILS == Check.required(fLocale) ) {
ex.add("Language is required.");
}
if ( ! ex.isEmpty() ) throw ex;
}
private Object[] getSignificantFields(){
return new Object[] {fUserName, fLocale};
}
}