package hirondelle.fish.translate.basetext;
import hirondelle.web4j.model.ModelCtorException;
import hirondelle.web4j.model.ModelUtil;
import hirondelle.web4j.model.Check;
import hirondelle.web4j.model.Id;
import hirondelle.web4j.util.Util;
import hirondelle.web4j.security.SafeText;
import static hirondelle.web4j.util.Consts.FAILS;
public final class BaseText {
public BaseText(Id aId, SafeText aBaseText, Boolean aIsCoderKey) throws ModelCtorException {
fId = aId;
fBaseText = aBaseText;
fIsCoderKey = Util.nullMeansFalse(aIsCoderKey);
validateState();
}
public Id getId(){ return fId; }
public SafeText getBaseText() { return fBaseText; }
public Boolean getIsCoderKey() { return fIsCoderKey; }
@Override public String toString(){
return ModelUtil.toStringFor(this);
}
@Override public boolean equals(Object aThat){
Boolean result = ModelUtil.quickEquals(this, aThat);
if ( result == null ){
BaseText that = (BaseText) 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 fBaseText;
private final Boolean fIsCoderKey;
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(fBaseText, Check.range(1, 255))) {
ex.add("Base Text is required, 1..255 chars, and must be unique.");
}
if ( FAILS == Check.required(fIsCoderKey)) {
ex.add("Is Coder Key is required.");
}
if ( ! ex.isEmpty() ) throw ex;
}
private Object[] getSignificantFields(){
return new Object[] {fBaseText, fIsCoderKey};
}
}