Restore double property

pull/281/head
Alex Baker 9 years ago
parent 5af1883562
commit 5822ccc761

@ -25,6 +25,8 @@ import java.util.Arrays;
import java.util.HashMap;
import java.util.Map.Entry;
import static com.todoroo.andlib.data.Property.DoubleProperty;
/**
* <code>AbstractModel</code> represents a row in a database.
* <p>
@ -219,6 +221,8 @@ public abstract class AbstractModel implements Parcelable, Cloneable {
return (TYPE) Integer.valueOf((String) value);
} else if(value instanceof Integer && property instanceof LongProperty) {
return (TYPE) Long.valueOf(((Number) value).longValue());
} else if(value instanceof String && property instanceof DoubleProperty) {
return (TYPE) Double.valueOf((String) value);
}
return (TYPE) value;
} catch (NumberFormatException e) {
@ -473,6 +477,12 @@ public abstract class AbstractModel implements Parcelable, Cloneable {
return null;
}
@Override
public Void visitDouble(Property<Double> property, Object value) {
store.put(property.getColumnName(), (Double) value);
return null;
}
@Override
public Void visitString(Property<String> property, Object value) {
store.put(property.getColumnName(), (String) value);

@ -128,6 +128,8 @@ public abstract class Property<TYPE> extends Field implements Cloneable {
public RETURN visitLong(Property<Long> property, PARAMETER data);
public RETURN visitDouble(Property<Double> property, PARAMETER data);
public RETURN visitString(Property<String> property, PARAMETER data);
}
@ -214,6 +216,39 @@ public abstract class Property<TYPE> extends Field implements Cloneable {
}
}
/**
* Double property type. See {@link Property}
*
* @author Tim Su <tim@todoroo.com>
*
*/
public static class DoubleProperty extends Property<Double> {
public DoubleProperty(Table table, String name) {
super(table, name);
}
public DoubleProperty(Table table, String name, int flags) {
super(table, name, flags);
}
protected DoubleProperty(Table table, String name, String expression) {
super(table, name, expression);
}
@Override
public <RETURN, PARAMETER> RETURN accept(
PropertyVisitor<RETURN, PARAMETER> visitor, PARAMETER data) {
return visitor.visitDouble(this, data);
}
@Override
public DoubleProperty cloneAs(String tableAlias, String columnAlias) {
return (DoubleProperty) super.cloneAs(tableAlias, columnAlias);
}
}
/**
* Long property type. See {@link Property}
*

@ -8,6 +8,11 @@ package com.todoroo.andlib.data;
*/
public class SqlConstructorVisitor implements Property.PropertyVisitor<String, Void> {
@Override
public String visitDouble(Property<Double> property, Void data) {
return String.format("%s REAL", property.getColumnName());
}
@Override
public String visitInteger(Property<Integer> property, Void data) {
return String.format("%s INTEGER", property.getColumnName());

@ -103,6 +103,15 @@ public class TodorooCursor<TYPE extends AbstractModel> extends CursorWrapper {
return cursor.getLong(column);
}
@Override
public Object visitDouble(Property<Double> property, TodorooCursor<?> cursor) {
int column = columnIndex(property, cursor);
if (property.checkFlag(Property.PROP_FLAG_NULLABLE) && cursor.isNull(column)) {
return null;
}
return cursor.getDouble(column);
}
@Override
public Object visitString(Property<String> property,
TodorooCursor<?> cursor) {

@ -289,6 +289,24 @@ public class TasksXmlExporter {
return null;
}
@Override
public Void visitDouble(Property<Double> property, AbstractModel data) {
try {
Double value = data.getValue(property);
String valueString = (value == null) ? XML_NULL : value.toString();
xml.attribute(null, property.name, valueString);
} catch (UnsupportedOperationException e) {
// didn't read this value, do nothing
} catch (IllegalArgumentException e) {
throw new RuntimeException(e);
} catch (IllegalStateException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
return null;
}
@Override
public Void visitString(Property<String> property, AbstractModel data) {
try {

@ -337,6 +337,16 @@ public class TasksXmlImporter {
return null;
}
@Override
public Void visitDouble(Property<Double> property, AbstractModel data) {
String value = xpp.getAttributeValue(null, property.name);
if (value != null) {
data.setValue(property, TasksXmlExporter.XML_NULL.equals(value) ?
null : Double.parseDouble(value));
}
return null;
}
@Override
public Void visitString(Property<String> property,
AbstractModel data) {

Loading…
Cancel
Save