Fixed broken leaky caching of OBJ models.

This commit is contained in:
RainWarrior 2015-12-30 16:08:08 +03:00
parent 33b1f8f30f
commit 10658f682e

View file

@ -11,6 +11,7 @@ import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Pattern;
@ -54,9 +55,13 @@ import org.apache.commons.lang3.tuple.Pair;
import com.google.common.base.Charsets;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
@SuppressWarnings("deprecation")
public class OBJModel implements IRetexturableModel, IModelCustomData
@ -1142,7 +1147,7 @@ public class OBJModel implements IRetexturableModel, IModelCustomData
public static class OBJState implements IModelState
{
protected Map<String, Boolean> visibilityMap = new HashMap<String, Boolean>();
protected Map<String, Boolean> visibilityMap = Maps.newHashMap();
public IModelState parent;
protected Operation operation = Operation.SET_TRUE;
@ -1236,6 +1241,27 @@ public class OBJModel implements IRetexturableModel, IModelCustomData
return builder.toString();
}
@Override
public int hashCode()
{
return Objects.hash(visibilityMap, parent, operation);
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
OBJState other = (OBJState) obj;
return Objects.equals(visibilityMap, other.visibilityMap) &&
Objects.equals(parent, other.parent) &&
operation == other.operation;
}
public enum Operation
{
SET_TRUE,
@ -1546,15 +1572,17 @@ public class OBJModel implements IRetexturableModel, IModelCustomData
}
}
private final Map<IModelState, OBJBakedModel> cache = new HashMap<IModelState, OBJBakedModel>();
private final LoadingCache<IModelState, OBJBakedModel> cache = CacheBuilder.newBuilder().maximumSize(20).build(new CacheLoader<IModelState, OBJBakedModel>()
{
public OBJBakedModel load(IModelState state) throws Exception
{
return new OBJBakedModel(model, state, format, textures);
}
});
public OBJBakedModel getCachedModel(IModelState state)
{
if (!cache.containsKey(state))
{
cache.put(state, new OBJBakedModel(this.model, state, this.format, this.textures));
}
return cache.get(state);
return cache.getUnchecked(state);
}
public OBJModel getModel()