com.maxifier.mxcache.impl.resource.AbstractDependencyNode Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mxcache-runtime Show documentation
Show all versions of mxcache-runtime Show documentation
Constains all classes necessary for launching a MxCache-instrumentated application
/*
* Copyright (c) 2008-2014 Maxifier Ltd. All Rights Reserved.
*/
package com.maxifier.mxcache.impl.resource;
import com.maxifier.mxcache.util.HashWeakReference;
import gnu.trove.set.hash.THashSet;
import javax.annotation.Nullable;
import java.lang.ref.Reference;
import java.util.*;
/**
* @author Alexander Kochurov ([email protected])
*/
public abstract class AbstractDependencyNode 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 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();
}
}
}
}
/**
* @return approximate size (may include some dead nodes)
*/
public synchronized int getApproxSize() {
return dependentNodes == null ? 0 : dependentNodes.size();
}
@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());
}
protected static boolean equal(@Nullable Object a, @Nullable Object b) {
return a == b || (a != null && a.equals(b));
}
}