org.redisson.reactive.PublisherAdder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of redisson Show documentation
Show all versions of redisson Show documentation
Redis based In-Memory Data Grid for Java
/**
* Copyright 2018 Nikita Koksharov
*
* 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 org.redisson.reactive;
import java.util.concurrent.atomic.AtomicLong;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscription;
import org.redisson.api.RCollectionReactive;
import reactor.rx.Promise;
import reactor.rx.Promises;
import reactor.rx.action.support.DefaultSubscriber;
/**
*
* @author Nikita Koksharov
*
* @param value type
*/
public class PublisherAdder {
private final RCollectionReactive destination;
public PublisherAdder(RCollectionReactive destination) {
this.destination = destination;
}
public Integer sum(Integer first, Integer second) {
return first + second;
}
public Publisher addAll(Publisher c) {
final Promise promise = Promises.prepare();
c.subscribe(new DefaultSubscriber() {
volatile boolean completed;
AtomicLong values = new AtomicLong();
Subscription s;
Integer lastSize = 0;
@Override
public void onSubscribe(Subscription s) {
this.s = s;
s.request(1);
}
@Override
public void onNext(V o) {
values.getAndIncrement();
destination.add(o).subscribe(new DefaultSubscriber() {
@Override
public void onSubscribe(Subscription s) {
s.request(1);
}
@Override
public void onError(Throwable t) {
promise.onError(t);
}
@Override
public void onNext(Integer o) {
lastSize = sum(lastSize, o);
s.request(1);
if (values.decrementAndGet() == 0 && completed) {
promise.onNext(lastSize);
}
}
});
}
@Override
public void onComplete() {
completed = true;
if (values.get() == 0) {
promise.onNext(lastSize);
}
}
});
return promise;
}
}