Netflix/RxJava — pull request #520 by zsxwing (2013-11-25 03:28:23+00:00)
Title: Fixed the blocking/non-blocking first

Hi, this PR fixed the last problem in #423.

* `Observable.first` in RxJava is same as `Observable.FirstAsync` in Rx.Net, which is a non-blocking operator. When the source sequence is empty, it will emit an `IllegalArgumentException`.
* `Observable.takeFirst` is an alias of `take(1)`. If the source sequence is empty, it will return an empty sequence.
* `BlockingObservable.first` in RxJava is same as `Observable.First` in Rx.Net, which is a blocking operator. When the source sequence is empty, it will emit an `IllegalArgumentException`.
* also added `BlockingObservable.firstOrDefault`.

Here I attached my test codes for Rx.Net.
```c#
Console.WriteLine("===Test FirstAsync");
{
IObservable obs = Observable.Range(1, 4).FirstAsync();
obs.Subscribe(
x => Console.WriteLine("FirstAsync: " + x),
e => Console.WriteLine("FirstAsync: " + e),
() => Console.WriteLine("FirstAsync completed")
);
}
{
IObservable obs = Observable.Empty().FirstAsync();
obs.Subscribe(
x => Console.WriteLine("FirstAsync with empty: " + x),
e => Console.WriteLine("FirstAsync with empty: " + e),
() => Console.WriteLine("FirstAsync with empty completed")
);
}
Console.WriteLine("===Test Take(1)");
{
IObservable obs = Observable.Range(1, 4).Take(1);
obs.Subscribe(
x => Console.WriteLine("Take(1): " + x),
e => Console.WriteLine("Take(1): " + e),
() => Console.WriteLine("Take(1) completed")
);
}
{
IObservable obs = Observable.Empty().Take(1);
obs.Subscribe(
x => Console.WriteLine("Take(1) with empty: " + x),
e => Console.WriteLine("Take(1) with empty: " + e),
() => Console.WriteLine("Take(1) with empty completed")
);
}
Console.WriteLine("===Test First(1)");
{
int value = Observable.Range(1, 4).First();
Console.WriteLine("First: " + value);
}
{
try
{
int value = Observable.Empty().First();
}
catch(Exception e) {
Console.WriteLine("First with empty: " + e);
}
}
```
The output is:

===Test FirstAsync
FirstAsync: 1
FirstAsync completed
FirstAsync with empty: System.InvalidOperationException: Sequence contains no el
ements.
===Test Take(1)
Take(1): 1
Take(1) completed
Take(1) with empty completed
===Test First(1)
First: 1
First with empty: System.InvalidOperationException: Sequence contains no element
s.
at System.Reactive.Linq.QueryLanguage.FirstOrDefaultInternal[TSource](IObserv
able`1 source, Boolean throwOnEmpty)
at System.Reactive.Linq.QueryLanguage.First[TSource](IObservable`1 source)
at System.Reactive.Linq.Observable.First[TSource](IObservable`1 source)
at ConsoleApplication1.Program.Main(String[] args) in C:\Users\szhu\documents
\visual studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:
line 190