Skip to content

Commit ff60149

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-2385 - Remove unnecessary null checks in MongoConverters.
Original pull request: spring-projects#802.
1 parent f7a0108 commit ff60149

File tree

3 files changed

+14
-35
lines changed

3 files changed

+14
-35
lines changed

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

+11-20
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ enum ObjectIdToStringConverter implements Converter<ObjectId, String> {
110110
INSTANCE;
111111

112112
public String convert(ObjectId id) {
113-
return id == null ? null : id.toString();
113+
return id.toString();
114114
}
115115
}
116116

@@ -136,7 +136,7 @@ enum ObjectIdToBigIntegerConverter implements Converter<ObjectId, BigInteger> {
136136
INSTANCE;
137137

138138
public BigInteger convert(ObjectId source) {
139-
return source == null ? null : new BigInteger(source.toString(), 16);
139+
return new BigInteger(source.toString(), 16);
140140
}
141141
}
142142

@@ -149,15 +149,15 @@ enum BigIntegerToObjectIdConverter implements Converter<BigInteger, ObjectId> {
149149
INSTANCE;
150150

151151
public ObjectId convert(BigInteger source) {
152-
return source == null ? null : new ObjectId(source.toString(16));
152+
return new ObjectId(source.toString(16));
153153
}
154154
}
155155

156156
enum BigDecimalToStringConverter implements Converter<BigDecimal, String> {
157157
INSTANCE;
158158

159159
public String convert(BigDecimal source) {
160-
return source == null ? null : source.toString();
160+
return source.toString();
161161
}
162162
}
163163

@@ -168,7 +168,7 @@ enum BigDecimalToDecimal128Converter implements Converter<BigDecimal, Decimal128
168168
INSTANCE;
169169

170170
public Decimal128 convert(BigDecimal source) {
171-
return source == null ? null : new Decimal128(source);
171+
return new Decimal128(source);
172172
}
173173
}
174174

@@ -195,7 +195,7 @@ enum BigIntegerToStringConverter implements Converter<BigInteger, String> {
195195
INSTANCE;
196196

197197
public String convert(BigInteger source) {
198-
return source == null ? null : source.toString();
198+
return source.toString();
199199
}
200200
}
201201

@@ -238,11 +238,6 @@ enum DocumentToStringConverter implements Converter<Document, String> {
238238

239239
@Override
240240
public String convert(Document source) {
241-
242-
if (source == null) {
243-
return null;
244-
}
245-
246241
return source.toJson();
247242
}
248243
}
@@ -258,7 +253,7 @@ enum TermToStringConverter implements Converter<Term, String> {
258253

259254
@Override
260255
public String convert(Term source) {
261-
return source == null ? null : source.getFormatted();
256+
return source.getFormatted();
262257
}
263258
}
264259

@@ -273,7 +268,7 @@ enum DocumentToNamedMongoScriptConverter implements Converter<Document, NamedMon
273268
@Override
274269
public NamedMongoScript convert(Document source) {
275270

276-
if (source == null) {
271+
if(source.isEmpty()) {
277272
return null;
278273
}
279274

@@ -295,10 +290,6 @@ enum NamedMongoScriptToDocumentConverter implements Converter<NamedMongoScript,
295290
@Override
296291
public Document convert(NamedMongoScript source) {
297292

298-
if (source == null) {
299-
return new Document();
300-
}
301-
302293
Document document = new Document();
303294

304295
document.put("_id", source.getName());
@@ -325,7 +316,7 @@ enum CurrencyToStringConverter implements Converter<Currency, String> {
325316
*/
326317
@Override
327318
public String convert(Currency source) {
328-
return source == null ? null : source.getCurrencyCode();
319+
return source.getCurrencyCode();
329320
}
330321
}
331322

@@ -461,7 +452,7 @@ enum LongToAtomicLongConverter implements Converter<Long, AtomicLong> {
461452

462453
@Override
463454
public AtomicLong convert(Long source) {
464-
return source != null ? new AtomicLong(source) : null;
455+
return new AtomicLong(source);
465456
}
466457
}
467458

@@ -477,7 +468,7 @@ enum IntegerToAtomicIntegerConverter implements Converter<Integer, AtomicInteger
477468

478469
@Override
479470
public AtomicInteger convert(Integer source) {
480-
return source != null ? new AtomicInteger(source) : null;
471+
return new AtomicInteger(source);
481472
}
482473
}
483474

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

+3-8
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@ public static class NamedMongoScriptToDocumentConverterUnitTests {
5656

5757
NamedMongoScriptToDocumentConverter converter = NamedMongoScriptToDocumentConverter.INSTANCE;
5858

59-
@Test // DATAMONGO-479
60-
public void convertShouldReturnEmptyDocWhenScriptIsNull() {
61-
assertThat(converter.convert(null)).isEqualTo(new Document());
62-
}
63-
6459
@Test // DATAMONGO-479
6560
public void convertShouldConvertScriptNameCorreclty() {
6661

@@ -87,9 +82,9 @@ public static class DocumentToNamedMongoScriptConverterUnitTests {
8782

8883
DocumentToNamedMongoScriptConverter converter = DocumentToNamedMongoScriptConverter.INSTANCE;
8984

90-
@Test // DATAMONGO-479
91-
public void convertShouldReturnNullIfSourceIsNull() {
92-
assertThat(converter.convert(null)).isNull();
85+
@Test // DATAMONGO-479, DATAMONGO-2385
86+
public void convertShouldReturnNullIfSourceIsEmpty() {
87+
assertThat(converter.convert(new Document())).isNull();
9388
}
9489

9590
@Test // DATAMONGO-479

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

-7
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
*/
1616
package org.springframework.data.mongodb.core.convert;
1717

18-
import static org.assertj.core.api.Assertions.*;
1918
import static org.mockito.Mockito.*;
2019

2120
import org.junit.Test;
22-
2321
import org.springframework.data.mongodb.core.convert.MongoConverters.TermToStringConverter;
2422
import org.springframework.data.mongodb.core.query.Term;
2523
import org.springframework.data.mongodb.core.query.Term.Type;
@@ -29,11 +27,6 @@
2927
*/
3028
public class TermToStringConverterUnitTests {
3129

32-
@Test // DATAMONGO-973
33-
public void shouldNotConvertNull() {
34-
assertThat(TermToStringConverter.INSTANCE.convert(null)).isNull();
35-
}
36-
3730
@Test // DATAMONGO-973
3831
public void shouldUseFormattedRepresentationForConversion() {
3932

0 commit comments

Comments
 (0)