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

103 lines
3.3 KiB
Java

/**
*
*/
package org.gcube.accounting.datamodel.aggregation;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Map;
import org.gcube.accounting.datamodel.AggregatedUsageRecord;
import org.gcube.accounting.datamodel.decorators.AggregatedField;
import org.gcube.accounting.datamodel.decorators.RequiredField;
import org.gcube.accounting.datamodel.validations.annotations.ValidInteger;
import org.gcube.accounting.datamodel.validations.annotations.ValidLong;
import org.gcube.accounting.exception.InvalidValueException;
/**
* This Class is for library internal use only
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*/
public class ServiceUsageRecord extends org.gcube.accounting.datamodel.implementations.ServiceUsageRecord implements AggregatedUsageRecord<ServiceUsageRecord, org.gcube.accounting.datamodel.implementations.ServiceUsageRecord> {
/**
* Generated Serial Version UID
*/
private static final long serialVersionUID = 6387584974618335623L;
// Redefining DURATION to Set @AggregatedField
@RequiredField @ValidLong @AggregatedField
public static final String DURATION = "duration";
@RequiredField @ValidInteger @AggregatedField
protected static final String INVOCATION_COUNT = "invocationCount";
@RequiredField @ValidLong @AggregatedField
protected static final String MAX_INVOCATION_TIME = "maxInvocationTime";
@RequiredField @ValidLong @AggregatedField
protected static final String MIN_INVOCATION_TIME = "minInvocationTime";
@Override
protected void init(){
super.init();
this.resourceProperties.put(AGGREGATED, true);
}
public ServiceUsageRecord(){
super();
}
protected ServiceUsageRecord(Map<String, Serializable> properties) throws InvalidValueException{
super(properties);
}
public ServiceUsageRecord(org.gcube.accounting.datamodel.implementations.ServiceUsageRecord record) throws InvalidValueException{
super(record.getResourceProperties());
this.setInvocationCount(1);
long duration = record.getDuration();
this.setMinInvocationTime(duration);
this.setMaxInvocationTime(duration);
Calendar creationTime = record.getCreationTime();
this.setCreationTime(creationTime);
this.setStartTime(creationTime);
this.setEndTime(creationTime);
}
public int getInvocationCount() {
return (Integer) this.resourceProperties.get(INVOCATION_COUNT);
}
public void setInvocationCount(int invocationCount) throws InvalidValueException {
setResourceProperty(INVOCATION_COUNT, invocationCount);
}
public long getMaxInvocationTime() {
return (Long) this.resourceProperties.get(MAX_INVOCATION_TIME);
}
public void setMaxInvocationTime(long maxInvocationTime) throws InvalidValueException {
setResourceProperty(MAX_INVOCATION_TIME, maxInvocationTime);
}
public long getMinInvocationTime() {
return (Long) this.resourceProperties.get(MIN_INVOCATION_TIME);
}
public void setMinInvocationTime(long minInvocationTime) throws InvalidValueException {
setResourceProperty(MIN_INVOCATION_TIME, minInvocationTime);
}
/**
* {@inheritDoc}
*/
@Override
public ServiceUsageRecord getAggregatedUsageRecord(
org.gcube.accounting.datamodel.implementations.ServiceUsageRecord usageRecord)
throws InvalidValueException {
if(usageRecord instanceof ServiceUsageRecord){
return (ServiceUsageRecord) usageRecord;
}
return new ServiceUsageRecord(usageRecord);
}
}