org.heigit.bigspatialdata.oshpbf.parser.rx.OshMerger Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of oshpbf-parser Show documentation
Show all versions of oshpbf-parser Show documentation
The PBF-Parser that transfers osh.pbf-files into database Objects.
package org.heigit.bigspatialdata.oshpbf.parser.rx;
import java.util.List;
import org.heigit.bigspatialdata.oshpbf.parser.osm.v0_6.Entity;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import io.reactivex.Flowable;
import io.reactivex.FlowableSubscriber;
import io.reactivex.internal.fuseable.ConditionalSubscriber;
import io.reactivex.internal.fuseable.QueueSubscription;
import io.reactivex.internal.subscriptions.SubscriptionHelper;
import io.reactivex.plugins.RxJavaPlugins;
public class OshMerger extends Flowable {
private final Flowable source;
public OshMerger(Flowable source){
this.source = source;
}
@Override
protected void subscribeActual(Subscriber super Osh> actual) {
if (actual instanceof ConditionalSubscriber) {
//System.out.println("s instanceof ConditionalSubscriber");
}
source.subscribe(new OshMergerSubscriber(actual));
}
private static final class OshMergerSubscriber implements FlowableSubscriber, Subscription {
/** The downstream subscriber. */
private final Subscriber super Osh> actual;
/** The upstream subscription. */
private Subscription s;
/** Flag indicating no further onXXX event should be accepted. */
private boolean done;
private Osh oshToMerge = null;
private OshMergerSubscriber(Subscriber super Osh> actual) {
this.actual = actual;
}
@Override
public final void onSubscribe(Subscription s) {
if (SubscriptionHelper.validate(this.s, s)) {
this.s = s;
if (s instanceof QueueSubscription) {
System.out.println("s instanceof QueueSubscription");
}
actual.onSubscribe(this);
}
}
@Override
public void onNext(Osh osh) {
if(osh.isComplete){
if(oshToMerge != null)
actual.onNext(oshToMerge);
actual.onNext(osh);
oshToMerge = null;
return;
}
if(oshToMerge != null){
if(oshToMerge.getId() == osh.getId()){
actual.onNext(merge(oshToMerge,osh));
oshToMerge = null;
return;
}
actual.onNext(oshToMerge);
oshToMerge = osh;
}
if(oshToMerge == null){
oshToMerge = osh;
s.request(1);
}
}
private static Osh merge(Osh a, Osh b){
final List versions = a.versions;
versions.addAll(b.versions);
final long[] pos = new long[a.pos.length+b.pos.length];
for(int i=0; i