package hirondelle.fish.main.member;
import hirondelle.web4j.model.ModelCtorException;
import hirondelle.web4j.model.ModelUtil;
import hirondelle.web4j.model.Check;
import hirondelle.web4j.model.Id;
import hirondelle.web4j.security.SafeText;
import hirondelle.web4j.util.Util;
import hirondelle.web4j.model.Code;
import hirondelle.fish.main.codes.CodeTable;
import static hirondelle.web4j.util.Consts.FAILS;
public final class Member {
public Member (Id aId, SafeText aMemberName, Boolean aIsActive, Id aDisposition) throws ModelCtorException {
fId = aId;
fName = aMemberName;
fIsActive = Util.nullMeansFalse(aIsActive);
fDisposition = CodeTable.codeFor(aDisposition, CodeTable.DISPOSITIONS);
validateState();
}
public Id getId() { return fId; }
public Boolean getIsActive() { return fIsActive; }
public SafeText getName() { return fName; }
public Code getDisposition(){ return fDisposition; }
@Override public String toString() {
return ModelUtil.toStringFor(this);
}
@Override public boolean equals( Object aThat ) {
Boolean result = ModelUtil.quickEquals(this, aThat);
if ( result == null ){
Member that = (Member) aThat;
result = ModelUtil.equalsFor(this.getSignificantFields(), that.getSignificantFields());
}
return result;
}
@Override public int hashCode() {
if ( fHashCode == 0 ) {
fHashCode = ModelUtil.hashCodeFor(getSignificantFields());
}
return fHashCode;
}
private final Id fId;
private final SafeText fName;
private final Boolean fIsActive;
private final Code fDisposition;
private int fHashCode;
private void validateState() throws ModelCtorException {
ModelCtorException ex = new ModelCtorException();
if ( FAILS == Check.optional(fId, Check.range(1,50)) ) {
ex.add("Id is optional, 1..50 chars.");
}
if ( FAILS == Check.required(fName, Check.range(2,50))) {
ex.add("Name is required, 2..50 chars.");
}
if ( FAILS == Check.required(fIsActive) ) {
ex.add("Is Active is required. Inactive Members will not appear in the RSVP list.");
}
if ( FAILS == Check.required(fDisposition) ) {
ex.add("Disposition is required.");
}
if ( ! ex.isEmpty() ) throw ex;
}
private Object[] getSignificantFields(){
return new Object[]{fName, fIsActive, fDisposition};
}
}