I need to provide records to the caller from one or two different data sources and either within a specified date range or year range.
My dilemma is should I use overloaded methods or a Request object with state validation logic.
So either:
public List<Record> getRecords (Date fromDate, Date toDate, boolean dataSourceARequired, boolean dataSourceBRequired)
public List<Record> getRecords (int fromYear, int toYear, boolean dataSourceARequired, boolean dataSourceBRequired)
or something like this:
public List<Record> getRecords(Request request)
where Request would look something like:
public class Request{
private final Date fromDate;
private final Date toDate;
private final int fromYear;
private final int toYear;
private final boolean dataSourceARequired;
private final boolean dataSourceBRequired;
public Request(Date fromDate, Date toDate, boolean dataSourceARequired, boolean dataSourceBRequired){
if (fromDate == null) {
throw new IllegalArgumentException("fromDate can't be null");
}
if (toDate == null) {
throw new IllegalArgumentException("toDate can't be null");
}
if (!dataSourceARequired && !dataSourceBRequired){
throw new IllegalStateException ("No data source requested");
}
if (fromDate.after(toDate)){
throw new IllegalStateException ("startDate can't be after endDate");
}
this.fromDate = fromDate;
this.toDate = toDate;
this.dataSourceARequired = dataSourceARequired;
this.dataSourceBRequired = dataSourceBRequired;
this.fromYear = -1;
this.toYear = -1;
}
public Request(int fromYear, int toYear, boolean dataSourceARequired, boolean dataSourceBRequired){
if (fromYear > toYear) {
throw new IllegalArgumentException("fromYear can't be greater than toYear");
}
if (!dataSourceARequired && !dataSourceBRequired){
throw new IllegalStateException ("No data source requested");
}
this.dataSourceARequired = dataSourceARequired;
this.dataSourceBRequired = dataSourceBRequired;
this.fromYear = fromYear;
this.toYear = toYear;
this.fromDate = null;
this.toDate = null;
}
}
Or is there another way?
Aucun commentaire:
Enregistrer un commentaire