@@ -550,12 +550,30 @@ public T[] toArray(T[] a) {
550550 for (int i = 0 ; i < s ; i ++) {
551551 a [i ] = (T )list .get (i );
552552 }
553- if (s < a .length - 1 ) {
553+ if (a .length > s ) {
554554 a [s ] = null ;
555555 }
556+ } else
557+ if (a .length > 0 ) {
558+ a [0 ] = null ;
556559 }
557560 return a ;
558561 }
562+ @ Override
563+ public T latest () {
564+ int idx = index ;
565+ if (idx > 0 ) {
566+ Object o = list .get (idx - 1 );
567+ if (nl .isCompleted (o ) || nl .isError (o )) {
568+ if (idx > 1 ) {
569+ return nl .getValue (list .get (idx - 2 ));
570+ }
571+ return null ;
572+ }
573+ return nl .getValue (o );
574+ }
575+ return null ;
576+ }
559577 }
560578
561579
@@ -715,6 +733,27 @@ public T[] toArray(T[] a) {
715733 }
716734 return list .toArray (a );
717735 }
736+ @ Override
737+ public T latest () {
738+ Node <Object > h = head ().next ;
739+ if (h == null ) {
740+ return null ;
741+ }
742+ Node <Object > p = null ;
743+ while (h != tail ()) {
744+ p = h ;
745+ h = h .next ;
746+ }
747+ Object value = leaveTransform .call (h .value );
748+ if (nl .isError (value ) || nl .isCompleted (value )) {
749+ if (p != null ) {
750+ value = leaveTransform .call (p .value );
751+ return nl .getValue (value );
752+ }
753+ return null ;
754+ }
755+ return nl .getValue (value );
756+ }
718757 }
719758
720759 // **************
@@ -781,6 +820,12 @@ I replayObserverFromIndexTest(
781820 * @return the array or a new array containing the current values
782821 */
783822 T [] toArray (T [] a );
823+ /**
824+ * Returns the latest value that has been buffered or null if no such value
825+ * present.
826+ * @return the latest value buffered or null if none
827+ */
828+ T latest ();
784829 }
785830
786831 /** Interface to manage eviction checking. */
@@ -1054,6 +1099,7 @@ public void evictFinal(NodeList<Object> list) {
10541099 * @return true if the subject has received a throwable through {@code onError}.
10551100 */
10561101 @ Experimental
1102+ @ Override
10571103 public boolean hasThrowable () {
10581104 NotificationLite <T > nl = ssm .nl ;
10591105 Object o = ssm .get ();
@@ -1064,6 +1110,7 @@ public boolean hasThrowable() {
10641110 * @return true if the subject completed normally via {@code onCompleted}
10651111 */
10661112 @ Experimental
1113+ @ Override
10671114 public boolean hasCompleted () {
10681115 NotificationLite <T > nl = ssm .nl ;
10691116 Object o = ssm .get ();
@@ -1075,6 +1122,7 @@ public boolean hasCompleted() {
10751122 * subject hasn't terminated yet or it terminated normally.
10761123 */
10771124 @ Experimental
1125+ @ Override
10781126 public Throwable getThrowable () {
10791127 NotificationLite <T > nl = ssm .nl ;
10801128 Object o = ssm .get ();
@@ -1098,15 +1146,10 @@ public int size() {
10981146 public boolean hasAnyValue () {
10991147 return !state .isEmpty ();
11001148 }
1101- /** An empty array to trigger getValues() to return a new array. */
1102- private static final Object [] EMPTY_ARRAY = new Object [0 ];
1103- /**
1104- * @return returns a snapshot of the currently buffered non-terminal events.
1105- */
1106- @ SuppressWarnings ("unchecked" )
11071149 @ Experimental
1108- public Object [] getValues () {
1109- return state .toArray ((T [])EMPTY_ARRAY );
1150+ @ Override
1151+ public boolean hasValue () {
1152+ return hasAnyValue ();
11101153 }
11111154 /**
11121155 * Returns a snapshot of the currently buffered non-terminal events into
@@ -1115,7 +1158,12 @@ public Object[] getValues() {
11151158 * @return the array {@code a} if it had enough capacity or a new array containing the available values
11161159 */
11171160 @ Experimental
1161+ @ Override
11181162 public T [] getValues (T [] a ) {
11191163 return state .toArray (a );
11201164 }
1165+ @ Override
1166+ public T getValue () {
1167+ return state .latest ();
1168+ }
11211169}
0 commit comments