1 package org.modelmapper.internal.converter;
2
3 import java.util.UUID;
4 import org.modelmapper.spi.ConditionalConverter;
5 import org.modelmapper.spi.MappingContext;
6
7 /**
8  * Converts objects to UUID.
9  */

10 class UuidConverter implements ConditionalConverter<Object, UUID> {
11
12   public UUID convert(MappingContext<Object, UUID> context) {
13     Object source = context.getSource();
14     if (source == null) {
15         return null;
16     }
17
18     Class<?> sourceType = context.getSourceType();
19     if (isCharArray(sourceType)) {
20       return UUID.fromString(new String((char[]) source));
21     }
22     return UUID.fromString(source.toString());
23   }
24
25   public MatchResult match(Class<?> sourceType, Class<?> destinationType) {
26     boolean destMatch = destinationType == UUID.class;
27     return destMatch ? isCharArray(sourceType) || sourceType == String.class
28         ? MatchResult.FULL
29         : MatchResult.PARTIAL
30         : MatchResult.NONE;
31   }
32
33   private boolean isCharArray(Class<?> sourceType) {
34     return sourceType.isArray() && (sourceType.getComponentType() == Character.TYPE
35         || sourceType.getComponentType() == Character.class);
36   }
37 }