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

com.maxifier.mxcache.impl.resource.nodes.MultipleDependencyNode Maven / Gradle / Ivy

/*
 * Copyright (c) 2008-2014 Maxifier Ltd. All Rights Reserved.
 */
package com.maxifier.mxcache.impl.resource.nodes;

import com.maxifier.mxcache.caches.CleaningNode;
import com.maxifier.mxcache.impl.resource.AbstractDependencyNode;
import com.maxifier.mxcache.util.TIdentityHashSet;

import java.util.*;
import java.lang.ref.WeakReference;

import javax.annotation.Nonnull;

/**
 * @author Alexander Kochurov ([email protected])
 */
public class MultipleDependencyNode extends AbstractDependencyNode {
    protected final List> instances = new ArrayList>();

    @Override
    public synchronized void addNode(@Nonnull CleaningNode node) {
        instances.add(new WeakReference(node));
    }

    @Override
    public synchronized void appendNodes(TIdentityHashSet elements) {
        for (Iterator> it = instances.iterator(); it.hasNext();) {
            WeakReference ref = it.next();
            CleaningNode node = ref.get();
            if (node != null) {
                elements.add(node);
            } else {
                it.remove();
            }
        }
    }

    @Override
    public String toString() {
        StringBuilder b = new StringBuilder("DependencyNode<");
        int gc = 0;
        int n = 0;
        for (WeakReference instance : instances) {
            CleaningNode node = instance.get();
            if (node == null) {
                gc++;
            } else {
                n++;
                b.append(node.toString()).append(", ");
            }
        }
        if (gc > 0) {
            b.append(gc).append(" GCed");
        } else if (n > 0) {
            b.setLength(b.length() - 2);
        }
        b.append(">");
        return b.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy