Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,18 @@ def class ObservableTests {
}
}

@Test
public void testLastOrDefault() {
def val = Observable.toObservable("one", "two").lastOrDefault("default", { x -> x.length() == 3})
assertEquals("two", val)
}

@Test
public void testLastOrDefault2() {
def val = Observable.toObservable("one", "two").lastOrDefault("default", { x -> x.length() > 3})
assertEquals("default", val)
}

def class AsyncObservable implements Func1<Observer<Integer>, Subscription> {

public Subscription call(final Observer<Integer> observer) {
Expand Down
136 changes: 136 additions & 0 deletions rxjava-core/src/main/java/rx/Observable.java
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,61 @@ public static <T> Observable<T> last(final Observable<T> that) {
return _create(OperationLast.last(that));
}

/**
* Returns the last element of an observable sequence, or a default value if no value is found.
* @param source the source observable.
* @param defaultValue a default value that would be returned if observable is empty.
* @param <T> the type of source.
* @return the last element of an observable sequence that matches the predicate, or a default value if no value is found.
*/
public static <T> T lastOrDefault(Observable<T> source, T defaultValue) {
boolean found = false;
T result = null;

for (T value : source.toIterable()) {
found = true;
result = value;
}

if (!found) {
return defaultValue;
}

return result;
}

/**
* Returns the last element of an observable sequence that matches the predicate, or a default value if no value is found.
* @param source the source observable.
* @param defaultValue a default value that would be returned if observable is empty.
* @param predicate a predicate function to evaluate for elements in the sequence.
* @param <T> the type of source.
* @return the last element of an observable sequence that matches the predicate, or a default value if no value is found.
*/
public static <T> T lastOrDefault(Observable<T> source, T defaultValue, Func1<T, Boolean> predicate) {
return lastOrDefault(source.filter(predicate), defaultValue);
}

/**
* Returns the last element of an observable sequence that matches the predicate, or a default value if no value is found.
* @param source the source observable.
* @param defaultValue a default value that would be returned if observable is empty.
* @param predicate a predicate function to evaluate for elements in the sequence.
* @param <T> the type of source.
* @return the last element of an observable sequence that matches the predicate, or a default value if no value is found.
*/
public static <T> T lastOrDefault(Observable<T> source, T defaultValue, Object predicate){
@SuppressWarnings("rawtypes")
final FuncN _f = Functions.from(predicate);

return lastOrDefault(source, defaultValue, new Func1<T, Boolean>() {
@Override
public Boolean call(T args) {
return (Boolean) _f.call(args);
}
});
}

/**
* Applies a function of your choosing to every notification emitted by an Observable, and returns
* this transformation as a new Observable sequence.
Expand Down Expand Up @@ -2030,6 +2085,38 @@ public Observable<T> last() {
return last(this);
}

/**
* Returns the last element, or a default value if no value is found.
*
* @param defaultValue a default value that would be returned if observable is empty.
* @return the last element of an observable sequence that matches the predicate, or a default value if no value is found.
*/
public T lastOrDefault(T defaultValue) {
return lastOrDefault(this, defaultValue);
}

/**
* Returns the last element that matches the predicate, or a default value if no value is found.
*
* @param defaultValue a default value that would be returned if observable is empty.
* @param predicate a predicate function to evaluate for elements in the sequence.
* @return the last element of an observable sequence that matches the predicate, or a default value if no value is found.
*/
public T lastOrDefault(T defaultValue, Func1<T, Boolean> predicate) {
return lastOrDefault(this, defaultValue, predicate);
}

/**
* Returns the last element that matches the predicate, or a default value if no value is found.
*
* @param defaultValue a default value that would be returned if observable is empty.
* @param predicate a predicate function to evaluate for elements in the sequence.
* @return the last element of an observable sequence that matches the predicate, or a default value if no value is found.
*/
public T lastOrDefault(T defaultValue, Object predicate) {
return lastOrDefault(this, defaultValue, predicate);
}

/**
* Applies a function of your choosing to every item emitted by an Observable, and returns this
* transformation as a new Observable sequence.
Expand Down Expand Up @@ -2743,6 +2830,55 @@ public Subscription call(Observer<String> observer) {

}

@Test
public void testLastOrDefault1() {
Observable<String> observable = toObservable("one", "two", "three");
assertEquals("three", observable.lastOrDefault("default"));
}

@Test
public void testLastOrDefault2() {
Observable<String> observable = toObservable();
assertEquals("default", observable.lastOrDefault("default"));
}

@Test
public void testLastOrDefault() {
Observable<Integer> observable = toObservable(1, 0, -1);
int last = observable.lastOrDefault(-100, new Func1<Integer, Boolean>() {
@Override
public Boolean call(Integer args) {
return args >= 0;
}
});
assertEquals(0, last);
}

@Test
public void testLastOrDefaultWrongPredicate() {
Observable<Integer> observable = toObservable(-1, -2, -3);
int last = observable.lastOrDefault(0, new Func1<Integer, Boolean>() {
@Override
public Boolean call(Integer args) {
return args >= 0;
}
});
assertEquals(0, last);
}

@Test
public void testLastOrDefaultWithPredicate() {
Observable<Integer> observable = toObservable(1, 0, -1);
int last = observable.lastOrDefault(0, new Func1<Integer, Boolean>() {
@Override
public Boolean call(Integer args) {
return args < 0;
}
});

assertEquals(-1, last);
}

private static class TestException extends RuntimeException {

}
Expand Down