001/*
002 * Configurate
003 * Copyright (C) zml and Configurate contributors
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.spongepowered.configurate.reactive;
018
019/**
020 * A listener to events that may be called by an {@link Publisher}.
021 *
022 * <p>For every publisher this subscriber is subscribed to, the subscriber will
023 * only process one event at a time -- effectively, within a single publisher
024 * this subscriber does not have to be aware of concurrent effects.
025 *
026 * @param <V> the value that will be received
027 * @since 4.0.0
028 */
029@FunctionalInterface
030public interface Subscriber<V> {
031
032    /**
033     * Called to submit a new item.
034     *
035     * @param item the item available
036     * @since 4.0.0
037     */
038    void submit(V item);
039
040    /**
041     * When an error occurs while subscribing to an {@link Publisher}, or is
042     * thrown during the execution of {@link #submit(Object)} that is not
043     * otherwise handled, this method will be called with the error. The
044     * associated {@link Publisher} will not send further update signals after
045     * an error is thrown.
046     *
047     * @param thrown the exception thrown
048     * @since 4.0.0
049     */
050    default void onError(Throwable thrown) {
051        final Thread t = Thread.currentThread();
052        t.getUncaughtExceptionHandler().uncaughtException(t, thrown);
053    }
054
055    /**
056     * When the {@link Publisher} this is subscribed to closes without error,
057     * this method will be called.
058     *
059     * @since 4.0.0
060     */
061    default void onClose() {
062        // no-op
063    }
064
065}