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

com.arm.mbed.cloud.sdk.subscribe.NotificationEmitter Maven / Gradle / Ivy

Go to download

The Pelion Cloud SDK (formerly known as Mbed Cloud SDK) provides a simplified interface to the Pelion Cloud APIs by exposing functionality using conventions and paradigms familiar to Java developers.

There is a newer version: 2.5.0
Show newest version
package com.arm.mbed.cloud.sdk.subscribe;

import java.util.LinkedList;
import java.util.List;

import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.FlowableEmitter;
import io.reactivex.FlowableOnSubscribe;

import com.arm.mbed.cloud.sdk.annotations.Internal;
import com.arm.mbed.cloud.sdk.annotations.Nullable;
import com.arm.mbed.cloud.sdk.annotations.Preamble;

@Preamble(description = "Notification emitter/publisher")
@Internal
public class NotificationEmitter {

    private final List> emitters = new LinkedList<>();

    /**
     * Constructor.
     */
    public NotificationEmitter() {
        super();
    }

    /**
     * Creates a communication channel for an observer.
     *
     * @param strategy
     *            backpressure strategy to apply.
     * @return a communication channel.
     */
    public Flowable create(BackpressureStrategy strategy) {

        final FlowableOnSubscribe source = new FlowableOnSubscribe() {

            @Override
            public void subscribe(FlowableEmitter emitter) {
                emitters.add(emitter);

            }
        };
        return Flowable.create(source, strategy);
    }

    /**
     * Completes the communication channel.
     */
    public void complete() {
        for (final FlowableEmitter emitter : emitters) {
            try {
                if (!emitter.isCancelled()) {
                    emitter.onComplete();
                }
            } catch (@SuppressWarnings("unused") Exception exception) {
                // Nothing to do
            }
        }
    }

    /**
     * Emits a notification onto the communication channel.
     *
     * @param notification
     *            notification to emit
     * @param throwable
     *            the exception to emit
     */
    public void emit(@Nullable T notification, @Nullable Throwable throwable) {
        if (throwable == null) {
            if (notification == null) {
                return;
            }
            for (final FlowableEmitter emitter : emitters) {
                emitter.onNext(notification);
            }
        } else {
            for (final FlowableEmitter emitter : emitters) {
                emitter.onError(throwable);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy