Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright 2015 David Karnok and Netflix, 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 hu.akarnokd.rxjava2.subjects.nbp;
import java.lang.reflect.Array;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.*;
import hu.akarnokd.rxjava2.Scheduler;
import hu.akarnokd.rxjava2.disposables.Disposable;
import hu.akarnokd.rxjava2.internal.functions.Objects;
import hu.akarnokd.rxjava2.internal.util.NotificationLite;
import hu.akarnokd.rxjava2.plugins.RxJavaPlugins;
import hu.akarnokd.rxjava2.schedulers.Schedulers;
/**
* Replays events to Subscribers.
*
*
This Subject respects the backpressure behavior of its Subscribers (individually).
*
* @param the value type
*/
public final class NbpReplaySubject extends NbpSubject {
public static NbpReplaySubject create() {
return create(16);
}
public static NbpReplaySubject create(int capacityHint) {
if (capacityHint <= 0) {
throw new IllegalArgumentException("capacityHint > 0 required but it was " + capacityHint);
}
ReplayBuffer buffer = new UnboundedReplayBuffer(capacityHint);
return createWithBuffer(buffer);
}
public static NbpReplaySubject createWithSize(int size) {
if (size <= 0) {
throw new IllegalArgumentException("size > 0 required but it was " + size);
}
SizeBoundReplayBuffer buffer = new SizeBoundReplayBuffer(size);
return createWithBuffer(buffer);
}
/* test */ static NbpReplaySubject createUnbounded() {
SizeBoundReplayBuffer buffer = new SizeBoundReplayBuffer(Integer.MAX_VALUE);
return createWithBuffer(buffer);
}
public static NbpReplaySubject createWithTime(long maxAge, TimeUnit unit) {
return createWithTime(maxAge, unit, Schedulers.trampoline());
}
public static NbpReplaySubject createWithTime(long maxAge, TimeUnit unit, Scheduler scheduler) {
return createWithTimeAndSize(maxAge, unit, scheduler, Integer.MAX_VALUE);
}
public static NbpReplaySubject createWithTimeAndSize(long maxAge, TimeUnit unit, Scheduler scheduler, int size) {
Objects.requireNonNull(unit, "unit is null");
Objects.requireNonNull(scheduler, "scheduler is null");
if (size <= 0) {
throw new IllegalArgumentException("size > 0 required but it was " + size);
}
SizeAndTimeBoundReplayBuffer buffer = new SizeAndTimeBoundReplayBuffer(size, maxAge, unit, scheduler);
return createWithBuffer(buffer);
}
static NbpReplaySubject createWithBuffer(ReplayBuffer buffer) {
State state = new State(buffer);
return new NbpReplaySubject(state);
}
final State state;
protected NbpReplaySubject(State state) {
super(state);
this.state = state;
}
@Override
public void onSubscribe(Disposable s) {
state.onSubscribe(s);
}
@Override
public void onNext(T t) {
if (t == null) {
onError(new NullPointerException());
return;
}
state.onNext(t);
}
@Override
public void onError(Throwable t) {
if (t == null) {
t = new NullPointerException();
}
state.onError(t);
}
@Override
public void onComplete() {
state.onComplete();
}
@Override
public boolean hasSubscribers() {
return state.subscribers.length != 0;
}
/* test */ int subscriberCount() {
return state.subscribers.length;
}
@Override
public Throwable getThrowable() {
Object o = state.get();
if (NotificationLite.isError(o)) {
return NotificationLite.getError(o);
}
return null;
}
@Override
public T getValue() {
return state.buffer.getValue();
}
@Override
public T[] getValues(T[] array) {
return state.buffer.getValues(array);
}
@Override
public boolean hasComplete() {
Object o = state.get();
return NotificationLite.isComplete(o);
}
@Override
public boolean hasThrowable() {
Object o = state.get();
return NotificationLite.isError(o);
}
@Override
public boolean hasValue() {
return state.buffer.size() != 0;
}
/* test*/ int size() {
return state.buffer.size();
}
static final class State extends AtomicReference