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

rx.internal.operators.OperatorReplay Maven / Gradle / Ivy

There is a newer version: 0.20.7
Show newest version
/**
 * Copyright 2014 Netflix, Inc.
 * 
 * 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 rx.internal.operators;


import rx.Observable;
import rx.Observable.OnSubscribe;
import rx.Scheduler;
import rx.Subscriber;
import rx.subjects.Subject;

/**
 * Replay with limited buffer and/or time constraints.
 * 
 * 
 * @see MSDN: Observable.Replay overloads
 */
public final class OperatorReplay {
    /** Utility class. */
    private OperatorReplay() {
        throw new IllegalStateException("No instances!");
    }

    /**
     * Creates a subject whose client observers will observe events
     * propagated through the given wrapped subject.
     * @param  the element type
     * @param subject the subject to wrap
     * @param scheduler the target scheduler
     * @return the created subject
     */
    public static  Subject createScheduledSubject(Subject subject, Scheduler scheduler) {
        final Observable observedOn = subject.observeOn(scheduler);
        SubjectWrapper s = new SubjectWrapper(new OnSubscribe() {

            @Override
            public void call(Subscriber o) {
                subscriberOf(observedOn).call(o);
            }

        }, subject);
        return s;
    }

    /**
     * Return an OnSubscribeFunc which delegates the subscription to the given observable.
     * 
     * @param  the value type
     * @param target the target observable
     * @return the function that delegates the subscription to the target
     */
    public static  OnSubscribe subscriberOf(final Observable target) {
        return new OnSubscribe() {
            @Override
            public void call(Subscriber t1) {
                target.unsafeSubscribe(t1);
            }
        };
    }

    /**
     * A subject that wraps another subject.
     * @param  the value type
     */
    public static final class SubjectWrapper extends Subject {
        /** The wrapped subject. */
        final Subject subject;

        public SubjectWrapper(OnSubscribe func, Subject subject) {
            super(func);
            this.subject = subject;
        }

        @Override
        public void onNext(T args) {
            subject.onNext(args);
        }

        @Override
        public void onError(Throwable e) {
            subject.onError(e);
        }

        @Override
        public void onCompleted() {
            subject.onCompleted();
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy