Created
February 29, 2016 00:39
-
-
Save schleumer/b8680c1f503f29cb01cd to your computer and use it in GitHub Desktop.
This file contains 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
package li.ues.hermes.system; | |
import android.database.Cursor; | |
import java.lang.reflect.Field; | |
import java.lang.reflect.Method; | |
import java.util.ArrayList; | |
import java.util.Dictionary; | |
import java.util.Hashtable; | |
import java.util.List; | |
class Tuple3<A, B, C> { | |
public A first; | |
public B second; | |
public C third; | |
public Tuple3(A v1, B v2, C v3) { | |
this.first = v1; | |
this.second = v2; | |
this.third = v3; | |
} | |
} | |
public class CursorEx { | |
Cursor cursor = null; | |
public CursorEx(Cursor cursor) { | |
this.cursor = cursor; | |
} | |
public <T> T getValue(String column, Class<T> t) { | |
return getValue(column, null, t); | |
} | |
public <T> T getValue(String column, T defaultValue, Class<T> t) { | |
Integer index = cursor.getColumnIndex(column); | |
if (index < 0) { | |
return defaultValue; | |
} | |
try { | |
if (t.isAssignableFrom(Long.class)) { | |
Long value = cursor.getLong(index); | |
return t.cast(value); | |
} else if (t.isAssignableFrom(String.class)) { | |
String value = cursor.getString(index); | |
if (value == null) { | |
return defaultValue; | |
} else { | |
return t.cast(value); | |
} | |
} else if (t.isAssignableFrom(Boolean.class)) { | |
Long value = cursor.getLong(index); | |
return t.cast(value > 0); | |
} else { | |
return defaultValue; | |
} | |
} catch (Exception ex) { | |
System.out.println(ex.getMessage()); | |
return defaultValue; | |
} | |
} | |
public String getValue(String column, String defaultValue) { | |
return getValue(column, defaultValue, String.class); | |
} | |
public Long getValue(String column, Long defaultValue) { | |
return getValue(column, defaultValue, Long.class); | |
} | |
public Boolean columnExists(String column) { | |
return cursor.getColumnIndex(column) >= 0; | |
} | |
public boolean moveToNext() { | |
return cursor.moveToNext(); | |
} | |
public void close() { | |
cursor.close(); | |
} | |
public <T> T defaultValueFor(Class<T> clazz) { | |
return clazz.cast("xd"); | |
} | |
public <T> List<T> all(Class<T> clazz) { | |
ArrayList<T> result = new ArrayList<>(); | |
Dictionary<String, Tuple3<String, Class<?>, String>> columnAttribute = new Hashtable<>(); | |
for (Field attr : clazz.getDeclaredFields()) { | |
if (attr.isAnnotationPresent(CursorColumn.class)) { | |
CursorColumn annotation = attr.getAnnotation(CursorColumn.class); | |
columnAttribute.put(attr.getName(), new Tuple3<String, Class<?>, String>(annotation.value(), attr.getType(), annotation.setter())); | |
} | |
} | |
while (cursor.moveToNext()) { | |
try { | |
T obj = clazz.newInstance(); | |
for (Field field : obj.getClass().getFields()) { | |
Tuple3<String, Class<?>, String> signature = columnAttribute.get(field.getName()); | |
if (signature != null && columnExists(signature.first)) { | |
Object value = getValue(signature.first, signature.second); | |
field.set(obj, value); | |
if (signature.third != null && !signature.third.isEmpty() && value != null) { | |
Method method = obj.getClass().getMethod(signature.third, signature.second); | |
method.invoke(obj, value); | |
} | |
} | |
} | |
result.add(obj); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
return result; | |
} | |
public <T> T first(Class<T> clazz) { | |
try { | |
return all(clazz).get(0); | |
} catch (Exception ex) { | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment