/
TypeTranslatorImpl.java
133 lines (122 loc) · 6 KB
/
TypeTranslatorImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* Hibernate OGM, Domain model persistence for NoSQL datastores
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.ogm.type.impl;
import static org.hibernate.ogm.util.impl.CollectionHelper.newHashMap;
import java.util.Collections;
import java.util.Map;
import org.hibernate.ogm.dialect.spi.GridDialect;
import org.hibernate.ogm.type.spi.GridType;
import org.hibernate.ogm.type.spi.TypeTranslator;
import org.hibernate.ogm.util.impl.Log;
import org.hibernate.ogm.util.impl.LoggerFactory;
import org.hibernate.type.AbstractStandardBasicType;
import org.hibernate.type.CustomType;
import org.hibernate.type.EnumType;
import org.hibernate.type.Type;
import org.hibernate.type.descriptor.java.BigDecimalTypeDescriptor;
import org.hibernate.type.descriptor.java.BigIntegerTypeDescriptor;
import org.hibernate.type.descriptor.java.BooleanTypeDescriptor;
import org.hibernate.type.descriptor.java.ByteTypeDescriptor;
import org.hibernate.type.descriptor.java.CalendarDateTypeDescriptor;
import org.hibernate.type.descriptor.java.CalendarTypeDescriptor;
import org.hibernate.type.descriptor.java.CharacterTypeDescriptor;
import org.hibernate.type.descriptor.java.ClassTypeDescriptor;
import org.hibernate.type.descriptor.java.DoubleTypeDescriptor;
import org.hibernate.type.descriptor.java.FloatTypeDescriptor;
import org.hibernate.type.descriptor.java.IntegerTypeDescriptor;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.java.JdbcDateTypeDescriptor;
import org.hibernate.type.descriptor.java.JdbcTimeTypeDescriptor;
import org.hibernate.type.descriptor.java.JdbcTimestampTypeDescriptor;
import org.hibernate.type.descriptor.java.LongTypeDescriptor;
import org.hibernate.type.descriptor.java.PrimitiveByteArrayTypeDescriptor;
import org.hibernate.type.descriptor.java.ShortTypeDescriptor;
import org.hibernate.type.descriptor.java.StringTypeDescriptor;
import org.hibernate.type.descriptor.java.UUIDTypeDescriptor;
import org.hibernate.type.descriptor.java.UrlTypeDescriptor;
import org.hibernate.usertype.UserType;
/**
* @author Emmanuel Bernard
* @author Nicolas Helleringer
*/
public class TypeTranslatorImpl implements TypeTranslator {
private static final Log log = LoggerFactory.make();
private final Map<JavaTypeDescriptor<?>, GridType> typeConverter;
private final GridDialect dialect;
public TypeTranslatorImpl(GridDialect dialect) {
this.dialect = dialect;
Map<JavaTypeDescriptor<?>, GridType> tmpMap = newHashMap( 20 );
tmpMap.put( ClassTypeDescriptor.INSTANCE, ClassType.INSTANCE );
tmpMap.put( LongTypeDescriptor.INSTANCE, LongType.INSTANCE );
tmpMap.put( IntegerTypeDescriptor.INSTANCE, IntegerType.INSTANCE );
tmpMap.put( DoubleTypeDescriptor.INSTANCE, DoubleType.INSTANCE );
tmpMap.put( FloatTypeDescriptor.INSTANCE, FloatType.INSTANCE );
tmpMap.put( ShortTypeDescriptor.INSTANCE, ShortType.INSTANCE );
tmpMap.put( CharacterTypeDescriptor.INSTANCE, CharacterType.INSTANCE );
tmpMap.put( StringTypeDescriptor.INSTANCE, StringType.INSTANCE );
tmpMap.put( UrlTypeDescriptor.INSTANCE, UrlType.INSTANCE );
tmpMap.put( BigDecimalTypeDescriptor.INSTANCE, BigDecimalType.INSTANCE );
tmpMap.put( BigIntegerTypeDescriptor.INSTANCE, BigIntegerType.INSTANCE );
tmpMap.put( BooleanTypeDescriptor.INSTANCE, BooleanType.INSTANCE );
tmpMap.put( ByteTypeDescriptor.INSTANCE, ByteType.INSTANCE );
tmpMap.put( JdbcDateTypeDescriptor.INSTANCE, DateType.INSTANCE );
tmpMap.put( JdbcTimestampTypeDescriptor.INSTANCE, TimestampType.INSTANCE );
tmpMap.put( JdbcTimeTypeDescriptor.INSTANCE, TimeType.INSTANCE );
tmpMap.put( CalendarDateTypeDescriptor.INSTANCE, CalendarDateType.INSTANCE );
tmpMap.put( CalendarTypeDescriptor.INSTANCE, CalendarType.INSTANCE );
tmpMap.put( PrimitiveByteArrayTypeDescriptor.INSTANCE, PrimitiveByteArrayType.INSTANCE );
tmpMap.put( UUIDTypeDescriptor.INSTANCE, UUIDType.INSTANCE );
typeConverter = Collections.unmodifiableMap( tmpMap );
}
@Override public GridType getType(Type type) {
if ( type == null ) {
return null;
}
//TODO should we cache results? It seems an actual HashMap might be slower but it makes it more robust
// against badly written dialects
GridType dialectType = dialect.overrideType( type );
if ( dialectType != null ) {
return dialectType;
}
else if ( type instanceof AbstractStandardBasicType ) {
AbstractStandardBasicType<?> exposedType = (AbstractStandardBasicType<?>) type;
final GridType gridType = typeConverter.get( exposedType.getJavaTypeDescriptor() );
if (gridType == null) {
throw log.unableToFindGridType( exposedType.getJavaTypeDescriptor().getJavaTypeClass().getName() );
}
return gridType;
}
else if ( type instanceof CustomType ) {
CustomType cType = (CustomType) type;
final UserType userType = cType.getUserType();
if ( userType instanceof EnumType ) {
EnumType enumType = (EnumType) userType;
//should we cache that (the key must be enumClass / isOrdinal
return new org.hibernate.ogm.type.impl.EnumType( cType, enumType );
}
//let it go it will eventually fail
}
else if ( type instanceof org.hibernate.type.ComponentType ) {
org.hibernate.type.ComponentType componentType = (org.hibernate.type.ComponentType) type;
return new ComponentType(componentType, this);
}
else if ( type instanceof org.hibernate.type.ManyToOneType ) {
//do some stuff
org.hibernate.type.ManyToOneType manyToOneType = (org.hibernate.type.ManyToOneType) type;
return new ManyToOneType(manyToOneType, this);
}
else if ( type instanceof org.hibernate.type.OneToOneType ) {
//do some stuff
org.hibernate.type.OneToOneType oneToOneType = (org.hibernate.type.OneToOneType) type;
return new OneToOneType(oneToOneType, this);
}
else if ( type instanceof org.hibernate.type.CollectionType ) {
return new CollectionType( (org.hibernate.type.CollectionType) type );
}
throw log.unableToFindGridType( type.getClass().getName() );
}
}