All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.heigit.bigspatialdata.oshpbf.parser.rx.OshMerger Maven / Gradle / Ivy

There is a newer version: 0.5.11
Show newest version
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 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 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 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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy