Skip to content

Commit e342717

Browse files
committed
Added support for UUID conversion
1 parent 24b0aeb commit e342717

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

src/main/java/org/bbottema/javareflection/valueconverter/ValueConversionHelper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.bbottema.javareflection.valueconverter.converters.FileConverters;
2626
import org.bbottema.javareflection.valueconverter.converters.NumberConverters;
2727
import org.bbottema.javareflection.valueconverter.converters.StringConverters;
28+
import org.bbottema.javareflection.valueconverter.converters.UUIDConverters;
2829
import org.jetbrains.annotations.NotNull;
2930
import org.jetbrains.annotations.Nullable;
3031

@@ -91,6 +92,7 @@ public static void resetDefaultConverters() {
9192
defaultConverters.addAll(BooleanConverters.BOOLEAN_CONVERTERS);
9293
defaultConverters.addAll(CharacterConverters.CHARACTER_CONVERTERS);
9394
defaultConverters.addAll(StringConverters.STRING_CONVERTERS);
95+
defaultConverters.addAll(UUIDConverters.UUID_CONVERTERS);
9496
defaultConverters.addAll(FileConverters.FILE_CONVERTERS);
9597

9698
for (ValueFunction<?, ?> numberConverter : defaultConverters) {

src/main/java/org/bbottema/javareflection/valueconverter/converters/StringConverters.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.math.BigInteger;
3333
import java.util.ArrayList;
3434
import java.util.Collection;
35+
import java.util.UUID;
3536

3637
import static org.slf4j.LoggerFactory.getLogger;
3738

@@ -50,6 +51,7 @@
5051
* <li><strong>Double (or primitive double)</strong>: <code>Double.parseDouble(value)</code></li>
5152
* <li><strong>BigInteger</strong>: <code>BigInteger.valueOf(Long.parseLong(value))</code></li>
5253
* <li><strong>BigDecimal</strong>: <code>new BigDecimal(value)</code></li>
54+
* <li><strong>UUID</strong>: <code>UUID.fromString(value)</code></li>
5355
* </ol>
5456
*/
5557
@Nullable
@@ -72,6 +74,7 @@ public final class StringConverters {
7274
converters.add(new ValueFunctionImpl<>(String.class, BigInteger.class, new StringToBigIntegerFunction()));
7375
converters.add(new ValueFunctionImpl<>(String.class, BigDecimal.class, new StringToBigDecimalFunction()));
7476
converters.add(new ValueFunctionImpl<>(String.class, File.class, new StringToFileFunction()));
77+
converters.add(new ValueFunctionImpl<>(String.class, UUID.class, new StringToUUIDFunction()));
7578
return converters;
7679
}
7780

@@ -230,4 +233,15 @@ public File apply(String value) {
230233
return file;
231234
}
232235
}
236+
237+
private static class StringToUUIDFunction implements Function<String, UUID> {
238+
@Override
239+
public UUID apply(String value) {
240+
try {
241+
return UUID.fromString(value);
242+
} catch (IllegalArgumentException e) {
243+
throw new IncompatibleTypeException(value, String.class, UUID.class);
244+
}
245+
}
246+
}
233247
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (C) ${project.inceptionYear} Benny Bottema ([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.bbottema.javareflection.valueconverter.converters;
17+
18+
import lombok.experimental.UtilityClass;
19+
import org.bbottema.javareflection.util.Function;
20+
import org.bbottema.javareflection.util.Function.Functions;
21+
import org.bbottema.javareflection.valueconverter.ValueFunction;
22+
import org.bbottema.javareflection.valueconverter.ValueFunction.ValueFunctionImpl;
23+
import org.jetbrains.annotations.Nullable;
24+
25+
import java.util.ArrayList;
26+
import java.util.Collection;
27+
import java.util.UUID;
28+
29+
@Nullable
30+
@UtilityClass
31+
public final class UUIDConverters {
32+
33+
public static final Collection<ValueFunction<UUID, ?>> UUID_CONVERTERS = produceUUIDConverters();
34+
35+
private static Collection<ValueFunction<UUID, ?>> produceUUIDConverters() {
36+
ArrayList<ValueFunction<UUID, ?>> converters = new ArrayList<>();
37+
converters.add(new ValueFunctionImpl<>(UUID.class, UUID.class, Functions.<UUID>identity()));
38+
converters.add(new ValueFunctionImpl<>(UUID.class, String.class, new UUIDToStringFunction()));
39+
return converters;
40+
}
41+
42+
private static class UUIDToStringFunction implements Function<UUID, String> {
43+
@Override
44+
public String apply(UUID value) {
45+
return value.toString();
46+
}
47+
}
48+
}

src/test/java/org/bbottema/javareflection/valueconverter/ValueConversionHelperTest.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
import org.junit.Test;
2424
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
2525

26-
import java.io.File;
2726
import java.math.BigDecimal;
2827
import java.math.BigInteger;
2928
import java.util.Calendar;
3029
import java.util.HashSet;
3130
import java.util.Set;
31+
import java.util.UUID;
3232

3333
import static java.lang.String.format;
3434
import static java.util.Arrays.asList;
@@ -273,6 +273,8 @@ public void testConvertStringClassOfQ() {
273273
assertThat(ValueConversionHelper.convert("h", String.class)).isEqualTo("h");
274274
assertThat(ValueConversionHelper.convert("ONE", TestEnum.class)).isEqualTo(TestEnum.ONE);
275275
assertThat(ValueConversionHelper.convert("h", Boolean.class)).isTrue();
276+
final UUID uuid = UUID.randomUUID();
277+
assertThat(ValueConversionHelper.convert(uuid.toString(), UUID.class)).isEqualTo(uuid);
276278
try {
277279
ValueConversionHelper.convert("falsef", Boolean.class);
278280
fail("should not be able to convert value");
@@ -297,6 +299,24 @@ public void testConvertStringClassOfQ() {
297299
} catch (IncompatibleTypeException e) {
298300
// OK
299301
}
302+
try {
303+
ValueConversionHelper.convert("", UUID.class);
304+
fail("should not be able to convert value");
305+
} catch (IncompatibleTypeException e) {
306+
// OK
307+
}
308+
try {
309+
ValueConversionHelper.convert("s", UUID.class);
310+
fail("should not be able to convert value");
311+
} catch (IncompatibleTypeException e) {
312+
// OK
313+
}
314+
try {
315+
ValueConversionHelper.convert("s-s-s-s-s", UUID.class);
316+
fail("should not be able to convert value");
317+
} catch (IncompatibleTypeException e) {
318+
// OK
319+
}
300320
}
301321

302322
@Test

0 commit comments

Comments
 (0)