@@ -1949,36 +1949,64 @@ trait Observable[+T]
1949
1949
def headOrElse [U >: T ](default : => U ): Observable [U ] = firstOrElse(default)
1950
1950
1951
1951
/**
1952
- * Returns an Observable that emits only the very first item emitted by the source Observable.
1953
- * This is just a shorthand for `take(1)` .
1954
- *
1952
+ * Returns an Observable that emits only the very first item emitted by the source Observable, or raises an
1953
+ * `IllegalArgumentException` if the source Observable is empty .
1954
+ * <p>
1955
1955
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/first.png">
1956
- *
1957
- * @return an Observable that emits only the very first item from the source, or none if the
1958
- * source Observable completes without emitting a single item.
1956
+ *
1957
+ * @return an Observable that emits only the very first item emitted by the source Observable, or raises an
1958
+ * `IllegalArgumentException` if the source Observable is empty
1959
+ * @see <a href="https://github.com/Netflix/RxJava/wiki/Filtering-Observables#wiki-first">RxJava Wiki: first()</a>
1960
+ * @see "MSDN: Observable.firstAsync()"
1959
1961
*/
1960
- def first : Observable [T ] = take(1 )
1962
+ def first : Observable [T ] = {
1963
+ toScalaObservable[T ](asJavaObservable.first)
1964
+ }
1961
1965
1962
- /*
1963
-
1964
- TODO once https://github.com/Netflix/RxJava/issues/417 is fixed, we can add head and tail methods
1966
+ /**
1967
+ * Returns an Observable that emits only the very first item emitted by the source Observable, or raises an
1968
+ * `IllegalArgumentException` if the source Observable is empty.
1969
+ * <p>
1970
+ * <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/first.png">
1971
+ *
1972
+ * @return an Observable that emits only the very first item emitted by the source Observable, or raises an
1973
+ * `IllegalArgumentException` if the source Observable is empty
1974
+ * @see <a href="https://github.com/Netflix/RxJava/wiki/Filtering-Observables#wiki-first">RxJava Wiki: first()</a>
1975
+ * @see "MSDN: Observable.firstAsync()"
1976
+ * @see [[Observable.first ]]
1977
+ */
1978
+ def head : Observable [T ] = first
1965
1979
1966
1980
/**
1967
- * emits NoSuchElementException("head of empty Observable") if empty
1981
+ * Returns an Observable that emits the last item emitted by the source Observable or notifies observers of
1982
+ * an `IllegalArgumentException` if the source Observable is empty.
1983
+ *
1984
+ * <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/last.png">
1985
+ *
1986
+ * @return an Observable that emits the last item from the source Observable or notifies observers of an
1987
+ * error
1988
+ * @see <a href="https://github.com/Netflix/RxJava/wiki/Filtering-Observable-Operators#wiki-last">RxJava Wiki: last()</a>
1989
+ * @see "MSDN: Observable.lastAsync()"
1968
1990
*/
1969
- def head: Observable[T] = {
1970
- this.take(1).fold[Option[T]](None)((v: Option[T], e: T) => Some(e)).map({
1971
- case Some(element) => element
1972
- case None => throw new NoSuchElementException("head of empty Observable")
1973
- })
1991
+ def last : Observable [T ] = {
1992
+ toScalaObservable[T ](asJavaObservable.last)
1974
1993
}
1975
-
1994
+
1976
1995
/**
1977
- * emits an UnsupportedOperationException("tail of empty list") if empty
1996
+ * If the source Observable completes after emitting a single item, return an Observable that emits that
1997
+ * item. If the source Observable emits more than one item or no items, throw an `IllegalArgumentException`.
1998
+ *
1999
+ * <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/single.png">
2000
+ *
2001
+ * @return an Observable that emits the single item emitted by the source Observable
2002
+ * @throws IllegalArgumentException
2003
+ * if the source emits more than one item or no items
2004
+ * @see <a href="https://github.com/Netflix/RxJava/wiki/Observable-Utility-Operators#wiki-single-and-singleordefault">RxJava Wiki: single()</a>
2005
+ * @see "MSDN: Observable.singleAsync()"
1978
2006
*/
1979
- def tail : Observable[T] = ???
1980
-
1981
- */
2007
+ def single : Observable [T ] = {
2008
+ toScalaObservable[ T ](asJavaObservable.single)
2009
+ }
1982
2010
1983
2011
/**
1984
2012
* Returns an Observable that forwards all sequentially distinct items emitted from the source Observable.
0 commit comments