@@ -140,4 +140,61 @@ public void onNext(T v) {
140140 public boolean hasObservers () {
141141 return state .observers ().length > 0 ;
142142 }
143+ /**
144+ * Check if the Subject has a value.
145+ * <p>Use the {@link #getValue()} method to retrieve such a value.
146+ * <p>Note that unless {@link #hasCompleted()} or {@link #hasThrowable()} returns true, the value
147+ * retrieved by {@code getValue()} may get outdated.
148+ * @return true if and only if the subject has some value but not an error
149+ */
150+ public boolean hasValue () {
151+ Object v = lastValue ;
152+ Object o = state .get ();
153+ return !nl .isError (o ) && nl .isNext (v );
154+ }
155+ /**
156+ * Check if the Subject has terminated with an exception.
157+ * @return true if the subject has received a throwable through {@code onError}.
158+ */
159+ public boolean hasThrowable () {
160+ Object o = state .get ();
161+ return nl .isError (o );
162+ }
163+ /**
164+ * Check if the Subject has terminated normally.
165+ * @return true if the subject completed normally via {@code onCompleted()}
166+ */
167+ public boolean hasCompleted () {
168+ Object o = state .get ();
169+ return o != null && !nl .isError (o );
170+ }
171+ /**
172+ * Returns the current value of the Subject if there is such a value and
173+ * the subject hasn't terminated with an exception.
174+ * <p>The can return {@code null} for various reasons. Use {@link #hasValue()}, {@link #hasThrowable()}
175+ * and {@link #hasCompleted()} to determine if such {@code null} is a valid value, there was an
176+ * exception or the Subject terminated without receiving any value.
177+ * @return the current value or {@code null} if the Subject doesn't have a value,
178+ * has terminated with an exception or has an actual {@code null} as a value.
179+ */
180+ public T getValue () {
181+ Object v = lastValue ;
182+ Object o = state .get ();
183+ if (!nl .isError (o ) && nl .isNext (v )) {
184+ return nl .getValue (v );
185+ }
186+ return null ;
187+ }
188+ /**
189+ * Returns the Throwable that terminated the Subject.
190+ * @return the Throwable that terminated the Subject or {@code null} if the
191+ * subject hasn't terminated yet or it terminated normally.
192+ */
193+ public Throwable getThrowable () {
194+ Object o = state .get ();
195+ if (nl .isError (o )) {
196+ return nl .getError (o );
197+ }
198+ return null ;
199+ }
143200}
0 commit comments