Android Open Source - Android-Lib-Database Row Mapper






From Project

Back to project page Android-Lib-Database.

License

The source code is released under:

Apache License

If you think the Android project Android-Lib-Database listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package android.lib.database;
/*  w w w.j  a  v  a  2 s. co m*/
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Set;

import android.database.Cursor;
import android.text.TextUtils;

/**
 * Maps a {@link android.database.Cursor} to an instance of <code>T</code>.
 * @param <T> the type of instance to map.
 */
public class RowMapper<T> {
    protected final Class<T> clazz;

    /**
     * Creates a new instance of {@link RowMapper} with the given class type.
     * @param clazz the class of the type to map.
     */
    public RowMapper(final Class<T> clazz) {
        this.clazz = clazz;
    }

    /**
     * Maps a {@link android.database.Cursor} to an instance of <code>T</code>.
     * @param cursor the {@link android.database.Cursor} to map.
     * @return an instance of <code>T</code>.
     * @throws IllegalAccessException if the if the default constructor of <code>T</code> is not visible,
     * or the default constructor of the {@link TypeConverter} associated with any field of <code>T</code> is not visible.
     * @throws InstantiationException if the instance of <code>T</code> can not be created,
     * or the instance of the {@link TypeConverter} associated with any field of <code>T</code> can not be created.
     * @throws UnsupportedTypeException if the type of any field is not SQLite-compatible and
     * no {@link TypeConverter} is associated with the field or the {@link TypeConverter} associated
     * failed to convert the type of the field.
     * @throws IllegalArgumentException if the column name is not found from the given <code>cursor</code>.
     */
    public final T mapRow(final Cursor cursor)
                    throws IllegalAccessException,
                    InstantiationException,
                    IllegalArgumentException,
                    UnsupportedTypeException {
        final T          t      = this.clazz.newInstance();
        final Set<Field> fields = new HashSet<Field>();

        for (final Field field : this.clazz.getFields()) {
            fields.add(field);
        }

        for (final Field field : this.clazz.getDeclaredFields()) {
            fields.add(field);
        }

        for (final Field field : fields) {
            field.setAccessible(true);

            final Column column = field.getAnnotation(Column.class);

            if (column != null) {
                field.set(t, RowMapper.getColumnValue(cursor, field));
            }
        }

        return t;
    }

    private static Object getColumnValue(final Cursor cursor, final Field field)
                    throws UnsupportedTypeException,
                    IllegalAccessException,
                    InstantiationException {
        final Column       column    = field.getAnnotation(Column.class);
        final String       name      = TextUtils.isEmpty(column.value()) ? field.getName() : column.value();
        final Class<?>     type      = field.getType();
        final UseConverter converter = field.getAnnotation(UseConverter.class);

        if (converter == null) {
            if (type.equals(boolean.class) || type.equals(Boolean.class)) {
                return Boolean.valueOf(cursor.getInt(cursor.getColumnIndexOrThrow(name)) == 1);
            } else if (type.equals(byte.class) || type.equals(Byte.class)) {
                return Byte.valueOf((byte)cursor.getInt(cursor.getColumnIndexOrThrow(name)));
            } else if (type.equals(byte[].class) || type.equals(Byte[].class)) {
                return cursor.getBlob(cursor.getColumnIndexOrThrow(name));
            } else if (type.equals(double.class) || type.equals(Double.class)) {
                return Double.valueOf(cursor.getDouble(cursor.getColumnIndexOrThrow(name)));
            } else if (type.equals(float.class) || type.equals(Float.class)) {
                return Float.valueOf(cursor.getFloat(cursor.getColumnIndexOrThrow(name)));
            } else if (type.equals(int.class) || type.equals(Integer.class)) {
                return Integer.valueOf(cursor.getInt(cursor.getColumnIndexOrThrow(name)));
            } else if (type.equals(long.class) || type.equals(Long.class)) {
                return Long.valueOf(cursor.getLong(cursor.getColumnIndexOrThrow(name)));
            } else if (type.equals(short.class) || type.equals(Short.class)) {
                return Short.valueOf(cursor.getShort(cursor.getColumnIndexOrThrow(name)));
            } else if (type.equals(String.class)) {
                return cursor.getString(cursor.getColumnIndexOrThrow(name));
            } else {
                throw new UnsupportedTypeException(String.format("%s is not of SQLite-compatible type", field.getName())); //$NON-NLS-1$
            }
        }

        final Class<?> fromType = converter.type();

        if (fromType.equals(boolean.class) || fromType.equals(Boolean.class)) {
            return ((TypeConverter<Boolean, ?>)converter.value().newInstance())
                            .fromDatabase(Boolean.valueOf(cursor.getInt(cursor.getColumnIndexOrThrow(name)) == 1));
        } else if (fromType.equals(byte.class) || fromType.equals(Byte.class)) {
            return ((TypeConverter<Byte, ?>)converter.value().newInstance())
                            .fromDatabase(Byte.valueOf((byte)cursor.getInt(cursor.getColumnIndexOrThrow(name))));
        } else if (fromType.equals(byte[].class)) {
            return ((TypeConverter<byte[], ?>)converter.value().newInstance())
                            .fromDatabase(cursor.getBlob(cursor.getColumnIndexOrThrow(name)));
        } else if (fromType.equals(Byte[].class)) {
            final byte[] value  = cursor.getBlob(cursor.getColumnIndexOrThrow(name));
            final Byte[] object = new Byte[value.length];

            for (int i = value.length; --i >= 0;) {
                object[i] = Byte.valueOf(value[i]);
            }

            return ((TypeConverter<Byte[], ?>)converter.value().newInstance())
                            .fromDatabase(object);
        } else if (fromType.equals(double.class) || fromType.equals(Double.class)) {
            return ((TypeConverter<Double, ?>)converter.value().newInstance())
                            .fromDatabase(Double.valueOf(cursor.getDouble(cursor.getColumnIndexOrThrow(name))));
        } else if (fromType.equals(float.class) || fromType.equals(Float.class)) {
            return ((TypeConverter<Float, ?>)converter.value().newInstance())
                            .fromDatabase(Float.valueOf(cursor.getFloat(cursor.getColumnIndexOrThrow(name))));
        } else if (fromType.equals(int.class) || fromType.equals(Integer.class)) {
            return ((TypeConverter<Integer, ?>)converter.value().newInstance())
                            .fromDatabase(Integer.valueOf(cursor.getInt(cursor.getColumnIndexOrThrow(name))));
        } else if (fromType.equals(long.class) || fromType.equals(Long.class)) {
            return ((TypeConverter<Long, ?>)converter.value().newInstance())
                            .fromDatabase(Long.valueOf(cursor.getLong(cursor.getColumnIndexOrThrow(name))));
        } else if (fromType.equals(short.class) || fromType.equals(Short.class)) {
            return ((TypeConverter<Short, ?>)converter.value().newInstance())
                            .fromDatabase(Short.valueOf(cursor.getShort(cursor.getColumnIndexOrThrow(name))));
        } else if (fromType.equals(String.class)) {
            return ((TypeConverter<String, ?>)converter.value().newInstance())
                            .fromDatabase(cursor.getString(cursor.getColumnIndexOrThrow(name)));
        } else {
            throw new UnsupportedTypeException(String.format("%s is not of SQLite-compatible type", field.getName())); //$NON-NLS-1$
        }
    }
}




Java Source Code List

android.lib.database.Column.java
android.lib.database.CompositeIndex.java
android.lib.database.DatabaseOpenHelper.java
android.lib.database.Database.java
android.lib.database.DateConverter.java
android.lib.database.Index.java
android.lib.database.JSONRowMapper.java
android.lib.database.RowMapper.java
android.lib.database.Table.java
android.lib.database.TypeConverter.java
android.lib.database.UniqueCompositeIndex.java
android.lib.database.UnsupportedTypeException.java
android.lib.database.UseConverter.java
android.lib.database.predicate.ManySidedPredicate.java
android.lib.database.predicate.Predicate.java
android.lib.database.predicate.ThreeSidedPredicate.java
android.lib.database.predicate.TwoSidedPredicate.java
android.lib.database.query.Delete.java
android.lib.database.query.Insert.java
android.lib.database.query.QueryBuilder.java
android.lib.database.query.Query.java
android.lib.database.query.Select.java
android.lib.database.query.Update.java