DATAREDIS-503 - Polishing.

Rename ConvertingHashMapper to ObjectHashMapper. Add fromHash with type to avoid casting. Add JavaDoc documentation to HashMapper. Add hash mapping to reference documentation.

Original pull request: #194.
This commit is contained in:
Mark Paluch
2016-04-29 16:42:08 +02:00
parent 359c3e4533
commit c1476b701b
5 changed files with 235 additions and 123 deletions

View File

@@ -1,12 +1,12 @@
/*
* Copyright 2011-2013 the original author or authors.
*
* Copyright 2011-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -20,12 +20,28 @@ import java.util.Map;
/**
* Core mapping contract between Java types and Redis hashes/maps. It's up to the implementation to support nested
* objects.
*
*
* @param <T> Object type
* @param <K> Redis Hash field type
* @param <V> Redis Hash value type
* @author Costin Leau
* @author Mark Paluch
*/
public interface HashMapper<T, K, V> {
/**
* Convert an {@code object} to a map that can be used with Redis hashes.
*
* @param object
* @return
*/
Map<K, V> toHash(T object);
/**
* Convert a {@code hash} (map) to an object.
*
* @param hash
* @return
*/
T fromHash(Map<K, V> hash);
}

View File

@@ -30,7 +30,7 @@ import org.springframework.data.redis.core.mapping.RedisMappingContext;
import org.springframework.data.util.TypeInformation;
/**
* {@link HashMapper} based on {@link MappingRedisConverter}. Does supports nested properties and simple types like
* {@link HashMapper} based on {@link MappingRedisConverter}. Supports nested properties and simple types like
* {@link String}.
*
* <pre>
@@ -38,6 +38,7 @@ import org.springframework.data.util.TypeInformation;
* class Person {
*
* String firstname;
* String lastname;
*
* List&lt;String&gt; nicknames;
* List&lt;Person&gt; coworkers;
@@ -62,25 +63,26 @@ import org.springframework.data.util.TypeInformation;
* </pre>
*
* @author Christoph Strobl
* @author Mark Paluch
* @since 1.8
*/
public class ConvertingHashMapper implements HashMapper<Object, byte[], byte[]> {
public class ObjectHashMapper implements HashMapper<Object, byte[], byte[]> {
private final MappingRedisConverter converter;
/**
* Creates new {@link ConvertingHashMapper}.
* Creates new {@link ObjectHashMapper}.
*/
public ConvertingHashMapper() {
public ObjectHashMapper() {
this(new CustomConversions());
}
/**
* Creates new {@link ConvertingHashMapper}.
* Creates new {@link ObjectHashMapper}.
*
* @param customConversions can be {@literal null}.
*/
public ConvertingHashMapper(CustomConversions customConversions) {
public ObjectHashMapper(CustomConversions customConversions) {
MappingRedisConverter mappingConverter = new MappingRedisConverter(new RedisMappingContext(),
new NoOpIndexResolver(), new NoOpReferenceResolver());
@@ -120,6 +122,18 @@ public class ConvertingHashMapper implements HashMapper<Object, byte[], byte[]>
return converter.read(Object.class, new RedisData(hash));
}
/**
* Convert a {@code hash} (map) to an object and return the casted result.
*
* @param hash
* @param type
* @param <T>
* @return
*/
public <T> T fromHash(Map<byte[], byte[]> hash, Class<T> type) {
return (T) fromHash(hash);
}
/**
* {@link ReferenceResolver} implementation always returning an empty {@link Map}.
*