本文整理汇总了Java中rx.Observable.Operator类的典型用法代码示例。如果您正苦于以下问题:Java Operator类的具体用法?Java Operator怎么用?Java Operator使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Operator类属于rx.Observable包,在下文中一共展示了Operator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: chunkToDocument
import rx.Observable.Operator; //导入依赖的package包/类
/**
* Convert a chunk to a Elasticsearch document
* @param chunk the chunk to convert
* @param fallbackCRSString a string representing the CRS that should be used
* to index the chunk if it does not specify a CRS itself (may be null if no
* CRS is available as fallback)
* @param parserOperator the operator used to parse the chunk stream into
* stream events
* @param indexerFactories a sequence of indexer factories that should be
* used to index the chunk
* @param <T> the type of the stream events created by <code>parserOperator</code>
* @return an observable that will emit the document
*/
private <T extends StreamEvent> Observable<Map<String, Object>> chunkToDocument(
ChunkReadStream chunk, String fallbackCRSString,
Operator<T, Buffer> parserOperator,
List<? extends IndexerFactory> indexerFactories) {
List<StreamIndexer<T>> indexers = new ArrayList<>();
indexerFactories.forEach(factory -> {
@SuppressWarnings("unchecked")
StreamIndexer<T> i = (StreamIndexer<T>)factory.createIndexer();
if (fallbackCRSString != null && i instanceof CRSAware) {
((CRSAware)i).setFallbackCRSString(fallbackCRSString);
}
indexers.add(i);
});
return RxHelper.toObservable(chunk)
.lift(parserOperator)
.doOnNext(e -> indexers.forEach(i -> i.onEvent(e)))
.last() // "wait" until the whole chunk has been consumed
.map(e -> {
// create the Elasticsearch document
Map<String, Object> doc = new HashMap<>();
indexers.forEach(i -> doc.putAll(i.getResult()));
return doc;
});
}
示例2: toLinkedHashSet
import rx.Observable.Operator; //导入依赖的package包/类
/**
* Cache all items and emit a single LinkedHashSet with all data when onComplete is called
* @return
*/
public static <T> Operator<Set<T>, T> toLinkedHashSet() {
return new Operator<Set<T>, T>() {
@Override
public Subscriber<? super T> call(final Subscriber<? super Set<T>> o) {
final Set<T> set = new LinkedHashSet<T>();
return new Subscriber<T>() {
@Override
public void onCompleted() {
o.onNext(set);
o.onCompleted();
}
@Override
public void onError(Throwable e) {
o.onError(e);
}
@Override
public void onNext(T t) {
set.add(t);
}
};
}
};
}
示例3: openChunkToDocument
import rx.Observable.Operator; //导入依赖的package包/类
/**
* Open a chunk and convert it to an Elasticsearch document. Retry operation
* several times before failing.
* @param path the path to the chunk to open
* @param chunkMeta metadata about the chunk
* @param indexMeta metadata used to index the chunk
* @return an observable that emits the document
*/
private Observable<Map<String, Object>> openChunkToDocument(
String path, ChunkMeta chunkMeta, IndexMeta indexMeta) {
return Observable.defer(() -> store.rxGetOne(path)
.flatMapObservable(chunk -> {
List<? extends IndexerFactory> factories;
Operator<? extends StreamEvent, Buffer> parserOperator;
// select indexers and parser depending on the mime type
String mimeType = chunkMeta.getMimeType();
if (belongsTo(mimeType, "application", "xml") ||
belongsTo(mimeType, "text", "xml")) {
factories = xmlIndexerFactories;
parserOperator = new XMLParserOperator();
} else if (belongsTo(mimeType, "application", "json")) {
factories = jsonIndexerFactories;
parserOperator = new JsonParserOperator();
} else {
return Observable.error(new NoStackTraceThrowable(String.format(
"Unexpected mime type '%s' while trying to index "
+ "chunk '%s'", mimeType, path)));
}
// call meta indexers
Map<String, Object> metaResults = new HashMap<>();
for (MetaIndexerFactory metaIndexerFactory : metaIndexerFactories) {
MetaIndexer metaIndexer = metaIndexerFactory.createIndexer();
metaIndexer.onIndexChunk(path, chunkMeta, indexMeta);
metaResults.putAll(metaIndexer.getResult());
}
// convert chunk to document and close it
return chunkToDocument(chunk, indexMeta.getFallbackCRSString(),
parserOperator, factories)
.doAfterTerminate(chunk::close)
// add results from meta indexers to converted document
.doOnNext(doc -> doc.putAll(metaResults));
}))
.retryWhen(makeRetry());
}
示例4: byCharacter
import rx.Observable.Operator; //导入依赖的package包/类
/**
* Converts a String into an Observable that emits the chars in the String.
*
* <p><img width="640" height="315" src="https://raw.github
* .com/wiki/ReactiveX/RxJava/images/rx-operators/from.png" alt="">
*
* @param source the source String
* @return an Observable that emits each char in the source String
* @see <a href="https://github.com/ReactiveX/RxJava/wiki/Creating-Observables#from">RxJava wiki:
* from</a>
*/
public static Observable<String> byCharacter(Observable<String> source) {
return source.lift(
new Operator<String, String>() {
@Override
public Subscriber<? super String> call(final Subscriber<? super String> subscriber) {
return new Subscriber<String>(subscriber) {
@Override
public void onCompleted() {
subscriber.onCompleted();
}
@Override
public void onError(Throwable e) {
subscriber.onError(e);
}
@Override
public void onNext(String str) {
for (char c : str.toCharArray()) {
subscriber.onNext(Character.toString(c));
}
}
};
}
});
}
示例5: testOnStartCalledOnceViaLift
import rx.Observable.Operator; //导入依赖的package包/类
@Test
public void testOnStartCalledOnceViaLift() {
final AtomicInteger c = new AtomicInteger();
Observable.just(1, 2, 3, 4).lift(new Operator<Integer, Integer>() {
@Override
public Subscriber<? super Integer> call(final Subscriber<? super Integer> child) {
return new Subscriber<Integer>() {
@Override
public void onStart() {
c.incrementAndGet();
request(1);
}
@Override
public void onComplete() {
child.onComplete();
}
@Override
public void onError(Throwable e) {
child.onError(e);
}
@Override
public void onNext(Integer t) {
child.onNext(t);
request(1);
}
};
}
}).subscribe();
assertEquals(1, c.get());
}
示例6: rate
import rx.Observable.Operator; //导入依赖的package包/类
public static <T> Operator<T, T> rate(final String label, final long interval, final TimeUnit units) {
final String caption = getSourceLabel(label);
return new Operator<T, T>() {
@Override
public Subscriber<? super T> call(final Subscriber<? super T> child) {
final AtomicLong counter = new AtomicLong();
final String sUnits = (interval == 1) ? TIME_UNIT[units.ordinal()] : String.format("({} {})", interval, TIME_UNIT[units.ordinal()]);
child.add(
Observable.interval(interval, units)
.subscribe(new Action1<Long>() {
@Override
public void call(Long t1) {
LOG.info("{} {} / {}", caption, counter.getAndSet(0), sUnits);
}
}));
return new Subscriber<T>(child) {
@Override
public void onCompleted() {
if (!isUnsubscribed())
child.onCompleted();
}
@Override
public void onError(Throwable e) {
if (!isUnsubscribed())
child.onError(e);
}
@Override
public void onNext(T t) {
counter.incrementAndGet();
if (!isUnsubscribed())
child.onNext(t);
}
};
}
};
}
示例7: join
import rx.Observable.Operator; //导入依赖的package包/类
/**
* Concatenates the sequence of values by adding a separator between them and emitting the result
* once the source completes.
*
* <p><img width="640" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/St
* .join.png" alt="">
*
* <p>The conversion from the value type to String is performed via {@link String#valueOf(Object)}
* calls.
*
* <p>For example:
*
* <p>
*
* <pre>
* Observable<Object> source = Observable.from("a", 1, "c");
* Observable<String> result = join(source, ", ");
* </pre>
*
* <p>will yield a single element equal to "a, 1, c".
*
* @param source the source sequence of CharSequence values
* @param separator the separator to a
* @return an Observable which emits a single String value having the concatenated values of the
* source observable with the separator between elements
*/
public static Observable<String> join(
final Observable<String> source, final CharSequence separator) {
return source.lift(
new Operator<String, String>() {
@Override
public Subscriber<String> call(final Subscriber<? super String> child) {
final JoinParentSubscriber parent = new JoinParentSubscriber(child, separator);
child.add(parent);
child.setProducer(
new Producer() {
@Override
public void request(long n) {
if (n > 0) {
parent.requestAll();
}
}
});
return parent;
}
});
}
示例8: testSetProducerSynchronousRequest
import rx.Observable.Operator; //导入依赖的package包/类
@Test
public void testSetProducerSynchronousRequest() {
TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
Observable.just(1, 2, 3).lift(new Operator<Integer, Integer>() {
@Override
public Subscriber<? super Integer> call(final Subscriber<? super Integer> child) {
final AtomicLong requested = new AtomicLong();
child.setProducer(new Producer() {
@Override
public void request(long n) {
if (!requested.compareAndSet(0, n)) {
child.onError(new RuntimeException("Expected to receive request before onNext but didn't"));
}
}
});
Subscriber<Integer> parent = new Subscriber<Integer>() {
@Override
public void onComplete() {
child.onComplete();
}
@Override
public void onError(Throwable e) {
child.onError(e);
}
@Override
public void onNext(Integer t) {
if (requested.compareAndSet(0, -99)) {
child.onError(new RuntimeException("Got values before requested"));
}
}
};
child.add(parent);
return parent;
}
}).subscribeOn(Schedulers.newThread()).subscribe(ts);
ts.awaitTerminalEvent();
ts.assertNoErrors();
}
示例9: toOperator
import rx.Observable.Operator; //导入依赖的package包/类
public static <T, R> Operator<R, T> toOperator(
Func1<? super Observable<T>, ? extends Observable<R>> function) {
return OperatorFromTransformer.toOperator(function);
}
示例10: toOperator
import rx.Observable.Operator; //导入依赖的package包/类
public static <R, T> Operator<R, T> toOperator(
Func1<? super Observable<T>, ? extends Observable<R>> operation) {
return new OperatorFromTransformer<R, T>(operation);
}
示例11: uberTracer
import rx.Observable.Operator; //导入依赖的package包/类
/**
* Trace all parts of an observable's state and especially when
* notifications are discarded due to being unsubscribed. This should
* only used for debugging purposes.
* @param label
* @return
*/
public static <T> Operator<T, T> uberTracer(String label) {
final String caption = getSourceLabel(label);
return new Operator<T, T>() {
@Override
public Subscriber<? super T> call(final Subscriber<? super T> s) {
s.add(Subscriptions.create(new Action0() {
@Override
public void call() {
LOG.info("{} unsubscribing", caption);
}
}));
return new Subscriber<T>(s) {
private AtomicLong completedCounter = new AtomicLong();
private AtomicLong nextCounter = new AtomicLong();
private AtomicLong errorCounter = new AtomicLong();
@Override
public void onCompleted() {
if (!s.isUnsubscribed()) {
s.onCompleted();
}
else {
LOG.info("{} ({}) Discarding onCompleted", caption, completedCounter.incrementAndGet());
}
}
@Override
public void onError(Throwable e) {
if (!s.isUnsubscribed()) {
s.onCompleted();
}
else {
LOG.info("{} ({}) Discarding onError", caption, errorCounter.incrementAndGet());
}
}
@Override
public void onNext(T t) {
if (!s.isUnsubscribed()) {
s.onNext(t);
}
else {
LOG.info("{} ({}) Discarding onNext", caption, nextCounter.incrementAndGet());
}
}
};
}
};
}
示例12: flatten
import rx.Observable.Operator; //导入依赖的package包/类
public static <T> Operator<T, Iterable<T>> flatten() {
return new OperatorFlattenIterable<T>();
}
示例13: split
import rx.Observable.Operator; //导入依赖的package包/类
public static Operator<byte[], byte[]> split(final int n) {
if (n <= 0)
throw new IllegalArgumentException("n must be positive");
return new Operator<byte[], byte[]>() {
@Override
public Subscriber<? super byte[]> call(
final Subscriber<? super byte[]> child) {
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
CompositeSubscription parent = new CompositeSubscription();
child.add(parent);
return new Subscriber<byte[]>(parent) {
@Override
public void onCompleted() {
if (!isUnsubscribed() && buffer.size() > 0) {
child.onNext(buffer.toByteArray());
buffer.reset();
}
if (!isUnsubscribed())
child.onCompleted();
}
@Override
public void onError(Throwable e) {
child.onError(e);
}
@Override
public void onNext(byte[] b) {
int i = 0;
while (!isUnsubscribed() && i < b.length) {
int num = Math.min(b.length - i, n - buffer.size());
if (num > 0)
buffer.write(b, i, num);
if (buffer.size() == n) {
child.onNext(buffer.toByteArray());
buffer.reset();
}
i += num;
}
}
};
}
};
}
示例14: passthrough
import rx.Observable.Operator; //导入依赖的package包/类
/**
* Operator that acts as a pass through. Use this when you want the operator
* to be interchangable with the default implementation being a single passthrough.
*
* {code
* <pre>
* Operator<T,T> customOperator = RxUtil.passthrough();
* observable
* .lift(customOperator)
* </pre>
* }
*/
public static <T> Operator<T, T> passthrough() {
return new Operator<T, T>() {
@Override
public Subscriber<? super T> call(final Subscriber<? super T> o) {
return o;
}
};
}
示例15: onLift
import rx.Observable.Operator; //导入依赖的package包/类
/**
* Invoked just as the operator functions is called to bind two operations together into a new
* {@link Observable} and the return value is used as the lifted function
* <p>
* This can be used to decorate or replace the {@link Operator} instance or just perform extra
* logging, metrics and other such things and pass-thru the onSubscribe.
*
* @param lift
* original {@link Operator}{@code <R, T>}
* @return {@link Operator}{@code <R, T>} function that can be modified, decorated, replaced or just
* returned as a pass-thru
*/
public <T, R> Operator<T, R> onLift(final Operator<T, R> lift) {
return lift;
}