@Override @SuppressWarnings("unchecked") public T[] getValues(T[] array) { Node<Object> h = head; int s = size(); if (s == 0) { if (array.length != 0) { array[0] = null; } } else { if (array.length < s) { array = (T[])Array.newInstance(array.getClass().getComponentType(), s); } int i = 0; while (i != s) { Node<Object> next = h.get(); array[i] = (T)next.value; i++; h = next; } if (array.length > s) { array[s] = null; } } return array; }
/** * Creates a size-bounded replay subject. * <p> * In this setting, the {@code ReplaySubject} holds at most {@code size} items in its internal buffer and * discards the oldest item. * <p> * When observers subscribe to a terminated {@code ReplaySubject}, they are guaranteed to see at most * {@code size} {@code onNext} events followed by a termination event. * <p> * If an observer subscribes while the {@code ReplaySubject} is active, it will observe all items in the * buffer at that point in time and each item observed afterwards, even if the buffer evicts items due to * the size constraint in the mean time. In other words, once an Observer subscribes, it will receive items * without gaps in the sequence. * * @param <T> * the type of items observed and emitted by the Subject * @param maxSize * the maximum number of buffered items * @return the created subject */ @CheckReturnValue @NonNull public static <T> ReplaySubject<T> createWithSize(int maxSize) { return new ReplaySubject<T>(new SizeBoundReplayBuffer<T>(maxSize)); }
/** * Creates an unbounded replay subject with the bounded-implementation for testing purposes. * <p> * This variant behaves like the regular unbounded {@code ReplaySubject} created via {@link #create()} but * uses the structures of the bounded-implementation. This is by no means intended for the replacement of * the original, array-backed and unbounded {@code ReplaySubject} due to the additional overhead of the * linked-list based internal buffer. The sole purpose is to allow testing and reasoning about the behavior * of the bounded implementations without the interference of the eviction policies. * * @param <T> * the type of items observed and emitted by the Subject * @return the created subject */ /* test */ static <T> ReplaySubject<T> createUnbounded() { return new ReplaySubject<T>(new SizeBoundReplayBuffer<T>(Integer.MAX_VALUE)); }
@Override @SuppressWarnings("unchecked") public T[] getValues(T[] array) { Node<Object> h = head; int s = size(); if (s == 0) { if (array.length != 0) { array[0] = null; } } else { if (array.length < s) { array = (T[])Array.newInstance(array.getClass().getComponentType(), s); } int i = 0; while (i != s) { Node<Object> next = h.get(); array[i] = (T)next.value; i++; h = next; } if (array.length > s) { array[s] = null; } } return array; }
@Override public void addFinal(Object notificationLite) { Node<Object> n = new Node<Object>(notificationLite); Node<Object> t = tail; tail = n; size++; t.lazySet(n); // releases both the tail and size trimHead(); done = true; }
/** * Creates a size-bounded replay subject. * <p> * In this setting, the {@code ReplaySubject} holds at most {@code size} items in its internal buffer and * discards the oldest item. * <p> * When observers subscribe to a terminated {@code ReplaySubject}, they are guaranteed to see at most * {@code size} {@code onNext} events followed by a termination event. * <p> * If an observer subscribes while the {@code ReplaySubject} is active, it will observe all items in the * buffer at that point in time and each item observed afterwards, even if the buffer evicts items due to * the size constraint in the mean time. In other words, once an Observer subscribes, it will receive items * without gaps in the sequence. * * @param <T> * the type of items observed and emitted by the Subject * @param maxSize * the maximum number of buffered items * @return the created subject */ @CheckReturnValue @NonNull public static <T> ReplaySubject<T> createWithSize(int maxSize) { return new ReplaySubject<T>(new SizeBoundReplayBuffer<T>(maxSize)); }
@Override public void add(T value) { Node<Object> n = new Node<Object>(value); Node<Object> t = tail; tail = n; size++; t.set(n); // releases both the tail and size trim(); }
/** * Creates an unbounded replay subject with the bounded-implementation for testing purposes. * <p> * This variant behaves like the regular unbounded {@code ReplaySubject} created via {@link #create()} but * uses the structures of the bounded-implementation. This is by no means intended for the replacement of * the original, array-backed and unbounded {@code ReplaySubject} due to the additional overhead of the * linked-list based internal buffer. The sole purpose is to allow testing and reasoning about the behavior * of the bounded implementations without the interference of the eviction policies. * * @param <T> * the type of items observed and emitted by the Subject * @return the created subject */ /* test */ static <T> ReplaySubject<T> createUnbounded() { return new ReplaySubject<T>(new SizeBoundReplayBuffer<T>(Integer.MAX_VALUE)); }
@Override public void add(T value) { Node<Object> n = new Node<Object>(value); Node<Object> t = tail; tail = n; size++; t.set(n); // releases both the tail and size trim(); }
@Override public void addFinal(Object notificationLite) { Node<Object> n = new Node<Object>(notificationLite); Node<Object> t = tail; tail = n; size++; t.lazySet(n); // releases both the tail and size trimHead(); done = true; }