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

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

/**
 * 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.Notification;
import rx.Observable.Operator;
import rx.Subscriber;

/**
 * Reverses the effect of {@link OperatorMaterialize} by transforming the Notification objects
 * emitted by a source Observable into the items or notifications they represent.
 * 

* *

* See here for the Microsoft Rx equivalent. * * @param the wrapped value type */ public final class OperatorDematerialize implements Operator> { /** Lazy initialization via inner-class holder. */ private static final class Holder { /** A singleton instance. */ static final OperatorDematerialize INSTANCE = new OperatorDematerialize(); } /** * @return a singleton instance of this stateless operator. */ @SuppressWarnings({ "rawtypes" }) public static OperatorDematerialize instance() { return Holder.INSTANCE; // using raw types because the type inference is not good enough } private OperatorDematerialize() { } @Override public Subscriber> call(final Subscriber child) { return new Subscriber>(child) { /** Do not send two onCompleted events. */ boolean terminated; @Override public void onNext(Notification t) { switch (t.getKind()) { case OnNext: if (!terminated) { child.onNext(t.getValue()); } break; case OnError: onError(t.getThrowable()); break; case OnCompleted: onCompleted(); break; } } @Override public void onError(Throwable e) { if (!terminated) { terminated = true; child.onError(e); } } @Override public void onCompleted() { if (!terminated) { terminated = true; child.onCompleted(); } } }; } }