Occurrence.java
Home
/
src /
main /
java /
br /
ufrgs /
inf /
prosoft /
memoizeit /
Occurrence.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package br.ufrgs.inf.prosoft.memoizeit;
import br.ufrgs.inf.prosoft.memoizeit.cache.Cache;
import br.ufrgs.inf.prosoft.memoizeit.graph.Node;
import br.ufrgs.inf.prosoft.memoizeit.utils.Action;
import br.ufrgs.inf.prosoft.memoizeit.utils.ObjectUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
*
* @author romulo
*/
public class Occurrence {
private final String instance;
private final Object returnValue;
private final List<Parameter> parameters;
private final long startTime;
private final long endTime;
private boolean truncated;
public Occurrence(String instance, Object returnValue, List<Parameter> parameters, long startTime, long endTime) {
this.instance = instance;
this.returnValue = returnValue;
this.parameters = parameters;
this.startTime = startTime;
this.endTime = endTime;
this.truncated = true;
}
public String getInstance() {
return instance;
}
public Object getReturnValue() {
return this.returnValue;
}
public List<Parameter> getParameters() {
return this.parameters;
}
public long getExecutionTime() {
return this.endTime - this.startTime;
}
protected Occurrence getView(int depth) {
if (!this.truncated) {
return this;
}
this.truncated = false;
Object returnValue = ObjectUtils.deepCopy(this.returnValue);
Action onTruncate = () -> {
this.truncated = true;
};
ObjectUtils.truncateObject(returnValue, depth, onTruncate);
List<Parameter> parameters = new ArrayList<>();
for (Parameter parameter : this.parameters) {
Object parameterView = ObjectUtils.deepCopy(parameter.getData());
ObjectUtils.truncateObject(parameterView, depth, onTruncate);
parameters.add(new Parameter(parameter.getType(), parameterView));
}
Occurrence occurrence = new Occurrence(this.instance, returnValue, parameters, this.startTime, this.endTime);
return occurrence;
}
protected void simulateCaching(Cache cache) {
String key = this.parameters.toString();
Object cached = cache.get(key);
if (cached == null) {
cache.put(key, this.returnValue);
} else if (!cached.equals(this.returnValue)) {
cache.invalidate(key);
}
}
private void removeUnusedFields(Map<String, Object> map, String parameterType, Node<String> methodNode) {
Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Object> entry = iterator.next();
String getter = parameterType + ".get" + entry.getKey().substring(0, 1).toUpperCase() + entry.getKey().substring(1);
if (!methodNode.getLinks().stream().anyMatch(node -> node.getContent().equals(getter))) {
iterator.remove();
}
}
}
protected void removeUnusedFields(Node<String> methodNode) {
for (Parameter parameter : this.parameters) {
String parameterType = parameter.getType();
if (parameter.getData() instanceof Collection) {
Collection collection = (Collection) parameter.getData();
if (collection.stream().findAny().get() instanceof Map) {
Collection<Map<String, Object>> cast = collection;
cast.forEach(map -> removeUnusedFields(map, parameterType, methodNode));
}
} else if (parameter.getData() instanceof Map) {
Map<String, Object> map = (Map<String, Object>) parameter.getData();
removeUnusedFields(map, parameterType, methodNode);
}
}
}
}