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

com.activeandroid.sqlbrite.BackpressureBufferLastOperator Maven / Gradle / Ivy

There is a newer version: 3.1.5
Show newest version
/*
 * Copyright (C) 2015 Square, Inc.
 *
 * 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 com.activeandroid.sqlbrite;

import rx.Observable.Operator;
import rx.Producer;
import rx.Subscriber;

/** An operator which keeps the last emitted instance when backpressure has been applied. */
final class BackpressureBufferLastOperator implements Operator {
  static final Operator instance = new BackpressureBufferLastOperator<>();

  static  Operator instance() {
    //noinspection unchecked
    return (Operator) instance;
  }

  private BackpressureBufferLastOperator() {
  }

  @Override public Subscriber call(final Subscriber child) {
    BufferLastSubscriber parent = new BufferLastSubscriber<>(child);
    child.add(parent);
    child.setProducer(parent.producer);
    return parent;
  }

  static final class BufferLastSubscriber extends Subscriber {
    private static final Object NONE = new Object();

    private final Subscriber child;

    private Object last = NONE; // Guarded by 'this'.
    private long requested; // Guarded by 'this'. Starts at zero.

    final Producer producer = new Producer() {
      @Override public void request(long n) {
        if (n < 0) {
          throw new IllegalArgumentException("requested " + n + " < 0");
        }
        if (n == 0) {
          return;
        }

        Object candidate;
        synchronized (BufferLastSubscriber.this) {
          candidate = last;

          long currentRequested = requested;
          if (Long.MAX_VALUE - n <= currentRequested) {
            requested = Long.MAX_VALUE;
          } else {
            if (candidate != NONE) {
              n--; // Decrement since we will be emitting a value.
            }
            requested = currentRequested + n;
          }
        }

        // Only emit if the value is not the explicit NONE marker.
        if (candidate != NONE) {
          //noinspection unchecked
          child.onNext((T) candidate);
        }
      }
    };

    public BufferLastSubscriber(Subscriber child) {
      this.child = child;
    }

    @Override public void onNext(T t) {
      boolean emit = false;
      synchronized (this) {
        long currentRequested = requested;
        if (currentRequested == Long.MAX_VALUE) {
          // No need to decrement when the firehose is open.
          emit = true;
        } else if (currentRequested > 0) {
          requested = currentRequested - 1;
          emit = true;
        } else {
          last = t; // Not emitting, store for later.
        }
      }

      if (emit) {
        child.onNext(t);
      }
    }

    @Override public void onStart() {
      request(Long.MAX_VALUE);
    }

    @Override public void onCompleted() {
      child.onCompleted();
    }

    @Override public void onError(Throwable e) {
      child.onError(e);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy