Skip to content

Commit b9282c8

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-1726 - Add oneValue() and firstValue() to FluentMongoOperations returning nullable types.
We leave the choice of using Optional open by also providing terminating find operation methods that return null instead of Optional. Original pull request: spring-projects#475.
1 parent 3ac379a commit b9282c8

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableFindOperation.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,33 @@ interface TerminatingFindOperation<T> {
7373
* @return {@link Optional#empty()} if no match found.
7474
* @throws org.springframework.dao.IncorrectResultSizeDataAccessException if more than one match found.
7575
*/
76-
Optional<T> one();
76+
default Optional<T> one() {
77+
return Optional.ofNullable(oneValue());
78+
}
79+
80+
/**
81+
* Get exactly zero or one result.
82+
*
83+
* @return {@literal null} if no match found.
84+
* @throws org.springframework.dao.IncorrectResultSizeDataAccessException if more than one match found.
85+
*/
86+
T oneValue();
7787

7888
/**
7989
* Get the first or no result.
8090
*
8191
* @return {@link Optional#empty()} if no match found.
8292
*/
83-
Optional<T> first();
93+
default Optional<T> first() {
94+
return Optional.ofNullable(firstValue());
95+
}
96+
97+
/**
98+
* Get the first or no result.
99+
*
100+
* @return {@literal null} if no match found.
101+
*/
102+
T firstValue();
84103

85104
/**
86105
* Get all matching elements.

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableFindOperationSupport.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,27 +104,27 @@ public TerminatingFindOperation<T> matching(Query query) {
104104
}
105105

106106
@Override
107-
public Optional<T> one() {
107+
public T oneValue() {
108108

109109
List<T> result = doFind(new DelegatingQueryCursorPreparer(getCursorPreparer(query, null)).limit(2));
110110

111111
if (ObjectUtils.isEmpty(result)) {
112-
return Optional.empty();
112+
return null;
113113
}
114114

115115
if (result.size() > 1) {
116116
throw new IncorrectResultSizeDataAccessException("Query " + asString() + " returned non unique result.", 1);
117117
}
118118

119-
return Optional.of(result.iterator().next());
119+
return result.iterator().next();
120120
}
121121

122122
@Override
123-
public Optional<T> first() {
123+
public T firstValue() {
124124

125125
List<T> result = doFind(new DelegatingQueryCursorPreparer(getCursorPreparer(query, null)).limit(1));
126126

127-
return ObjectUtils.isEmpty(result) ? Optional.empty() : Optional.of(result.iterator().next());
127+
return ObjectUtils.isEmpty(result) ? null : result.iterator().next();
128128
}
129129

130130
@Override

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ExecutableFindOperationSupportTests.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,30 @@ public void findByTooManyResults() {
140140
template.query(Person.class).matching(query(where("firstname").in("han", "luke"))).one();
141141
}
142142

143+
@Test // DATAMONGO-1726
144+
public void findByReturningOneValue() {
145+
assertThat(template.query(Person.class).matching(query(where("firstname").is("luke"))).oneValue()).isEqualTo(luke);
146+
}
147+
148+
@Test(expected = IncorrectResultSizeDataAccessException.class) // DATAMONGO-1726
149+
public void findByReturningOneValueButTooManyResults() {
150+
template.query(Person.class).matching(query(where("firstname").in("han", "luke"))).oneValue();
151+
}
152+
153+
@Test // DATAMONGO-1726
154+
public void findByReturningFirstValue() {
155+
156+
assertThat(template.query(Person.class).matching(query(where("firstname").is("luke"))).firstValue())
157+
.isEqualTo(luke);
158+
}
159+
160+
@Test // DATAMONGO-1726
161+
public void findByReturningFirstValueForManyResults() {
162+
163+
assertThat(template.query(Person.class).matching(query(where("firstname").in("han", "luke"))).firstValue())
164+
.isIn(han, luke);
165+
}
166+
143167
@Test // DATAMONGO-1563
144168
public void streamAll() {
145169

0 commit comments

Comments
 (0)