/* * The contents of this file are subject to the terms of the Common Development and * Distribution License (the License). You may not use this file except in compliance with the * License. * * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the * specific language governing permission and limitations under the License. * * When distributing Covered Software, include this CDDL Header Notice in each file and include * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL * Header, with the fields enclosed by brackets [] replaced by your own identifying * information: "Portions Copyright [year] [name of copyright owner]". * * Copyright 2016 ForgeRock AS. */ package com.forgerock.reactive; import org.forgerock.util.Function; import org.reactivestreams.Publisher; /** * Single is a reactive-streams compatible promise. * * @param * Type of the datum emitted */ public interface Single extends Publisher { /** Emitter is used to notify when the operation has been completed, successfully or not. */ public interface Emitter { /** * Signal a success value. * * @param t * the value, not null */ void onSuccess(V t); /** * Signal an exception. * * @param t * the exception, not null */ void onError(Throwable t); } /** Adapts the streaming api to a callback one. */ public interface OnSubscribe { /** * Called for each SingleObserver that subscribes. * @param e the safe emitter instance, never null * @throws Exception on error */ void onSubscribe(Emitter e) throws Exception; } /** * Transform this {@link Single} into a {@link Stream}. * * @return A new {@link Stream} */ Stream toStream(); /** * Transforms the value emitted by this single. * * @param * Type of the datum emitted after transformation * @param function * Function to apply to the result of this single. * @return A new {@link Single} with the transformed value. */ Single map(Function function); /** * Transforms asynchronously the value emitted by this single into another. * * @param * Type of the datum emitted after transformation * @param function * Function to apply to perform the asynchronous transformation * @return A new {@link Single} transforming the datum emitted by this {@link Single}. */ Single flatMap(Function, Exception> function); /** * Subscribe to the single value emitted by this {@link Single}. * * @param resultConsumer * A {@link Consumer} which will be invoked by the value emitted by this single * @param errorConsumer * A {@link Consumer} which will be invoked with the error emitted by this single */ void subscribe(Consumer resultConsumer, Consumer errorConsumer); }