@@ -767,6 +767,11 @@ final void removeFirst() {
767
767
}
768
768
769
769
setFirst (head );
770
+ // correct the tail if all items have been removed
771
+ head = get ();
772
+ if (head .get () == null ) {
773
+ tail = head ;
774
+ }
770
775
}
771
776
/**
772
777
* Arranges the given node is the new head from now on.
@@ -775,35 +780,31 @@ final void removeFirst() {
775
780
final void setFirst (Node n ) {
776
781
if (eagerTruncate ) {
777
782
Node m = new Node (null , n .index );
778
- Node nextNode = n .get ();
779
- if (nextNode == null ) {
780
- tail = m ;
781
- }
782
- m .lazySet (nextNode );
783
+ m .lazySet (n .get ());
783
784
n = m ;
784
785
}
785
786
set (n );
786
787
}
787
788
788
789
@ Override
789
790
public final void next (T value ) {
790
- Object o = enterTransform (NotificationLite .next (value ));
791
+ Object o = enterTransform (NotificationLite .next (value ), false );
791
792
Node n = new Node (o , ++index );
792
793
addLast (n );
793
794
truncate ();
794
795
}
795
796
796
797
@ Override
797
798
public final void error (Throwable e ) {
798
- Object o = enterTransform (NotificationLite .error (e ));
799
+ Object o = enterTransform (NotificationLite .error (e ), true );
799
800
Node n = new Node (o , ++index );
800
801
addLast (n );
801
802
truncateFinal ();
802
803
}
803
804
804
805
@ Override
805
806
public final void complete () {
806
- Object o = enterTransform (NotificationLite .complete ());
807
+ Object o = enterTransform (NotificationLite .complete (), true );
807
808
Node n = new Node (o , ++index );
808
809
addLast (n );
809
810
truncateFinal ();
@@ -897,9 +898,10 @@ public final void replay(InnerSubscription<T> output) {
897
898
* Override this to wrap the NotificationLite object into a
898
899
* container to be used later by truncate.
899
900
* @param value the value to transform into the internal representation
901
+ * @param terminal is this a terminal value?
900
902
* @return the transformed value
901
903
*/
902
- Object enterTransform (Object value ) {
904
+ Object enterTransform (Object value , boolean terminal ) {
903
905
return value ;
904
906
}
905
907
/**
@@ -1001,8 +1003,8 @@ static final class SizeAndTimeBoundReplayBuffer<T> extends BoundedReplayBuffer<T
1001
1003
}
1002
1004
1003
1005
@ Override
1004
- Object enterTransform (Object value ) {
1005
- return new Timed <Object >(value , scheduler .now (unit ), unit );
1006
+ Object enterTransform (Object value , boolean terminal ) {
1007
+ return new Timed <Object >(value , terminal ? Long . MAX_VALUE : scheduler .now (unit ), unit );
1006
1008
}
1007
1009
1008
1010
@ Override
@@ -1019,7 +1021,7 @@ void truncate() {
1019
1021
1020
1022
int e = 0 ;
1021
1023
for (;;) {
1022
- if (next != null ) {
1024
+ if (next != null && size > 1 ) { // never truncate the very last item just added
1023
1025
if (size > limit ) {
1024
1026
e ++;
1025
1027
size --;
0 commit comments