package hirondelle.predict.main.lists;
import hirondelle.web4j.model.Check;
import hirondelle.web4j.model.ModelCtorException;
import hirondelle.web4j.model.ModelUtil;
import static hirondelle.web4j.util.Consts.FAILS;
import hirondelle.web4j.model.Id;
import hirondelle.web4j.model.DateTime;
import hirondelle.web4j.security.SafeText;
public final class PredictionList {
public PredictionList(
Id aId, SafeText aTitle, DateTime aCreationDate, Id aUser
) throws ModelCtorException {
fId = aId;
fTitle = aTitle;
fCreationDate = aCreationDate;
fUser = aUser;
validateState(CheckDate.YES);
}
public PredictionList(
Id aId, SafeText aTitle, DateTime aCreationDate, Id aUser, SafeText aUserScreenName
) throws ModelCtorException {
fId = aId;
fTitle = aTitle;
fCreationDate = aCreationDate;
fUser = aUser;
fUserScreenName = aUserScreenName;
validateState(CheckDate.YES);
}
public PredictionList(Id aId, SafeText aTitle, Id aUser) throws ModelCtorException {
fId = aId;
fTitle = aTitle;
fUser = aUser;
validateState(CheckDate.NO);
}
public Id getId() { return fId; }
public SafeText getTitle() { return fTitle; }
public DateTime getCreationDate() { return fCreationDate; }
public Id getUser() { return fUser; }
public SafeText getUserScreenName() { return fUserScreenName; }
@Override public String toString(){
return ModelUtil.toStringFor(this);
}
@Override public boolean equals(Object aThat){
Boolean result = ModelUtil.quickEquals(this, aThat);
if ( result == null ) {
PredictionList that = (PredictionList)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 fTitle;
private DateTime fCreationDate;
private final Id fUser;
private SafeText fUserScreenName;
private int fHashCode;
private enum CheckDate{YES, NO};
private void validateState(CheckDate aCheckDate) throws ModelCtorException {
ModelCtorException ex = new ModelCtorException();
if ( FAILS == Check.required(fTitle, Check.max(50)) ) {
ex.add("Please provide a title (up to 50 characters)");
}
if ( FAILS == Check.required(fUser) ){
ex.add("User id is missing (programmer error)");
}
if( CheckDate.YES == aCheckDate ){
if ( FAILS == Check.required(fCreationDate) ) {
ex.add("Creation date missing is missing (programmer error).");
}
}
if ( ! ex.isEmpty() ) throw ex;
}
private Object[] getSignificantFields(){
return new Object[] {fTitle, fCreationDate, fUser};
}
}