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

functionalj.event.Topic Maven / Gradle / Ivy

// ============================================================================
// Copyright (c) 2017-2019 Nawapunth Manusitthipol (NawaMan - http://nawaman.net).
// ----------------------------------------------------------------------------
// MIT License
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// ============================================================================
package functionalj.event;

import static java.util.Objects.requireNonNull;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Predicate;

import functionalj.function.Func1;
import functionalj.function.FuncUnit1;
import functionalj.function.FuncUnit2;
import functionalj.list.FuncList;
import functionalj.result.Result;
import lombok.val;

public class Topic {
    
    private final AtomicReference>> subscriptions 
            = new AtomicReference<>(FuncList.empty());
    
    private final AtomicBoolean isActive = new AtomicBoolean(true);
    
    public boolean isActive() {
        return isActive.get();
    }
    
    boolean publish(DATA data) {
        boolean stillActive = isActive.get();
        if (stillActive) {
            val result = Result.valueOf(data);
            notifySubscription(result);
        }
        return stillActive;
    }
    
    void done() {
        notifySubscription(Result.ofNoMore());
        
        isActive.set(false);
        subscriptions.set(FuncList.empty());
    }
    
    private void notifySubscription(Result result) {
        subscriptions
            .get()
            .filter (Subscription::isActive)
            .forEach(sub -> {
                try {
                    sub.notifyNext(result);
                } catch (Throwable e) {
                    // TODO: handle exception
                }
            });
    }
    
    private  Topic newSubTopic(FuncUnit2, Topic> resultConsumer) {
        val topic = new SubTopic(this, resultConsumer);
        return topic;
    }
    
    @SuppressWarnings("unchecked")
    public  Topic map(Func1 mapper) {
        requireNonNull(mapper);
        return (Topic)newSubTopic((Result r, Topic targetTopic) -> {
            val result = r.map(mapper);
            targetTopic.notifySubscription((Result)result);
        });
    }
    
    @SuppressWarnings("unchecked")
    public  Topic mapResult(Func1, Result> mapper) {
        requireNonNull(mapper);
        return (Topic)newSubTopic((Result r, Topic targetTopic) -> {
            val result = mapper.apply(r);
            targetTopic.notifySubscription((Result)result);
        });
    }
    public Topic filter(Predicate filter) {
        requireNonNull(filter);
        return (Topic)newSubTopic((Result r, Topic targetTopic) -> {
            val result = r.filter(filter);
            targetTopic.notifySubscription((Result)result);
        });
    }
    
    public Topic filterResult(Predicate> filter) {
        requireNonNull(filter);
        return (Topic)newSubTopic((Result r, Topic targetTopic) -> {
            val result = r.flatMap(d -> filter.test(r) ? r : Result.ofNull());
            targetTopic.notifySubscription((Result)result);
        });
    }
    
    public Subscription subscribe(FuncUnit1 subscribe) {
        return subscribe(subscribe.thenReturn(Subscription.Continue));
    }
    public Subscription subscribe(Func1 subscribe) {
        return onNext(result -> {
            return result.map(subscribe).orElse(Subscription.Continue);
        });
    }
    
    public Subscription onNext(FuncUnit1> subscribe) {
        return onNext(subscribe.thenReturn(Subscription.Continue));
    }
    public Subscription onNext(Func1, Cancellation> subscribe) {
        requireNonNull(subscribe);
        val subscription = new Subscription(this, subscribe);
        subscriptions.getAndUpdate(subs -> {
            if (subs.isEmpty() && !isActive.get()) {
                unsubcribe(subscription);
                return subs;
            }
            return subs.append(subscription).toImmutableList();
        });
        return subscription;
    }
    
    void unsubcribe(Subscription subscription) {
        subscription.notifyNext(Result.ofNoMore());
        subscriptions.getAndUpdate(subs -> {
            val newSub = subs.exclude(subscription).toImmutableList();
            if (newSub.isEmpty())
                done();
            return newSub;
        });
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy