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

com.github.davidmoten.rx.OperationToOperator Maven / Gradle / Ivy

There is a newer version: 0.7.19
Show newest version
package com.github.davidmoten.rx;

import rx.Observable;
import rx.Observable.Operator;
import rx.Subscriber;
import rx.functions.Func1;
import rx.observers.Subscribers;
import rx.subjects.PublishSubject;

/**
 * Converts an Operation (a function converting one Observable into another)
 * into an {@link Operator}.
 * 
 * @param 
 *            to type
 * @param 
 *            from type
 */
public class OperationToOperator implements Operator {

	public static  Operator toOperator(
			Func1, Observable> operation) {
		return new OperationToOperator(operation);
	}

	/**
	 * The operation to convert.
	 */
	private final Func1, Observable> operation;

	/**
	 * Constructor.
	 * 
	 * @param operation
	 *            to be converted into {@link Operator}
	 */
	public OperationToOperator(Func1, Observable> operation) {
		this.operation = operation;
	}

	@Override
	public Subscriber call(Subscriber subscriber) {
		final PublishSubject subject = PublishSubject.create();
		Subscriber result = Subscribers.from(subject);
		subscriber.add(result);
		operation.call(subject).subscribe(subscriber);
		return result;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy