Skip to content

Commit

Permalink
Changed TimestampIdGenerator.java to use numeric timestamp by default…
Browse files Browse the repository at this point in the history
…, but still supports ISO standard date format

Signed-off-by: gburgett <gordon.burgett@gmail.com>
  • Loading branch information
gburgett committed Apr 17, 2013
1 parent 8003bf8 commit 1ec03dc
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions java/XFlat/src/org/xflatdb/xflat/db/TimestampIdGenerator.java
Expand Up @@ -87,18 +87,17 @@ public String idToString(Object id) {
if(String.class.equals(clazz)){
return (String)id;
}
Date ret;
if(Date.class.equals(clazz)){
ret = (Date)id;
}
else if(Long.class.equals(clazz)){
ret = new Date((Long)id);

if(Long.class.equals(clazz)){
return ((Long)id).toString();
}
else{
throw new UnsupportedOperationException("Unknown ID type " + id.getClass());

if(Date.class.equals(clazz)){
Date ret = (Date)id;
return Long.toString(ret.getTime());
}

return format.get().format(ret);
throw new UnsupportedOperationException("Unknown ID type " + id.getClass());
}

@Override
Expand All @@ -108,23 +107,30 @@ public Object stringToId(String id, Class<?> idType) {
return id;
}

Date date;
if(id == null){
date = new Date(0);
if(id == null)
return null;

Long timestamp;
try{
timestamp = Long.valueOf(id);
}
else{
catch(NumberFormatException ex){
//try parsing as a date
try {
date = format.get().parse(id);
} catch (ParseException ex) {
date = new Date(0);
Date date = format.get().parse(id);
timestamp = date.getTime();
} catch (ParseException ex2) {
//can't parse, return null;
return null;
}
}

if(Date.class.equals(idType)){
return date;
if(idType.isAssignableFrom(Long.class)){
return timestamp;
}
if(Long.class.equals(idType)){
return date.getTime();

if(idType.isAssignableFrom(Date.class)){
return new Date(timestamp);
}

throw new UnsupportedOperationException("Unknown ID type " + idType);
Expand Down

0 comments on commit 1ec03dc

Please sign in to comment.