Skip to content
This repository was archived by the owner on Aug 17, 2020. It is now read-only.

Commit b36fbbd

Browse files
committed
Merge pull request #56 from phajduk/added-maptooneordefault
mapToOneOrNull replaced by mapToOneOrDefault
2 parents 67e9645 + 8d792c2 commit b36fbbd

3 files changed

Lines changed: 36 additions & 19 deletions

File tree

sqlbrite/src/androidTest/java/com/squareup/sqlbrite/BriteDatabaseTest.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ public final class BriteDatabaseTest {
145145
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES) //
146146
.mapToList(new Func1<Cursor, Employee>() {
147147
private int count;
148+
148149
@Override public Employee call(Cursor cursor) {
149150
return count++ == 2 ? null : Employee.MAPPER.call(cursor);
150151
}
@@ -207,30 +208,38 @@ public final class BriteDatabaseTest {
207208
}
208209
}
209210

210-
@Test public void queryMapToOneOrNull() {
211+
@Test public void queryMapToOneOrDefault() {
211212
Employee employees = db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " LIMIT 1")
212-
.mapToOneOrNull(Employee.MAPPER)
213+
.mapToOneOrDefault(Employee.MAPPER, null)
213214
.toBlocking()
214215
.first();
215216
assertThat(employees).isEqualTo(new Employee("alice", "Alice Allison"));
216217
}
217218

218-
@Test public void queryMapToOneOrNullEmpty() {
219+
@Test public void queryMapToOneOrDefaultEmpty() {
219220
Employee employees = db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " WHERE 1=2")
220-
.mapToOneOrNull(Employee.MAPPER)
221+
.mapToOneOrDefault(Employee.MAPPER, new Employee("bob", "Bob Bobberson"))
222+
.toBlocking()
223+
.first();
224+
assertThat(employees).isEqualTo(new Employee("bob", "Bob Bobberson"));
225+
}
226+
227+
@Test public void queryMapToOneOrDefaultEmptyUsingNull() {
228+
Employee employees = db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " WHERE 1=2")
229+
.mapToOneOrDefault(Employee.MAPPER, null)
221230
.toBlocking()
222231
.first();
223232
assertThat(employees).isNull();
224233
}
225234

226-
@Test public void queryMapToOneOrNullMapperReturnNullThrows() {
235+
@Test public void queryMapToOneOrDefaultMapperReturnNullThrows() {
227236
BlockingObservable<Employee> employees =
228237
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES) //
229-
.mapToOneOrNull(new Func1<Cursor, Employee>() {
238+
.mapToOneOrDefault(new Func1<Cursor, Employee>() {
230239
@Override public Employee call(Cursor cursor) {
231240
return null;
232241
}
233-
}) //
242+
}, null) //
234243
.toBlocking();
235244
try {
236245
employees.first();
@@ -241,10 +250,10 @@ public final class BriteDatabaseTest {
241250
}
242251
}
243252

244-
@Test public void queryMapToOneOrNullMultipleRowsThrows() {
253+
@Test public void queryMapToOneOrDefaultMultipleRowsThrows() {
245254
BlockingObservable<Employee> employees =
246255
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " LIMIT 2") //
247-
.mapToOneOrNull(Employee.MAPPER) //
256+
.mapToOneOrDefault(Employee.MAPPER, null) //
248257
.toBlocking();
249258
try {
250259
employees.first();

sqlbrite/src/main/java/com/squareup/sqlbrite/QueryObservable.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public final class QueryObservable extends Observable<Query> {
3636
*/
3737
@CheckResult @NonNull
3838
public final <T> Observable<T> mapToOne(@NonNull final Func1<Cursor, T> mapper) {
39-
return lift(new QueryToOneOperator<>(mapper, false));
39+
return lift(new QueryToOneOperator<>(mapper, false, null));
4040
}
4141

4242
/**
@@ -45,18 +45,20 @@ public final <T> Observable<T> mapToOne(@NonNull final Func1<Cursor, T> mapper)
4545
* <p>
4646
* It is an error for a query to pass through this operator with more than 1 row in its result
4747
* set. Use {@code LIMIT 1} on the underlying SQL query to prevent this. Result sets with 0 rows
48-
* emit {@code null}.
48+
* emit {@code defaultValue}.
4949
* <p>
5050
* This method is equivalent to:
5151
* <pre>{@code
52-
* flatMap(q -> q.asRows(mapper).take(1).defaultIfEmpty(null))
52+
* flatMap(q -> q.asRows(mapper).take(1).defaultIfEmpty(defaultValue))
5353
* }</pre>
5454
*
5555
* @param mapper Maps the current {@link Cursor} row to {@code T}. May not return null.
56+
* @param defaultValue Value returned if result set is empty
5657
*/
5758
@CheckResult @NonNull
58-
public final <T> Observable<T> mapToOneOrNull(@NonNull final Func1<Cursor, T> mapper) {
59-
return lift(new QueryToOneOperator<>(mapper, true));
59+
public final <T> Observable<T> mapToOneOrDefault(@NonNull final Func1<Cursor, T> mapper,
60+
T defaultValue) {
61+
return lift(new QueryToOneOperator<>(mapper, true, defaultValue));
6062
}
6163

6264
/**

sqlbrite/src/main/java/com/squareup/sqlbrite/QueryToOneOperator.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99

1010
final class QueryToOneOperator<T> implements Observable.Operator<T, SqlBrite.Query> {
1111
private final Func1<Cursor, T> mapper;
12-
private final boolean emitNull;
12+
private boolean emitDefault;
13+
private T defaultValue;
1314

14-
QueryToOneOperator(Func1<Cursor, T> mapper, boolean emitNull) {
15+
QueryToOneOperator(Func1<Cursor, T> mapper, boolean emitDefault, T defaultValue) {
1516
this.mapper = mapper;
16-
this.emitNull = emitNull;
17+
this.emitDefault = emitDefault;
18+
this.defaultValue = defaultValue;
1719
}
1820

1921
@Override public Subscriber<? super SqlBrite.Query> call(final Subscriber<? super T> subscriber) {
@@ -35,8 +37,12 @@ final class QueryToOneOperator<T> implements Observable.Operator<T, SqlBrite.Que
3537
} finally {
3638
cursor.close();
3739
}
38-
if (!subscriber.isUnsubscribed() && (item != null || emitNull)) {
39-
subscriber.onNext(item);
40+
if (!subscriber.isUnsubscribed()) {
41+
if (item != null) {
42+
subscriber.onNext(item);
43+
} else if (emitDefault) {
44+
subscriber.onNext(defaultValue);
45+
}
4046
}
4147
} catch (Throwable e) {
4248
Exceptions.throwIfFatal(e);

0 commit comments

Comments
 (0)