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

com.github.dxee.dject.lifecycle.ManagedInstanceAction Maven / Gradle / Ivy

Go to download

A collection of guice extensions, help to improve the developer experience of guice.

There is a newer version: 1.5.1
Show newest version
package com.github.dxee.dject.lifecycle;

import java.lang.ref.Reference;
import java.util.concurrent.Callable;

/**
 * Runnable that applies one or more LifecycleActions to a managed instance T or Reference<T>.
 * For Reference<T> the action is invoked on a best-effort basis, if the referent is non-null
 * at time run() is invoked
 */
public final class ManagedInstanceAction implements Callable {
    private final Object target; // the managed instance
    private final Reference targetReference; // reference to the managed instance
    private final Iterable actions; // set of actions that will be applied to target

    public ManagedInstanceAction(Object target, Iterable actions) {
        // keep hard reference to target
        this.target = target;
        this.targetReference = null;
        this.actions = actions;
    }

    public ManagedInstanceAction(Reference target, Object context, Iterable actions) {
        this.target = null;
        this.targetReference = target; // keep hard reference to target
        this.actions = actions;
    }

    @Override
    public Void call() throws Exception {
        Object target = (targetReference == null) ? this.target : targetReference.get();
        if (target != null) {
            for (LifecycleAction m : actions) {
                m.call(target);
            }
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy