Skip to content

Commit b626c2f

Browse files
christophstroblodrotbohm
authored andcommitted
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 c3e894e commit b626c2f

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
@@ -81,7 +81,10 @@ public static Collection<Object> getConvertersToRegister() {
8181
converters.add(DBObjectToNamedMongoScriptCoverter.INSTANCE);
8282
converters.add(CurrencyToStringConverter.INSTANCE);
8383
converters.add(StringToCurrencyConverter.INSTANCE);
84-
converters.add(NumberToNumberConverterFactory.INSTANCE);
84+
converters.add(AtomicIntegerToIntegerConverter.INSTANCE);
85+
converters.add(AtomicLongToLongConverter.INSTANCE);
86+
converters.add(LongToAtomicLongConverter.INSTANCE);
87+
converters.add(IntegerToAtomicIntegerConverter.INSTANCE);
8588

8689
return converters;
8790
}
@@ -374,4 +377,68 @@ public T convert(Number source) {
374377
}
375378
}
376379
}
380+
381+
/**
382+
* {@link ConverterFactory} implementation converting {@link AtomicLong} into {@link Long}.
383+
*
384+
* @author Christoph Strobl
385+
* @since 1.10
386+
*/
387+
@WritingConverter
388+
public static enum AtomicLongToLongConverter implements Converter<AtomicLong, Long> {
389+
INSTANCE;
390+
391+
@Override
392+
public Long convert(AtomicLong source) {
393+
return NumberUtils.convertNumberToTargetClass(source, Long.class);
394+
}
395+
}
396+
397+
/**
398+
* {@link ConverterFactory} implementation converting {@link AtomicInteger} into {@link Integer}.
399+
*
400+
* @author Christoph Strobl
401+
* @since 1.10
402+
*/
403+
@WritingConverter
404+
public static enum AtomicIntegerToIntegerConverter implements Converter<AtomicInteger, Integer> {
405+
INSTANCE;
406+
407+
@Override
408+
public Integer convert(AtomicInteger source) {
409+
return NumberUtils.convertNumberToTargetClass(source, Integer.class);
410+
}
411+
}
412+
413+
/**
414+
* {@link ConverterFactory} implementation converting {@link Long} into {@link AtomicLong}.
415+
*
416+
* @author Christoph Strobl
417+
* @since 1.10
418+
*/
419+
@ReadingConverter
420+
public static enum LongToAtomicLongConverter implements Converter<Long, AtomicLong> {
421+
INSTANCE;
422+
423+
@Override
424+
public AtomicLong convert(Long source) {
425+
return source != null ? new AtomicLong(source) : null;
426+
}
427+
}
428+
429+
/**
430+
* {@link ConverterFactory} implementation converting {@link Integer} into {@link AtomicInteger}.
431+
*
432+
* @author Christoph Strobl
433+
* @since 1.10
434+
*/
435+
@ReadingConverter
436+
public static enum IntegerToAtomicIntegerConverter implements Converter<Integer, AtomicInteger> {
437+
INSTANCE;
438+
439+
@Override
440+
public AtomicInteger convert(Integer source) {
441+
return source != null ? new AtomicInteger(source) : null;
442+
}
443+
}
377444
}

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)