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

org.javabits.yar.guice.WatcherRegistration Maven / Gradle / Ivy

/*
 * Copyright 2013 Romain Gilles
 *
 *    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 org.javabits.yar.guice;

import com.google.common.base.FinalizableReferenceQueue;
import com.google.common.base.FinalizableWeakReference;
import org.javabits.yar.*;

import javax.annotation.Nullable;
import java.lang.ref.WeakReference;
import java.util.IdentityHashMap;

import static org.javabits.yar.SupplierEvent.Type.ADD;
import static org.javabits.yar.SupplierEvent.Type.REMOVE;

/**
 * TODO comment
 * Date: 2/20/13
 * Time: 7:04 PM
 *
 * @author Romain Gilles
 */
class WatcherRegistration extends FinalizableWeakReference> implements Pair, Watcher>, org.javabits.yar.Registration {

    private final IdMatcher left;
    private final Watcher right;
    private final Registry registry;


    static  WatcherRegistration newWatcherRegistration(IdMatcher leftValue, SupplierListener supplierListener, FinalizableReferenceQueue referenceQueue, Registry registry) {
        return new WatcherRegistration<>(leftValue, new SupplierWatcherToSupplierListenerAdapter(supplierListener), referenceQueue, registry);
    }

    @SuppressWarnings("unchecked")
    static  WatcherRegistration newWatcherRegistration(IdMatcher leftValue, Watcher rightValue, FinalizableReferenceQueue referenceQueue, Registry registry) {
        return new WatcherRegistration(leftValue, new WatcherDecorator<>(rightValue), referenceQueue, registry);
    }

    WatcherRegistration(IdMatcher leftValue, Watcher rightValue, FinalizableReferenceQueue referenceQueue, Registry registry) {
        super(rightValue, referenceQueue);
        left = leftValue;
        right = rightValue;
        this.registry = registry;
    }

    @Override
    public Id id() {
        return left().id();
    }

    @Override
    public IdMatcher left() {
        return left;
    }

    @Override
    public Watcher right() {
        return right;
    }

    @Override
    public void finalizeReferent() {
        registry.removeWatcher(this);
    }

    @Override
    public String toString() {
        return "WatcherRegistration{" +
                "left=" + left +
                ", right=" + right +
                '}';
    }

    static class WatcherDecorator implements Watcher {
        private final WeakReference> delegate;
        private final IdentityHashMap, Supplier> trackedElements = new IdentityHashMap<>();

        WatcherDecorator(Watcher delegate) {
            this.delegate = new WeakReference<>(delegate);
        }

        @Nullable
        @Override
        public Supplier add(Supplier element) {
            Watcher watcher = delegate.get();
            if (watcher != null) {
                Supplier trackedElement = watcher.add(element);
                if (trackedElement != null) {
                    trackedElements.put(element, trackedElement);
                }
                return trackedElement;
            } else {
                clearTrackedElements();
                return null;
            }
        }

        private void clearTrackedElements() {
            trackedElements.clear();
        }

        @Override
        public void remove(Supplier element) {
            Supplier trackedElement = trackedElements.remove(element);
            if (trackedElement != null) {
                Watcher watcher = delegate.get();
                if (watcher != null) {
                    watcher.remove(trackedElement);
                } else {
                    clearTrackedElements();
                }
            }
        }

        @Override
        public String toString() {
            return "WatcherDecorator{" +
                    "delegate=" + delegate.get() +
                    '}';
        }
    }

    private static class SupplierWatcherToSupplierListenerAdapter implements Watcher {
        private final SupplierListener supplierListener;

        public SupplierWatcherToSupplierListenerAdapter(SupplierListener supplierListener) {
            this.supplierListener = supplierListener;
        }

        @Nullable
        @Override
        public Supplier add(Supplier supplier) {
            supplierListener.supplierChanged(new SupplierEvent(ADD, supplier));
            return supplier;
        }

        @Override
        public void remove(Supplier supplier) {
            supplierListener.supplierChanged(new SupplierEvent(REMOVE, supplier));
        }

        @Override
        public String toString() {
            return "SupplierWatcherToSupplierListenerAdapter{" +
                    "supplierListener=" + supplierListener +
                    '}';
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy