Having this class:
public class DataPeriod {
private final String key;
private final LocalDate from;
private final LocalDate to;
private final Map<LocalDate, DataPoint> dataPoints = new HashMap<LocalDate, DataPoint>();
public DataPeriod(String key, LocalDate from, LocalDate to) {
this.key = key;
this.from = from;
this.to = to;
}
public LocalDate getFrom() {
return from;
}
public LocalDate getTo() {
return to;
}
public void hit(int id) {
DataPoint dataPoint = getOrCreate();
dataPoint.hit(id);
}
private DataPoint getOrCreate() {
LocalDate now = LocalDate.now();
String dataPointKey = key + now.toString();
if ( !dataPoints.containsKey(now) ) {
DataPoint dataPoint = new DataPoint(dataPointKey);
dataPoints.put(now, dataPoint);
}
return dataPoints.get(dataPointKey);
}
public long count() {
long count = 0l;
for (DataPoint dataPoint : dataPoints.values()) {
count += dataPoint.count();
}
return count;
}
Could be a good idea extract the methods hit(int id)
and getOrCreate()
to some other class as a service or helper. But then they should be modified in order to receive parameter for DataPoint
.
I think has sense include them into DataPoint
class because they represent actions which DataPeriod
must know.
There is some pattern for that scenario? In terms of concurrency extract or not extract these methods makes any difference?
Aucun commentaire:
Enregistrer un commentaire