Last active
August 29, 2015 14:04
-
-
Save showsky/b8f60b6f81af6d95dd22 to your computer and use it in GitHub Desktop.
Converting between virtual and actual column names, using a small utility class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class ColumnDef { | |
| public static enum Type { | |
| BOOLEAN, BYTE, BYTEARRAY, DOUBLE, FLOAT, INTEGER, LONG, SHORT, STRING | |
| }; | |
| private final String name; | |
| private final Type type; | |
| public ColumnDef(String name, Type type) { | |
| this.name = name; | |
| this.type = type; | |
| } | |
| public void copy(String srcCol, ContentValues src, ContentValues dst) { | |
| switch (type) { | |
| case BOOLEAN: | |
| dst.put(name, src.getAsBoolean(srcCol)); | |
| break; | |
| case BYTE: | |
| dst.put(name, src.getAsByte(srcCol)); | |
| break; | |
| case BYTEARRAY: | |
| dst.put(name, src.getAsByteArray(srcCol)); | |
| break; | |
| case DOUBLE: | |
| dst.put(name, src.getAsDouble(srcCol)); | |
| break; | |
| case FLOAT: | |
| dst.put(name, src.getAsFloat(srcCol)); | |
| break; | |
| case INTEGER: | |
| dst.put(name, src.getAsInteger(srcCol)); | |
| break; | |
| case LONG: | |
| dst.put(name, src.getAsLong(srcCol)); | |
| break; | |
| case SHORT: | |
| dst.put(name, src.getAsShort(srcCol)); | |
| break; | |
| case STRING: | |
| dst.put(name, src.getAsString(srcCol)); | |
| break; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private static final Map<String, ColumnDef> COL_MAP; | |
| static { | |
| Map<String, ColumnDef> m = new HashMap<String, ColumnDef>(); | |
| m.put( | |
| KeyValContract.Columns.KEY, | |
| new ColumnDef(KeyValHelper.COL_KEY, ColumnDef.Type.STRING) | |
| ); | |
| m.put( | |
| KeyValContract.Columns.VAL, | |
| new ColumnDef(KeyValHelper.COL_VAL, ColumnDef.Type.STRING) | |
| ); | |
| COL_MAP = Collections.unmodifiableMap(m); | |
| } | |
| // code omitted... | |
| private ContentValues translateCols(ContentValues vals) { | |
| ContentValues newVals = new ContentValues(); | |
| for (String colName: vals.keySet()) { | |
| ColumnDef colDef = COL_MAP.get(colName); | |
| if (null == colDef) { | |
| throw new IllegalArgumentException( "Unrecognized column: " + colName); | |
| } | |
| } | |
| colDef.copy(colName, vals, newVals); } | |
| return newVals; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment