com.maxifier.mxcache.impl.caches.def.ByteInlineDependencyCache Maven / Gradle / Ivy
/*
* Copyright (c) 2008-2014 Maxifier Ltd. All Rights Reserved.
*/
package com.maxifier.mxcache.impl.caches.def;
import com.maxifier.mxcache.caches.*;
import com.maxifier.mxcache.impl.MutableStatistics;
import com.maxifier.mxcache.impl.resource.DependencyNode;
import com.maxifier.mxcache.impl.resource.DependencyNodeVisitor;
import com.maxifier.mxcache.util.HashWeakReference;
import com.maxifier.mxcache.util.TIdentityHashSet;
import gnu.trove.set.hash.THashSet;
import javax.annotation.Nonnull;
import java.lang.ref.Reference;
import java.util.Iterator;
import java.util.Set;
/**
* Inline dependency caches are special ones that implement both Cache and DependencyNode.
* It is used to reduce memory consumption of memory cache for the simplest types of caches.
*
* THIS IS GENERATED CLASS! DON'T EDIT IT MANUALLY!
*
* GENERATED FROM PInlineDependencyCache.template
*
* @author Andrey Yakoushin ([email protected])
* @author Alexander Kochurov ([email protected])
*/
public class ByteInlineDependencyCache extends ByteInlineCacheImpl implements DependencyNode {
/**
* Set of dependent nodes. It may be null cause there is no need to allocate whole set for each node.
*/
private Set> dependentNodes;
private Reference selfReference;
public ByteInlineDependencyCache(Object owner, ByteCalculatable calculable, MutableStatistics statistics) {
super(owner, calculable, statistics);
setDependencyNode(this);
}
public synchronized void visitDependantNodes(DependencyNodeVisitor visitor) {
if (dependentNodes != null) {
for (Iterator> it = dependentNodes.iterator(); it.hasNext();) {
Reference ref = it.next();
DependencyNode instance = ref.get();
if (instance != null) {
visitor.visit(instance);
} else {
it.remove();
}
}
}
}
@Override
public synchronized Reference getSelfReference() {
if (selfReference == null) {
selfReference = new HashWeakReference(this);
}
return selfReference;
}
@Override
public synchronized void trackDependency(DependencyNode node) {
if (dependentNodes == null) {
dependentNodes = new THashSet>();
}
dependentNodes.add(node.getSelfReference());
}
@Override
public synchronized void addNode(@Nonnull CleaningNode cache) {
throw new UnsupportedOperationException("Inline dependency node should has only one cache");
}
@Override
public synchronized void appendNodes(TIdentityHashSet elements) {
elements.add(this);
}
}