本文整理汇总了Java中rx.Observable.Transformer类的典型用法代码示例。如果您正苦于以下问题:Java Transformer类的具体用法?Java Transformer怎么用?Java Transformer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Transformer类属于rx.Observable包,在下文中一共展示了Transformer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: toSet
import rx.Observable.Transformer; //导入依赖的package包/类
public static <T> Transformer<T, Set<T>> toSet() {
return new Transformer<T, Set<T>>() {
@Override
public Observable<Set<T>> call(Observable<T> o) {
return o.collect(new Func0<Set<T>>() {
@Override
public Set<T> call() {
return new HashSet<T>();
}
}, new Action2<Set<T>, T>() {
@Override
public void call(Set<T> set, T t) {
set.add(t);
}
});
}
};
}
示例2: bindUntilEvent
import rx.Observable.Transformer; //导入依赖的package包/类
public <T> Transformer<T, T> bindUntilEvent(@NonNull final PublishSubject<IContextLifecycle> lifecycleSubject,
@NonNull final IContextLifecycle event) {
return new Observable.Transformer<T, T>() {
@Override
public Observable<T> call(Observable<T> sourceObservable) {
Observable<IContextLifecycle> o =
lifecycleSubject.takeFirst(new Func1<IContextLifecycle, Boolean>() {
@Override
public Boolean call(IContextLifecycle contextLifecycle) {
return contextLifecycle.equals(event);
}
});
return sourceObservable.takeUntil(o);
}
};
}
示例3: collectStats
import rx.Observable.Transformer; //导入依赖的package包/类
public static <T, R extends Number> Transformer<T, Pair<T, Statistics>> collectStats(
final Func1<? super T, ? extends R> function) {
return new Transformer<T, Pair<T, Statistics>>() {
@Override
public Observable<Pair<T, Statistics>> call(Observable<T> source) {
return source.scan(Pair.create((T) null, Statistics.create()),
new Func2<Pair<T, Statistics>, T, Pair<T, Statistics>>() {
@Override
public Pair<T, Statistics> call(Pair<T, Statistics> pair, T t) {
return Pair.create(t, pair.b().add(function.call(t)));
}
}).skip(1);
}
};
}
示例4: toListWhile
import rx.Observable.Transformer; //导入依赖的package包/类
/**
* <p>
* Returns a {@link Transformer} that returns an {@link Observable} that is
* a buffering of the source Observable into lists of sequential items that
* satisfy the condition {@code condition}.
*
* <p>
* <img src=
* "https://github.com/davidmoten/rxjava-extras/blob/master/src/docs/toListWhile.png?raw=true"
* alt="marble diagram">
*
* @param condition
* condition function that must return true if an item is to be
* part of the list being prepared for emission
* @param <T>
* the generic type of the source Observable
* @return transformer as above
*/
public static <T> Transformer<T, List<T>> toListWhile(
final Func2<? super List<T>, ? super T, Boolean> condition) {
Func0<List<T>> initialState = new Func0<List<T>>() {
@Override
public List<T> call() {
return new ArrayList<T>();
}
};
Action2<List<T>, T> collect = new Action2<List<T>, T>() {
@Override
public void call(List<T> list, T n) {
list.add(n);
}
};
return collectWhile(initialState, collect, condition);
}
示例5: repeatLast
import rx.Observable.Transformer; //导入依赖的package包/类
public static final <T> Transformer<T, T> repeatLast() {
return new Transformer<T, T>() {
@Override
public Observable<T> call(Observable<T> o) {
return o.materialize().buffer(2, 1)
.flatMap(new Func1<List<Notification<T>>, Observable<T>>() {
@Override
public Observable<T> call(List<Notification<T>> list) {
Notification<T> a = list.get(0);
if (list.size() == 2 && list.get(1).isOnCompleted()) {
return Observable.just(a.getValue()).repeat();
} else if (a.isOnError()) {
return Observable.error(list.get(0).getThrowable());
} else if (a.isOnCompleted()) {
return Observable.empty();
} else {
return Observable.just(a.getValue());
}
}
});
}
};
}
示例6: removePairs
import rx.Observable.Transformer; //导入依赖的package包/类
private static Transformer<Integer, Integer> removePairs() {
Func1<Integer, Boolean> isCandidateForFirst = new Func1<Integer, Boolean>() {
@Override
public Boolean call(Integer t) {
return t == 1;
}
};
Func2<Integer, Integer, Boolean> remove = new Func2<Integer, Integer, Boolean>() {
@Override
public Boolean call(Integer t1, Integer t2) {
return t1 == 1 && t2 == 2;
}
};
return Transformers.removePairs(isCandidateForFirst, remove);
}
示例7: mainCompletes
import rx.Observable.Transformer; //导入依赖的package包/类
@Test
public void mainCompletes() {
Transformer<Integer, Integer> op = new TransformerOnTerminateResume<Integer>(new Func1<Throwable, Observable<Integer>>() {
@Override
public Observable<Integer> call(Throwable e) {
return Observable.just(11);
}
}, Observable.just(12));
TestSubscriber<Integer> ts = TestSubscriber.create();
Observable.range(1, 10)
.compose(op)
.subscribe(ts);
ts.assertValues(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12);
ts.assertNoErrors();
ts.assertCompleted();
}
示例8: mainErrors
import rx.Observable.Transformer; //导入依赖的package包/类
@Test
public void mainErrors() {
Transformer<Integer, Integer> op = new TransformerOnTerminateResume<Integer>(new Func1<Throwable, Observable<Integer>>() {
@Override
public Observable<Integer> call(Throwable e) {
return Observable.just(11);
}
}, Observable.just(12));
TestSubscriber<Integer> ts = TestSubscriber.create();
Observable.range(1, 10).concatWith(Observable.<Integer>error(new IOException()))
.compose(op)
.subscribe(ts);
ts.assertValues(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
ts.assertNoErrors();
ts.assertCompleted();
}
示例9: testStateTransitionThrowsError
import rx.Observable.Transformer; //导入依赖的package包/类
@Test
public void testStateTransitionThrowsError() {
final RuntimeException ex = new RuntimeException("boo");
Func0<Integer> initialState = Functions.constant0(1);
Func3<Integer, Integer, Observer<Integer>, Integer> transition = new Func3<Integer, Integer, Observer<Integer>, Integer>() {
@Override
public Integer call(Integer collection, Integer t, Observer<Integer> observer) {
throw ex;
}
};
Func2<Integer, Observer<Integer>, Boolean> completion = Functions.alwaysTrue2();
Transformer<Integer, Integer> transformer = Transformers.stateMachine(initialState,
transition, completion);
TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
Observable.just(1, 1, 1).compose(transformer).subscribe(ts);
ts.awaitTerminalEvent();
ts.assertError(ex);
}
示例10: testUnsubscriptionFromTransition
import rx.Observable.Transformer; //导入依赖的package包/类
@Test
public void testUnsubscriptionFromTransition() {
Func0<Integer> initialState = Functions.constant0(1);
Func3<Integer, Integer, Subscriber<Integer>, Integer> transition = new Func3<Integer, Integer, Subscriber<Integer>, Integer>() {
@Override
public Integer call(Integer collection, Integer t, Subscriber<Integer> subscriber) {
subscriber.onNext(123);
subscriber.unsubscribe();
return 1;
}
};
Func2<Integer, Observer<Integer>, Boolean> completion = Functions.alwaysTrue2();
Transformer<Integer, Integer> transformer = Transformers.stateMachine(initialState,
transition, completion);
TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
Observable.just(1, 1, 1).repeat().compose(transformer).subscribe(ts);
ts.assertValue(123);
ts.assertCompleted();
ts.assertUnsubscribed();
}
示例11: testForCompletionWithinStateMachine
import rx.Observable.Transformer; //导入依赖的package包/类
@Test
public void testForCompletionWithinStateMachine() {
Func0<Integer> initialState = Functions.constant0(1);
Func3<Integer, Integer, Subscriber<Integer>, Integer> transition = new Func3<Integer, Integer, Subscriber<Integer>, Integer>() {
@Override
public Integer call(Integer collection, Integer t, Subscriber<Integer> subscriber) {
subscriber.onNext(123);
// complete from within transition
subscriber.onCompleted();
return 1;
}
};
Func2<? super Integer, ? super Subscriber<Integer>, Boolean> completion = Functions
.alwaysTrue2();
Transformer<Integer, Integer> transformer = Transformers.stateMachine(initialState,
transition, completion);
TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
final AtomicInteger count = new AtomicInteger(0);
Observable.just(1, 2, 3).doOnNext(Actions.increment1(count)).compose(transformer)
.subscribe(ts);
ts.assertValues(123);
ts.assertCompleted();
assertEquals(1, count.get());
}
示例12: aggregateMultiLineNmea
import rx.Observable.Transformer; //导入依赖的package包/类
public static final Transformer<NmeaMessage, NmeaMessage> aggregateMultiLineNmea(
int bufferSize) {
return new Transformer<NmeaMessage, NmeaMessage>() {
@Override
public Observable<NmeaMessage> call(Observable<NmeaMessage> o) {
return Observable.defer(() -> {
AisNmeaBuffer buffer = new AisNmeaBuffer(bufferSize);
// use maxConcurrent so doesn't request unbounded
return o.flatMap(nmea -> {
return addToBuffer(buffer, nmea);
} , 1);
});
}
};
}
示例13: aggregateMultiLineNmeaWithLines
import rx.Observable.Transformer; //导入依赖的package包/类
public static final Transformer<NmeaMessage, NmeaMessage> aggregateMultiLineNmeaWithLines(
int bufferSize) {
return new Transformer<NmeaMessage, NmeaMessage>() {
@Override
public Observable<NmeaMessage> call(Observable<NmeaMessage> o) {
return Observable.defer(() -> {
AisNmeaBuffer buffer = new AisNmeaBuffer(bufferSize);
// use maxConcurrent so doesn't request unbounded
return o.flatMap(nmea -> {
return addToBuffer(buffer, nmea);
} , 1);
});
}
};
}
示例14: call
import rx.Observable.Transformer; //导入依赖的package包/类
@Override
public Observable<DriftCandidate> call(Observable<HasFix> o) {
Transformer<HasFix, DriftCandidate> t = Transformers.stateMachine()
.initialState(new State(options)) //
.transition(new Transition<State, HasFix, DriftCandidate>() {
@Override
public State call(State state, HasFix value,
Subscriber<DriftCandidate> subscriber) {
state.onNext(value, subscriber, isCandidate);
return state;
}
}) //
.build();
return o.compose(t);
}
示例15: handleResult
import rx.Observable.Transformer; //导入依赖的package包/类
/**
* 统一返回结果处理
*
* @param <T>
* @return
*/
public static <T> Transformer<CoreDataResponse<T>, T> handleResult() { //compose判断结果
return httpResponseObservable -> httpResponseObservable.flatMap(new Func1<CoreDataResponse<T>, Observable<T>>() {
@Override
public Observable<T> call(CoreDataResponse<T> tMyHttpResponse) {
if (tMyHttpResponse.getCode() == 200) {
return createData(tMyHttpResponse.getNewslist());
} else {
return Observable.error(new CoreApiException("服务器返回error"));
}
}
});
}