Android Open Source - adme Enum String A D M E Serializer






From Project

Back to project page adme.

License

The source code is released under:

Apache License

If you think the Android project adme 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 com.danielesegato.adme.db.serializer;
/*from  ww w .ja  v  a 2 s  .  c o m*/
import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;

import com.danielesegato.adme.config.ADMEFieldConfig;
import com.danielesegato.adme.config.SQLiteType;

import java.lang.reflect.Field;

/**
 * Persist an {@link java.lang.Enum} in the database, as string. This storage data type is less
 * performance-efficient then storing integers but provide better flexibility in developing and upgrading
 * your enum because you do not have to worry about their order, you just have to avoid renaming them.
 *
 * This is the default configured way of storing an Enum in the database but you can override it with
 * your own {@link com.danielesegato.adme.db.ADMESerializer}.
 *
 * @see com.danielesegato.adme.db.serializer.EnumIntADMESerializer
 * @see com.danielesegato.adme.ADME#registerADMESerializer(Class, com.danielesegato.adme.db.ADMESerializer)
 */
public class EnumStringADMESerializer extends BaseADMESerializer {
    private static final String LOG_TAG = EnumStringADMESerializer.class.getSimpleName();
    private static EnumStringADMESerializer singleton = new EnumStringADMESerializer();

    public static EnumStringADMESerializer getSingleton() {
        return singleton;
    }

    @Override
    public SQLiteType getSQLiteType() {
        return SQLiteType.TEXT;
    }

    @Override
    public Object sqlToJava(Cursor cursor, int columnPos, ADMEFieldConfig fieldConfig) {
        Field field = getEnumFieldOrThrow(fieldConfig);
        if (cursor.isNull(columnPos)) {
            return null;
        }
        String enumName = cursor.getString(columnPos);
        return getEnumByName(field, enumName, fieldConfig);
    }

    @Override
    public String stringToSqlRaw(String val, ADMEFieldConfig fieldConfig) {
        Enum<?> enumByName = getEnumByName(getEnumFieldOrThrow(fieldConfig), val, fieldConfig);
        return String.format("'%s'", enumByName.name().replaceAll("'", "\\'"));
    }

    @Override
    public void storeInContentValues(String key, ContentValues values, Object fieldValue, ADMEFieldConfig fieldConfig) throws IllegalArgumentException {
        Field field = getEnumFieldOrThrow(fieldConfig);
        if (fieldValue != null && !(fieldValue instanceof Enum)) {
            throw new IllegalArgumentException(String.format("Field value for entity %s field %s can't be considered an Enum for key %s: %s (%s)",
                    fieldConfig.getADMEEntityConfig().getEntityName(),
                    fieldConfig.getColumnName(),
                    key,
                    fieldValue,
                    fieldValue.getClass()));
        }
        if (fieldValue != null) {
            Enum<?> checkedEnum = getEnumByName(field, ((Enum<?>)fieldValue).name(), fieldConfig);
            values.put(key, checkedEnum.ordinal());
        } else {
            values.putNull(key);
        }
    }

    private Field getEnumFieldOrThrow(ADMEFieldConfig fieldConfig) {
        Field field = fieldConfig.getJavaField();
        if (!field.isEnumConstant()) {
            throw new IllegalArgumentException(String.format("Field %s (%s) for entity %s is not an Enum",
                    field.getName(), field, fieldConfig.getADMEEntityConfig().getEntityName()));
        }
        return field;
    }

    private Enum<?> getEnumByName(Field field, String enumName, ADMEFieldConfig fieldConfig) {
        try {
            return Enum.valueOf((Class<? extends Enum>)field.getType(), enumName);
        } catch (IllegalArgumentException notFound) {
            if (fieldConfig.getFallbackEnumName() == null || fieldConfig.getFallbackEnumName().length() == 0) {
                throw notFound;
            }
            Enum<?> fallbackEnum = null;
            try {
                fallbackEnum = Enum.valueOf((Class<? extends Enum>)field.getType(), fieldConfig.getFallbackEnumName());
            } catch (IllegalArgumentException notFoundUnknown) {
                throw new IllegalArgumentException(String.format("Unknown enum for name %s in field %s for entity %s, configured fallback replacement '%s' not found for enum %s",
                        enumName, field.getName(), fieldConfig.getADMEEntityConfig().getEntityName(), fieldConfig.getFallbackEnumName(), field.getType()));
            }
            if (Log.isLoggable(LOG_TAG, Log.DEBUG)) {
                Log.d(LOG_TAG, String.format("Unknown enum name %s in field %s for entity %s, replacing with configured fallback %d (%s)",
                        enumName, field.getName(), fieldConfig.getADMEEntityConfig().getEntityName(), fallbackEnum.ordinal(), fallbackEnum.name()));
            }
            return fallbackEnum;
        }
    }
}




Java Source Code List

com.danielesegato.adme.ADME.java
com.danielesegato.adme.annotation.ADMEEntity.java
com.danielesegato.adme.annotation.ADMEField.java
com.danielesegato.adme.annotation.ADMEIndexConstraint.java
com.danielesegato.adme.config.ADMEConfigUtils.java
com.danielesegato.adme.config.ADMEEntityConfig.java
com.danielesegato.adme.config.ADMEFieldConfig.java
com.danielesegato.adme.config.ADMEIndexConstraintConfig.java
com.danielesegato.adme.config.OnForeignUpdateDelete.java
com.danielesegato.adme.config.SQLiteType.java
com.danielesegato.adme.db.ADMESerializerMapping.java
com.danielesegato.adme.db.ADMESerializer.java
com.danielesegato.adme.db.ContentProviderUris.java
com.danielesegato.adme.db.SQLiteContentProvider.java
com.danielesegato.adme.db.serializer.BaseADMESerializer.java
com.danielesegato.adme.db.serializer.BigDecimalADMESerializer.java
com.danielesegato.adme.db.serializer.BooleanADMESerializer.java
com.danielesegato.adme.db.serializer.BooleanObjectADMESerializer.java
com.danielesegato.adme.db.serializer.CurrencyADMESerializer.java
com.danielesegato.adme.db.serializer.DateAsStringADMESerializer.java
com.danielesegato.adme.db.serializer.DateAsTimestampADMESerializer.java
com.danielesegato.adme.db.serializer.DoubleADMESerializer.java
com.danielesegato.adme.db.serializer.DoubleObjectADMESerializer.java
com.danielesegato.adme.db.serializer.EnumIntADMESerializer.java
com.danielesegato.adme.db.serializer.EnumStringADMESerializer.java
com.danielesegato.adme.db.serializer.IntADMESerializer.java
com.danielesegato.adme.db.serializer.IntObjectADMESerializer.java
com.danielesegato.adme.db.serializer.LongADMESerializer.java
com.danielesegato.adme.db.serializer.LongObjectADMESerializer.java
com.danielesegato.adme.db.serializer.StringADMESerializer.java
com.danielesegato.adme.provider.ADMEContentProviderComponent.java
com.danielesegato.adme.provider.ADMEContentProvider.java
com.danielesegato.adme.utils.DateHelper.java
com.danielesegato.adme.utils.SQLStringHelper.java
com.danielesegato.adme.utils.SQLiteScriptParser.java
com.danielesegato.demo.adme.ADMEDemoMainActivity.java
com.danielesegato.demo.adme.NavigationDrawerFragment.java