accounting-lib/src/main/java/org/gcube/accounting/datamodel/aggregation/scheduler/AggregationScheduler.java

60 lines
1.4 KiB
Java

package org.gcube.accounting.datamodel.aggregation.scheduler;
import java.util.ArrayList;
import java.util.List;
import org.gcube.accounting.datamodel.UsageRecord;
import org.gcube.accounting.persistence.PersistenceExecutor;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public abstract class AggregationScheduler {
protected static AggregationScheduler aggregationScheduler;
public static AggregationScheduler getInstance(){
if(aggregationScheduler==null){
aggregationScheduler = new BufferAggregationScheduler();
}
return aggregationScheduler;
}
protected List<UsageRecord> records;
/**
* @return the records
*/
public List<UsageRecord> getRecords() {
return records;
}
protected AggregationScheduler(){
this.records = new ArrayList<UsageRecord>();
}
protected void madeAggregation(UsageRecord UsageRecord){
}
/**
* Get an usage records and try to aggregate with other buffered
* Usage Record.
* @param usageRecord the Usage Record To Buffer
* @return true if is time to persist the buffered Usage Record
* @throws Exception if fails
*/
public synchronized void aggregate(UsageRecord UsageRecord, PersistenceExecutor persistenceExecutor) throws Exception {
madeAggregation(UsageRecord);
if(isTimeToPersist()){
persistenceExecutor.persist(records.toArray(new UsageRecord[records.size()]));
}
}
protected abstract boolean isTimeToPersist();
}