Skip to content

Commit

Permalink
add Date support
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Bessonov committed Jul 23, 2011
1 parent 54a37f4 commit aa35426
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 60 deletions.
6 changes: 3 additions & 3 deletions src/main/java/redis/clients/johm/JOhm.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ public static <T> T save(final Object model, boolean saveChildren) {
fieldName = field.getName();
Object fieldValueObject = field.get(model);
if (fieldValueObject != null) {
hashedObject
.put(fieldName, fieldValueObject.toString());
String fieldValue = JOhmUtils.Convertor.object2string(field, fieldValueObject);
hashedObject.put(fieldName, fieldValue);
}

}
Expand Down Expand Up @@ -357,7 +357,7 @@ private static void fillField(final Map<String, String> hashedObject,
field.setAccessible(true);
field.set(
newInstance,
JOhmUtils.Convertor.convert(field,
JOhmUtils.Convertor.string2object(field,
hashedObject.get(field.getName())));
}
if (field.isAnnotationPresent(Reference.class)) {
Expand Down
44 changes: 40 additions & 4 deletions src/main/java/redis/clients/johm/JOhmUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,32 @@
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.SimpleTimeZone;

import redis.clients.johm.collections.RedisList;
import redis.clients.johm.collections.RedisMap;
import redis.clients.johm.collections.RedisSet;
import redis.clients.johm.collections.RedisSortedSet;

public final class JOhmUtils {

static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ", Locale.US);

static {
sdf.setTimeZone(new SimpleTimeZone(0, "GMT"));
}

static String getReferenceKeyName(final Field field) {
return field.getName() + "_id";
}
Expand Down Expand Up @@ -195,11 +207,15 @@ public static enum JOhmCollectionDataType {
}

public final static class Convertor {
static Object convert(final Field field, final String value) {
return convert(field.getType(), value);
static Object string2object(final Field field, final String value) {
return string2object(field.getType(), value);
}

public static String object2string(final Field field, final Object value) {
return object2string(field.getType(), value);
}

public static Object convert(final Class<?> type, final String value) {
public static Object string2object(final Class<?> type, final String value) {
if (type.equals(Byte.class) || type.equals(byte.class)) {
return new Byte(value);
}
Expand Down Expand Up @@ -248,6 +264,17 @@ public static Object convert(final Class<?> type, final String value) {
return new BigInteger(value);
}

if (type.equals(Date.class)) {
if (value == null) {
return null;
}
try {
return sdf.parse(value);
} catch (ParseException e) {
throw new IllegalArgumentException(e);
}
}

if (type.isEnum() || type.equals(Enum.class)) {
// return Enum.valueOf(type, value);
return null; // TODO: handle these
Expand All @@ -265,6 +292,13 @@ public static Object convert(final Class<?> type, final String value) {

return value;
}

public static String object2string(final Class<?> type, final Object value) {
if (Date.class.equals(type)) {
return sdf.format((Date)value);
}
return value.toString();
}
}

static final class Validator {
Expand All @@ -280,7 +314,8 @@ static void checkValidAttribute(final Field field) {
|| type.equals(Boolean.class) || type.equals(boolean.class)
|| type.equals(BigDecimal.class)
|| type.equals(BigInteger.class)
|| type.equals(String.class)) {
|| type.equals(String.class)
|| type.equals(Date.class)) {
} else {
throw new JOhmException(field.getType().getSimpleName()
+ " is not a JOhm-supported Attribute");
Expand Down Expand Up @@ -486,6 +521,7 @@ public static boolean checkSupportedPrimitiveClazz(
JOHM_SUPPORTED_PRIMITIVES.add(boolean.class);
JOHM_SUPPORTED_PRIMITIVES.add(BigDecimal.class);
JOHM_SUPPORTED_PRIMITIVES.add(BigInteger.class);
JOHM_SUPPORTED_PRIMITIVES.add(Date.class);

JOHM_SUPPORTED_ANNOTATIONS.add(Array.class);
JOHM_SUPPORTED_ANNOTATIONS.add(Attribute.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private T get(int index) {
.lindex(index);
if (!JOhmUtils.isNullOrEmpty(key)) {
if (johmElementType == JOhmCollectionDataType.PRIMITIVE) {
element = (T) Convertor.convert(elementClazz, key);
element = (T) Convertor.string2object(elementClazz, key);
} else if (johmElementType == JOhmCollectionDataType.MODEL) {
element = JOhm.<T> get(elementClazz, Integer.valueOf(key));
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/redis/clients/johm/collections/RedisList.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public T get(int index) {
.lindex(index);
if (!JOhmUtils.isNullOrEmpty(key)) {
if (johmElementType == JOhmCollectionDataType.PRIMITIVE) {
element = (T) Convertor.convert(elementClazz, key);
element = (T) Convertor.string2object(elementClazz, key);
} else if (johmElementType == JOhmCollectionDataType.MODEL) {
element = JOhm.<T> get(elementClazz, Integer.valueOf(key));
}
Expand Down Expand Up @@ -252,7 +252,7 @@ private synchronized List<T> scrollElements() {
field.getName()).lrange(0, -1);
for (String key : keys) {
if (johmElementType == JOhmCollectionDataType.PRIMITIVE) {
elements.add((T) Convertor.convert(elementClazz, key));
elements.add((T) Convertor.string2object(elementClazz, key));
} else if (johmElementType == JOhmCollectionDataType.MODEL) {
elements.add((T) JOhm.get(elementClazz, Integer.valueOf(key)));
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/redis/clients/johm/collections/RedisMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public V get(Object key) {

if (!JOhmUtils.isNullOrEmpty(valueKey)) {
if (johmValueType == JOhmCollectionDataType.PRIMITIVE) {
value = (V) Convertor.convert(valueClazz, valueKey);
value = (V) Convertor.string2object(valueClazz, valueKey);
} else if (johmValueType == JOhmCollectionDataType.MODEL) {
value = JOhm.<V> get(valueClazz, Integer.parseInt(valueKey));
}
Expand All @@ -123,7 +123,7 @@ public Set<K> keySet() {
for (String key : nest.cat(JOhmUtils.getId(owner)).cat(field.getName())
.hkeys()) {
if (johmKeyType == JOhmCollectionDataType.PRIMITIVE) {
keys.add((K) JOhmUtils.Convertor.convert(keyClazz, key));
keys.add((K) JOhmUtils.Convertor.string2object(keyClazz, key));
} else if (johmKeyType == JOhmCollectionDataType.MODEL) {
keys.add(JOhm.<K> get(keyClazz, Integer.parseInt(key)));
}
Expand Down Expand Up @@ -204,21 +204,21 @@ private synchronized Map<K, V> scrollElements() {
for (Map.Entry<String, String> entry : savedHash.entrySet()) {
if (johmKeyType == JOhmCollectionDataType.PRIMITIVE
&& johmValueType == JOhmCollectionDataType.PRIMITIVE) {
savedKey = (K) JOhmUtils.Convertor.convert(keyClazz, entry
savedKey = (K) JOhmUtils.Convertor.string2object(keyClazz, entry
.getKey());
savedValue = (V) JOhmUtils.Convertor.convert(valueClazz, entry
savedValue = (V) JOhmUtils.Convertor.string2object(valueClazz, entry
.getValue());
} else if (johmKeyType == JOhmCollectionDataType.PRIMITIVE
&& johmValueType == JOhmCollectionDataType.MODEL) {
savedKey = (K) JOhmUtils.Convertor.convert(keyClazz, entry
savedKey = (K) JOhmUtils.Convertor.string2object(keyClazz, entry
.getKey());
savedValue = JOhm.<V> get(valueClazz, Integer.parseInt(entry
.getValue()));
} else if (johmKeyType == JOhmCollectionDataType.MODEL
&& johmValueType == JOhmCollectionDataType.PRIMITIVE) {
savedKey = JOhm.<K> get(keyClazz, Integer.parseInt(entry
.getKey()));
savedValue = (V) JOhmUtils.Convertor.convert(valueClazz, entry
savedValue = (V) JOhmUtils.Convertor.string2object(valueClazz, entry
.getValue());
} else if (johmKeyType == JOhmCollectionDataType.MODEL
&& johmValueType == JOhmCollectionDataType.MODEL) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/redis/clients/johm/collections/RedisSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,11 @@ private synchronized Set<T> scrollElements() {
Set<T> elements = new HashSet<T>();
for (String key : keys) {
if (johmElementType == JOhmCollectionDataType.PRIMITIVE) {
elements.add((T) Convertor.convert(elementClazz, key));
elements.add((T) Convertor.string2object(elementClazz, key));
} else if (johmElementType == JOhmCollectionDataType.MODEL) {
elements.add((T) JOhm.get(elementClazz, Integer.valueOf(key)));
}
}
return elements;
}
}
}
Loading

0 comments on commit aa35426

Please sign in to comment.