All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.maxifier.mxcache.impl.resource.AbstractDependencyNode Maven / Gradle / Ivy

Go to download

Constains all classes necessary for launching a MxCache-instrumentated application

There is a newer version: 2.6.9
Show newest version
/*
 * 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));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy