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

com.joanzapata.android.kiss.api.internal.Injector Maven / Gradle / Ivy

Go to download

Kiss uses annotations to shorten the code to start asynchronous long running tasks and cache results

There is a newer version: 0.0.4
Show newest version
/**
 * Copyright 2014 Joan Zapata
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.joanzapata.android.kiss.api.internal;

import com.joanzapata.android.kiss.api.Message;

import java.lang.ref.WeakReference;

import static com.joanzapata.android.kiss.api.annotation.OnMessage.Priority;

public abstract class Injector {

    protected WeakReference target;

    void setTarget(T target) {
        this.target = new WeakReference(target);
        inject(target);
    }

    public T getTarget() {
        return target.get();
    }

    /**
     * Dispatch the event to the target.
     * @return true if object is still valid (target is alive)
     * false otherwise. In case of false, this object should
     * not be called anymore.
     */
    boolean dispatch(Message event, Priority priority) {
        T targetObject = target.get();
        if (targetObject == null) return false;
        dispatch(targetObject, event, priority);
        return true;
    }

    protected abstract void inject(T injectable);

    protected abstract void dispatch(T target, Message event, Priority priority);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy