Android Open Source - android-contentprovider-generator Field






From Project

Back to project page android-contentprovider-generator.

License

The source code is released under:

GNU General Public License

If you think the Android project android-contentprovider-generator 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

/*
 * This source is part of the//  www .  j  a va 2  s.  com
 *      _____  ___   ____
 *  __ / / _ \/ _ | / __/___  _______ _
 * / // / , _/ __ |/ _/_/ _ \/ __/ _ `/
 * \___/_/|_/_/ |_/_/ (_)___/_/  \_, /
 *                              /___/
 * repository.
 *
 * Copyright (C) 2012-2014 Benoit 'BoD' Lubek (BoD@JRAF.org)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jraf.androidcontentprovidergenerator.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import org.apache.commons.lang.WordUtils;

public class Field {
    public static class Json {
        public static final String NAME = "name";
        public static final String TYPE = "type";
        public static final String DOCUMENTATION = "documentation";
        public static final String INDEX = "index";
        public static final String NULLABLE = "nullable";
        public static final String DEFAULT_VALUE = "defaultValue";
        public static final String DEFAULT_VALUE_LEGACY = "default_value";
        public static final String ENUM_NAME = "enumName";
        public static final String ENUM_VALUES = "enumValues";
        public static final String FOREIGN_KEY = "foreignKey";
        public static final String FOREIGN_KEY_TABLE = "table";
        public static final String FOREIGN_KEY_ON_DELETE_ACTION = "onDelete";

        private static final String TYPE_STRING = "String";
        private static final String TYPE_INTEGER = "Integer";
        private static final String TYPE_LONG = "Long";
        private static final String TYPE_FLOAT = "Float";
        private static final String TYPE_DOUBLE = "Double";
        private static final String TYPE_BOOLEAN = "Boolean";
        private static final String TYPE_DATE = "Date";
        private static final String TYPE_BYTE_ARRAY = "byte[]";
        private static final String TYPE_ENUM = "enum";

        private static final String ON_DELETE_ACTION_NO_ACTION = "NO ACTION";
        private static final String ON_DELETE_ACTION_RESTRICT = "RESTRICT";
        private static final String ON_DELETE_ACTION_SET_NULL = "SET NULL";
        private static final String ON_DELETE_ACTION_SET_DEFAULT = "SET DEFAULT";
        private static final String ON_DELETE_ACTION_CASCADE = "CASCADE";
    }

    public static enum Type {
        // @formatter:off
        STRING(Json.TYPE_STRING, "TEXT", String.class, String.class),
        INTEGER(Json.TYPE_INTEGER, "INTEGER", Integer.class, int.class),
        LONG(Json.TYPE_LONG, "INTEGER", Long.class, long.class),
        FLOAT(Json.TYPE_FLOAT, "REAL", Float.class, float.class),
        DOUBLE(Json.TYPE_DOUBLE, "REAL", Double.class, double.class),
        BOOLEAN(Json.TYPE_BOOLEAN, "INTEGER", Boolean.class, boolean.class),
        DATE(Json.TYPE_DATE, "INTEGER", Date.class, Date.class),
        BYTE_ARRAY(Json.TYPE_BYTE_ARRAY, "BLOB", byte[].class, byte[].class),
        ENUM(Json.TYPE_ENUM, "INTEGER", null, null),
        // @formatter:on
        ;

        private String mJsonName;
        private String mSqlType;
        private Class<?> mNullableJavaType;
        private Class<?> mNotNullableJavaType;

        private Type(String jsonName, String sqlType, Class<?> nullableJavaType, Class<?> notNullableJavaType) {
            mJsonName = jsonName;
            mSqlType = sqlType;
            mNullableJavaType = nullableJavaType;
            mNotNullableJavaType = notNullableJavaType;
            sTypeJsonNames.put(jsonName, this);
        }

        public static Type fromJsonName(String jsonName) {
            Type res = sTypeJsonNames.get(jsonName);
            if (res == null) throw new IllegalArgumentException("The type '" + jsonName + "' is unknown");
            return res;
        }

        public String getSqlType() {
            return mSqlType;
        }

        public Class<?> getNullableJavaType() {
            return mNullableJavaType;
        }

        public Class<?> getNotNullableJavaType() {
            return mNotNullableJavaType;
        }

        public boolean hasNotNullableJavaType() {
            if (this == ENUM) return false;
            return !mNullableJavaType.equals(mNotNullableJavaType);
        }
    }

    public static enum OnDeleteAction {
        // @formatter:off
        NO_ACTION(Json.ON_DELETE_ACTION_NO_ACTION),
        RESTRICT(Json.ON_DELETE_ACTION_RESTRICT),
        SET_NULL(Json.ON_DELETE_ACTION_SET_NULL),
        SET_DEFAULT(Json.ON_DELETE_ACTION_SET_DEFAULT),
        CASCADE(Json.ON_DELETE_ACTION_CASCADE),
        // @formatter:on
        ;

        private OnDeleteAction(String jsonName) {
            sOnDeleteActionJsonNames.put(jsonName, this);
        }

        public static OnDeleteAction fromJsonName(String jsonName) {
            OnDeleteAction res = sOnDeleteActionJsonNames.get(jsonName);
            if (res == null) throw new IllegalArgumentException("The onDelete value '" + jsonName + "' is unknown");
            return res;
        }
    }

    private static HashMap<String, Type> sTypeJsonNames = new HashMap<>();
    private static HashMap<String, OnDeleteAction> sOnDeleteActionJsonNames = new HashMap<>();

    private final Entity mEntity;
    private final String mName;
    private final String mDocumentation;
    private final Type mType;
    private final boolean mIsId;
    private final boolean mIsIndex;
    private final boolean mIsNullable;
    private final String mDefaultValue;
    private final String mEnumName;
    private final List<EnumValue> mEnumValues = new ArrayList<>();
    private final ForeignKey mForeignKey;
    private boolean mIsForeign;
    private boolean mIsAmbiguous;
    private Field mOriginalField;
    private String mPath;

    public Field(Entity entity, String name, String documentation, String type, boolean isId, boolean isIndex, boolean isNullable, String defaultValue,
            String enumName, List<EnumValue> enumValues, ForeignKey foreignKey) {
        mEntity = entity;
        mName = name;
        mDocumentation = documentation;
        mType = Type.fromJsonName(type);
        mIsId = isId;
        mIsIndex = isIndex;
        mIsNullable = isNullable;
        mDefaultValue = defaultValue;
        mEnumName = enumName;
        if (enumValues != null) mEnumValues.addAll(enumValues);
        mForeignKey = foreignKey;
    }

    public Field asForeignField(String path) {
        Field res = new Field(mEntity, mName, mDocumentation, mType.mJsonName, mIsId, mIsIndex, mIsNullable, mDefaultValue, mEnumName, mEnumValues, mForeignKey);
        res.mIsForeign = true;
        res.mOriginalField = this;
        res.mPath = path;
        return res;
    }

    public Entity getEntity() {
        return mEntity;
    }

    public String getNameUpperCase() {
        return mName.toUpperCase(Locale.US);
    }

    public String getNameLowerCase() {
        return mName.toLowerCase(Locale.US);
    }

    public String getNameCamelCase() {
        return WordUtils.capitalizeFully(mName, new char[] { '_' }).replaceAll("_", "");
    }

    public String getNameCamelCaseLowerCase() {
        return WordUtils.uncapitalize(getNameCamelCase());
    }

    public String getEnumName() {
        return mEnumName;
    }

    public List<EnumValue> getEnumValues() {
        return mEnumValues;
    }

    public String getPrefixedName() {
        return mEntity.getNameLowerCase() + "__" + getNameLowerCase();
    }

    public String getNameLowerCaseOrPrefixed() {
        if (mIsAmbiguous) return getPrefixedName();
        return getNameLowerCase();
    }

    public Type getType() {
        return mType;
    }

    public boolean getIsId() {
        return mIsId;
    }

    public boolean getIsIndex() {
        return mIsIndex;
    }

    public boolean getIsNullable() {
        return mIsNullable;
    }

    public String getDefaultValue() {
        return mDefaultValue;
    }

    public boolean getHasDefaultValue() {
        return mDefaultValue != null && mDefaultValue.length() > 0;
    }

    public String getJavaTypeSimpleName() {
        if (mType == Type.ENUM) {
            return mEnumName;
        }
        if (mIsNullable) {
            return mType.getNullableJavaType().getSimpleName();
        }
        return mType.getNotNullableJavaType().getSimpleName();
    }

    public boolean getIsConvertionNeeded() {
        return !mIsNullable && mType.hasNotNullableJavaType();
    }

    public boolean isEnum() {
        return mType == Type.ENUM;
    }

    public ForeignKey getForeignKey() {
        return mForeignKey;
    }

    public boolean getIsForeign() {
        return mIsForeign;
    }

    public String getPath() {
        return mPath;
    }

    /* package */void setIsAmbiguous(boolean isAmbiguous) {
        mIsAmbiguous = isAmbiguous;
        if (mOriginalField != null) mOriginalField.mIsAmbiguous = isAmbiguous;
    }

    /* package */boolean getIsAmbiguous() {
        return mIsAmbiguous;
    }

    public String getDocumentation() {
        return mDocumentation;
    }

    @Override
    public String toString() {
        return "Field [mName=" + mName + ", mDocumentation=" + mDocumentation + ", mType=" + mType + ", mIsId=" + mIsId + ", mIsIndex=" + mIsIndex
                + ", mIsNullable=" + mIsNullable + ", mDefaultValue=" + mDefaultValue + ", mEnumName=" + mEnumName + ", mEnumValues=" + mEnumValues
                + ", mForeignKey=" + mForeignKey + "]";
    }
}




Java Source Code List

org.jraf.androidcontentprovidergenerator.Arguments.java
org.jraf.androidcontentprovidergenerator.Config.java
org.jraf.androidcontentprovidergenerator.Constants.java
org.jraf.androidcontentprovidergenerator.Log.java
org.jraf.androidcontentprovidergenerator.Main.java
org.jraf.androidcontentprovidergenerator.model.Constraint.java
org.jraf.androidcontentprovidergenerator.model.Entity.java
org.jraf.androidcontentprovidergenerator.model.EnumValue.java
org.jraf.androidcontentprovidergenerator.model.Field.java
org.jraf.androidcontentprovidergenerator.model.ForeignKey.java
org.jraf.androidcontentprovidergenerator.model.Model.java
org.jraf.androidcontentprovidergenerator.sample.app.SampleActivity.java
org.jraf.androidcontentprovidergenerator.sample.provider.SampleProvider.java
org.jraf.androidcontentprovidergenerator.sample.provider.SampleSQLiteOpenHelperCallbacks.java
org.jraf.androidcontentprovidergenerator.sample.provider.SampleSQLiteOpenHelper.java
org.jraf.androidcontentprovidergenerator.sample.provider.base.AbstractContentValues.java
org.jraf.androidcontentprovidergenerator.sample.provider.base.AbstractCursor.java
org.jraf.androidcontentprovidergenerator.sample.provider.base.AbstractSelection.java
org.jraf.androidcontentprovidergenerator.sample.provider.company.CompanyColumns.java
org.jraf.androidcontentprovidergenerator.sample.provider.company.CompanyContentValues.java
org.jraf.androidcontentprovidergenerator.sample.provider.company.CompanyCursor.java
org.jraf.androidcontentprovidergenerator.sample.provider.company.CompanySelection.java
org.jraf.androidcontentprovidergenerator.sample.provider.person.Gender.java
org.jraf.androidcontentprovidergenerator.sample.provider.person.PersonColumns.java
org.jraf.androidcontentprovidergenerator.sample.provider.person.PersonContentValues.java
org.jraf.androidcontentprovidergenerator.sample.provider.person.PersonCursor.java
org.jraf.androidcontentprovidergenerator.sample.provider.person.PersonSelection.java
org.jraf.androidcontentprovidergenerator.sample.provider.personteam.PersonTeamColumns.java
org.jraf.androidcontentprovidergenerator.sample.provider.personteam.PersonTeamContentValues.java
org.jraf.androidcontentprovidergenerator.sample.provider.personteam.PersonTeamCursor.java
org.jraf.androidcontentprovidergenerator.sample.provider.personteam.PersonTeamSelection.java
org.jraf.androidcontentprovidergenerator.sample.provider.serialnumber.SerialNumberColumns.java
org.jraf.androidcontentprovidergenerator.sample.provider.serialnumber.SerialNumberContentValues.java
org.jraf.androidcontentprovidergenerator.sample.provider.serialnumber.SerialNumberCursor.java
org.jraf.androidcontentprovidergenerator.sample.provider.serialnumber.SerialNumberSelection.java
org.jraf.androidcontentprovidergenerator.sample.provider.team.TeamColumns.java
org.jraf.androidcontentprovidergenerator.sample.provider.team.TeamContentValues.java
org.jraf.androidcontentprovidergenerator.sample.provider.team.TeamCursor.java
org.jraf.androidcontentprovidergenerator.sample.provider.team.TeamSelection.java