![JAR search and dependency download from the Maven repository](/logo.png)
hu.akarnokd.reactive4java.util.Observables Maven / Gradle / Ivy
Show all versions of reactive4java Show documentation
/*
* Copyright 2011-2013 David Karnok
*
* 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 hu.akarnokd.reactive4java.util;
import hu.akarnokd.reactive4java.base.Action0;
import hu.akarnokd.reactive4java.base.Action0E;
import hu.akarnokd.reactive4java.base.Func1;
import hu.akarnokd.reactive4java.base.Observable;
import hu.akarnokd.reactive4java.base.Observer;
import java.io.Closeable;
import java.io.IOException;
import javax.annotation.Nonnull;
/**
* Utility class that helps in creating various observable instances.
* @author akarnokd, 2013.01.10.
* @since 0.97
*/
public final class Observables {
/** Utility class. */
private Observables() { }
/**
* Converts the original Java Observable into an reactive-Observable instance.
* Since Java Observables had no concept of error and termination, and
* were considered active observable, the returned reactive-observable
* never terminates or throws an error.
* Note that since java-observables are not generic, ClassCastException
* might occur if the transmitted value has incompatible class.
* @param the element type
* @param javaObservable the java-observable to be used
* @return the reactive-observable
*/
@Nonnull
public static Observable toObservable(
@Nonnull final java.util.Observable javaObservable) {
return new OriginalObservableWrapper(javaObservable);
}
/**
* Converts the reactive-observable into the original Java Observable.
* Since java-observables are always active, the source
* sequence is immediately registered. In order to have a java-observable
* that activates once the first observer registers, supply this method
* with a Reactive.refCount() wrapped observable. The returned
* java-observable implements Closeable which can be used to
* terminate the connection to source and deregister all observers
* Incoming error and finish events will close the connection
* to the source and deregister all observers.
* @param the observable sequence type
* @param source the source sequence of anything
* @return the java-observable
* @see HybridSubject
*/
@Nonnull
public static OriginalObservableWrapper toOriginalObservable(
@Nonnull final Observable source) {
return toOriginalObservable(source, true);
}
/**
* Converts the reactive-observable into the original Java Observable.
* Since java-observables are always active, the source
* sequence is immediately registered. In order to have a java-observable
* that activates once the first observer registers, supply this method
* with a Reactive.refCount() wrapped observable. The returned
* java-observable implements Closeable which can be used to
* terminate the connection to source and deregister all observers
* Incoming error and finish events are ignored if unregisterObservers
* is false, or they will cause deregistration otherwise.
* @param the observable sequence type
* @param source the source sequence of anything
* @param unregisterObservers should the registered observers deregistered
* in case of an error or finish message or closing the observer?
* @return the java-observable
* @see HybridSubject
*/
@Nonnull
public static OriginalObservableWrapper toOriginalObservable(
@Nonnull final Observable source,
final boolean unregisterObservers) {
final java.util.Observable javaObservable = new java.util.Observable();
Closeable c = source.register(new Observer