com.mongodb.async.client.Observables Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mongodb-driver-async Show documentation
Show all versions of mongodb-driver-async Show documentation
The MongoDB Asynchronous Driver
/*
* Copyright 2008-present MongoDB, 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 com.mongodb.async.client;
import com.mongodb.Block;
import com.mongodb.async.SingleResultCallback;
import java.util.List;
/**
* Observable helpers.
*
* Allows async methods to be converted into event-based {@link Observable}s.
*
* @since 3.1
* @deprecated Prefer the Reactive Streams-based asynchronous driver (mongodb-driver-reactivestreams artifactId)
*/
@Deprecated
public final class Observables {
/**
* Convert a {@link MongoIterable} into an {@link Observable}.
*
* @param mongoIterable the MongoIterable to subscribe to
* @param The type of result being observed
* @return the observable version of the mongoIterable
*/
public static Observable observe(final MongoIterable mongoIterable) {
return new Observable() {
@Override
public void subscribe(final Observer super TResult> observer) {
new MongoIterableSubscription(mongoIterable, observer);
}
};
}
/**
* Allows the conversion of {@link SingleResultCallback} based operations into an {@link Observable}
*
* Requires a {@link Block} that is passed the callback to be used with the operation.
* This is required to make sure that the operation only occurs once the {@link Subscription} signals for data.
*
* A typical example would be when wrapping callback based methods to make them observable.
* For example, converting {@link MongoCollection#count(SingleResultCallback)} into an {@link Observable}:
*
* {@code
* Observable countObservable = observe(new Block>() {
* public void apply(final SingleResultCallback callback) {
* collection.countDocuments(callback);
* }
* });
* }
*
*
* @param operation the block that implements the operation.
* @param The type of result being observed
* @return the observable version of the callback based operation
*/
public static Observable observe(final Block> operation) {
return new Observable() {
@Override
public void subscribe(final Observer super TResult> observer) {
new SingleResultCallbackSubscription(operation, observer);
}
};
}
/**
* Allows the conversion of {@link SingleResultCallback} based operations and flattens the results in an {@link Observable}.
*
* Requires a {@link Block} that is passed the callback to be used with the operation.
* This is required to make sure that the operation only occurs once the {@link Subscription} signals for data.
*
* @param operation the operation that is passed a callback and is used to delay execution of an operation until demanded.
* @param The type of result being observed
* @return a subscription
*/
public static Observable observeAndFlatten(final Block>> operation) {
return new Observable() {
@Override
public void subscribe(final Observer super TResult> observer) {
new FlatteningSingleResultCallbackSubscription(operation, observer);
}
};
}
private Observables() {
}
}