DATACMNS-485 - API improvements to in TypeInformationMapper area.

The default implementations of the TypeInformation Mapper interfaces now work with ClassTypeInformation where possible to express they only map raw types effectively.

Added TypeInformation.getRawTypeInformation() to easily turn whatever TypeInformation into the raw type representation. Changed ClassTypeInformation.from(…) to return ClassTypeInformations directly.
This commit is contained in:
Oliver Gierke
2014-04-15 07:41:18 +02:00
parent 64caad04a3
commit 165c0595e3
6 changed files with 66 additions and 41 deletions

View File

@@ -40,18 +40,18 @@ import org.springframework.util.ClassUtils;
@SuppressWarnings({ "unchecked", "rawtypes" })
public class ClassTypeInformation<S> extends TypeDiscoverer<S> {
public static final TypeInformation<Collection> COLLECTION = new ClassTypeInformation(Collection.class);
public static final TypeInformation<List> LIST = new ClassTypeInformation(List.class);
public static final TypeInformation<Set> SET = new ClassTypeInformation(Set.class);
public static final TypeInformation<Map> MAP = new ClassTypeInformation(Map.class);
public static final TypeInformation<Object> OBJECT = new ClassTypeInformation(Object.class);
public static final ClassTypeInformation<Collection> COLLECTION = new ClassTypeInformation(Collection.class);
public static final ClassTypeInformation<List> LIST = new ClassTypeInformation(List.class);
public static final ClassTypeInformation<Set> SET = new ClassTypeInformation(Set.class);
public static final ClassTypeInformation<Map> MAP = new ClassTypeInformation(Map.class);
public static final ClassTypeInformation<Object> OBJECT = new ClassTypeInformation(Object.class);
private static final Map<Class<?>, Reference<TypeInformation<?>>> CACHE = Collections
.synchronizedMap(new WeakHashMap<Class<?>, Reference<TypeInformation<?>>>());
private static final Map<Class<?>, Reference<ClassTypeInformation<?>>> CACHE = Collections
.synchronizedMap(new WeakHashMap<Class<?>, Reference<ClassTypeInformation<?>>>());
static {
for (TypeInformation<?> info : Arrays.asList(COLLECTION, LIST, SET, MAP, OBJECT)) {
CACHE.put(info.getType(), new WeakReference<TypeInformation<?>>(info));
for (ClassTypeInformation<?> info : Arrays.asList(COLLECTION, LIST, SET, MAP, OBJECT)) {
CACHE.put(info.getType(), new WeakReference<ClassTypeInformation<?>>(info));
}
}
@@ -64,19 +64,19 @@ public class ClassTypeInformation<S> extends TypeDiscoverer<S> {
* @param type must not be {@literal null}.
* @return
*/
public static <S> TypeInformation<S> from(Class<S> type) {
public static <S> ClassTypeInformation<S> from(Class<S> type) {
Assert.notNull(type, "Type must not be null!");
Reference<TypeInformation<?>> cachedReference = CACHE.get(type);
Reference<ClassTypeInformation<?>> cachedReference = CACHE.get(type);
TypeInformation<?> cachedTypeInfo = cachedReference == null ? null : cachedReference.get();
if (cachedTypeInfo != null) {
return (TypeInformation<S>) cachedTypeInfo;
return (ClassTypeInformation<S>) cachedTypeInfo;
}
TypeInformation<S> result = new ClassTypeInformation<S>(type);
CACHE.put(type, new WeakReference<TypeInformation<?>>(result));
ClassTypeInformation<S> result = new ClassTypeInformation<S>(type);
CACHE.put(type, new WeakReference<ClassTypeInformation<?>>(result));
return result;
}
@@ -116,6 +116,15 @@ public class ClassTypeInformation<S> extends TypeDiscoverer<S> {
return type;
}
/*
* (non-Javadoc)
* @see org.springframework.data.util.TypeDiscoverer#getRawTypeInformation()
*/
@Override
public ClassTypeInformation<?> getRawTypeInformation() {
return this;
}
/*
* (non-Javadoc)
* @see org.springframework.data.util.TypeDiscoverer#isAssignableFrom(org.springframework.data.util.TypeInformation)

View File

@@ -270,6 +270,15 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
return this.resolvedType;
}
/*
* (non-Javadoc)
* @see org.springframework.data.util.TypeInformation#getRawTypeInformation()
*/
@Override
public ClassTypeInformation<?> getRawTypeInformation() {
return ClassTypeInformation.from(getType()).getRawTypeInformation();
}
/*
* (non-Javadoc)
* @see org.springframework.data.util.TypeInformation#getActualType()

View File

@@ -82,6 +82,14 @@ public interface TypeInformation<S> {
*/
Class<S> getType();
/**
* Returns a {@link ClassTypeInformation} to represent the {@link TypeInformation} of the raw type of the current
* instance.
*
* @return
*/
ClassTypeInformation<?> getRawTypeInformation();
/**
* Transparently returns the {@link java.util.Map} value type if the type is a {@link java.util.Map}, returns the
* component type if the type {@link #isCollectionLike()} or the simple type if none of this applies.