Skip to content

Commit 3bc8a43

Browse files
DATAMONGO-1416 - Get rid of the warnings for Atomic… type conversions.
We now use explicit converters instead of a ConverterFactory. This reduces noise in log when registering converters. Original pull request: #362.
1 parent f82f030 commit 3bc8a43

File tree

2 files changed

+107
-1
lines changed

2 files changed

+107
-1
lines changed

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

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ public static Collection<Object> getConvertersToRegister() {
8080
converters.add(DocumentToNamedMongoScriptCoverter.INSTANCE);
8181
converters.add(CurrencyToStringConverter.INSTANCE);
8282
converters.add(StringToCurrencyConverter.INSTANCE);
83-
converters.add(NumberToNumberConverterFactory.INSTANCE);
83+
converters.add(AtomicIntegerToIntegerConverter.INSTANCE);
84+
converters.add(AtomicLongToLongConverter.INSTANCE);
85+
converters.add(LongToAtomicLongConverter.INSTANCE);
86+
converters.add(IntegerToAtomicIntegerConverter.INSTANCE);
8487

8588
return converters;
8689
}
@@ -389,4 +392,68 @@ public T convert(Number source) {
389392
}
390393
}
391394
}
395+
396+
/**
397+
* {@link ConverterFactory} implementation converting {@link AtomicLong} into {@link Long}.
398+
*
399+
* @author Christoph Strobl
400+
* @since 1.10
401+
*/
402+
@WritingConverter
403+
public static enum AtomicLongToLongConverter implements Converter<AtomicLong, Long> {
404+
INSTANCE;
405+
406+
@Override
407+
public Long convert(AtomicLong source) {
408+
return NumberUtils.convertNumberToTargetClass(source, Long.class);
409+
}
410+
}
411+
412+
/**
413+
* {@link ConverterFactory} implementation converting {@link AtomicInteger} into {@link Integer}.
414+
*
415+
* @author Christoph Strobl
416+
* @since 1.10
417+
*/
418+
@WritingConverter
419+
public static enum AtomicIntegerToIntegerConverter implements Converter<AtomicInteger, Integer> {
420+
INSTANCE;
421+
422+
@Override
423+
public Integer convert(AtomicInteger source) {
424+
return NumberUtils.convertNumberToTargetClass(source, Integer.class);
425+
}
426+
}
427+
428+
/**
429+
* {@link ConverterFactory} implementation converting {@link Long} into {@link AtomicLong}.
430+
*
431+
* @author Christoph Strobl
432+
* @since 1.10
433+
*/
434+
@ReadingConverter
435+
public static enum LongToAtomicLongConverter implements Converter<Long, AtomicLong> {
436+
INSTANCE;
437+
438+
@Override
439+
public AtomicLong convert(Long source) {
440+
return source != null ? new AtomicLong(source) : null;
441+
}
442+
}
443+
444+
/**
445+
* {@link ConverterFactory} implementation converting {@link Integer} into {@link AtomicInteger}.
446+
*
447+
* @author Christoph Strobl
448+
* @since 1.10
449+
*/
450+
@ReadingConverter
451+
public static enum IntegerToAtomicIntegerConverter implements Converter<Integer, AtomicInteger> {
452+
INSTANCE;
453+
454+
@Override
455+
public AtomicInteger convert(Integer source) {
456+
return source != null ? new AtomicInteger(source) : null;
457+
}
458+
}
392459
}

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,22 @@
2020

2121
import java.math.BigDecimal;
2222
import java.util.Currency;
23+
import java.util.concurrent.atomic.AtomicInteger;
24+
import java.util.concurrent.atomic.AtomicLong;
2325

26+
import org.hamcrest.core.IsInstanceOf;
2427
import org.junit.Test;
2528
import org.springframework.data.geo.Box;
2629
import org.springframework.data.geo.Circle;
2730
import org.springframework.data.geo.Point;
2831
import org.springframework.data.geo.Polygon;
2932
import org.springframework.data.geo.Shape;
33+
import org.springframework.data.mongodb.core.convert.MongoConverters.AtomicIntegerToIntegerConverter;
34+
import org.springframework.data.mongodb.core.convert.MongoConverters.AtomicLongToLongConverter;
3035
import org.springframework.data.mongodb.core.convert.MongoConverters.BigDecimalToStringConverter;
3136
import org.springframework.data.mongodb.core.convert.MongoConverters.CurrencyToStringConverter;
37+
import org.springframework.data.mongodb.core.convert.MongoConverters.IntegerToAtomicIntegerConverter;
38+
import org.springframework.data.mongodb.core.convert.MongoConverters.LongToAtomicLongConverter;
3239
import org.springframework.data.mongodb.core.convert.MongoConverters.StringToBigDecimalConverter;
3340
import org.springframework.data.mongodb.core.convert.MongoConverters.StringToCurrencyConverter;
3441
import org.springframework.data.mongodb.core.geo.Sphere;
@@ -140,4 +147,36 @@ public void convertsCurrencyToStringCorrectly() {
140147
public void convertsStringToCurrencyCorrectly() {
141148
assertThat(StringToCurrencyConverter.INSTANCE.convert("USD"), is(Currency.getInstance("USD")));
142149
}
150+
151+
/**
152+
* @see DATAMONGO-1416
153+
*/
154+
@Test
155+
public void convertsAtomicLongToLongCorrectly() {
156+
assertThat(AtomicLongToLongConverter.INSTANCE.convert(new AtomicLong(100L)), is(100L));
157+
}
158+
159+
/**
160+
* @see DATAMONGO-1416
161+
*/
162+
@Test
163+
public void convertsAtomicIntegerToIntegerCorrectly() {
164+
assertThat(AtomicIntegerToIntegerConverter.INSTANCE.convert(new AtomicInteger(100)), is(100));
165+
}
166+
167+
/**
168+
* @see DATAMONGO-1416
169+
*/
170+
@Test
171+
public void convertsLongToAtomicLongCorrectly() {
172+
assertThat(LongToAtomicLongConverter.INSTANCE.convert(100L), IsInstanceOf.instanceOf(AtomicLong.class));
173+
}
174+
175+
/**
176+
* @see DATAMONGO-1416
177+
*/
178+
@Test
179+
public void convertsIntegerToAtomicIntegerCorrectly() {
180+
assertThat(IntegerToAtomicIntegerConverter.INSTANCE.convert(100), IsInstanceOf.instanceOf(AtomicInteger.class));
181+
}
143182
}

0 commit comments

Comments
 (0)