Back to project page Abstract-Model.
The source code is released under:
Apache License
If you think the Android project Abstract-Model listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.logician.abstractModel; /*from www. jav a 2 s . c o m*/ import java.util.EnumSet; /** * Class for defining columns in {@link Model} implementation tables. * Each column generates its own SQL for its containing Model. * * @see Model#getColumns() * @see Type * @author Logician * */ public final class Column { /** * The name of the column. */ final String name; /** * The {@link Type} of the column. */ final Type type; /** * The integer length of the column, or -1 if default. */ final int length; /** * The NOT NULL flag for the column. */ final EnumSet<Flag> flags; final String SQL; /** * Full detail constructor. * * Use EnumSet.of() for quick flag-set creation. * @see EnumSet#of(Enum, Enum, Enum) * @param name The SQL-friendly name of the column. * @param type The Type of the column. * @param length The integer maximum size of the column. * @param flags If the column is to be defined as NOT NULL. */ public Column(String name, Type type, int length, EnumSet<Flag> flags){ this.name = name; this.type = type; this.length = length; this.flags = flags; this.SQL = getColumnSQL(); } /** * Constructor that sets no flags. * * @param name The SQL-friendly name of the column. * @param type The Type of the column. * @param length The integer maximum size of the column. */ public Column(String name, Type type, int length){ this(name, type, length, EnumSet.noneOf(Flag.class)); } /** * Constructor that lets the length of the column fall back to the default size for the type. * * Use EnumSet.of() for quick flag-set creation. * @see EnumSet#of(Enum, Enum, Enum) * @param name The SQL-friendly name of the column. * @param type The Type of the column. * @param flags An {@link EnumSet} of flags for this column. */ public Column(String name, Type type, EnumSet<Flag> flags){ this(name, type, -1, flags); } /** * Constructor for a Column with default length and no flags. * * See the SQLite documentation for the default size for each column type. * * @param name The SQL-friendly name of the column. * @param type The Type of the column. */ public Column(String name, Type type){ this(name, type, -1); } /** * The defining SQL for the column for the CREATE TABLE command of its containing model. * @return The column's SQL. */ String getColumnSQL(){ if(SQL != null) return SQL; else return new StringBuilder(getQuotedName()) .append(' ') .append(getTypeText()) .append(' ') .append(getFlagsText()) .toString(); } private String getQuotedName(){ return "\"" + name + "\""; } private String getTypeText(){ String text = null; switch(type){ case BOOLEAN: text = "BOOLEAN"; break; case BYTE_ARR: text ="BLOB" + getLength(""); break; case INTEGER: text = "INT" + getLength(""); break; case DOUBLE: text = "DOUBLE"; break; case FLOAT: text = "FLOAT"; break; case LONG: text = "BIGINT"; break; case JSON_OBJECT: case JSON_ARRAY: case STRING: text = "TEXT"; break; } return text; } private static final String UNIQUE = "UNIQUE"; private static final String NOT_NULL = "NOT NULL"; private static final String PRIMARY_KEY = "PRIMARY KEY"; private String getFlagsText(){ if(flags == null || flags.size() == 0) return ""; StringBuilder builder = new StringBuilder(); for(Flag flag : flags){ if(builder.length() != 0) builder.append(' '); switch(flag){ case UNIQUE: builder.append(UNIQUE); break; case NOT_NULL: builder.append(NOT_NULL); break; case PRIMARY_KEY: builder.append(PRIMARY_KEY); } } return builder.toString(); } private String getLength(String defaultStr){ return (length > 0) ? "(" + Integer.toString(length) + ")" : defaultStr; } /** * Returns false if {@code o} is not of class Column, else returns {@link #equals(Column)}. */ @Override public boolean equals(Object o){ if(!(o instanceof Column)) return false; return equals((Column) o); } /** * Returns true only if all the fields of this Column are equal to those of {@code col}. * * @param col The column to compare to. * @return If the columns' fields are all equal. */ public boolean equals(Column col){ return name == col.name && type == col.type && length == col.length && flags == col.flags; } /** * @see Flag * @return If the UNIQUE flag is set. */ public boolean isUnique(){ return flags.contains(Flag.UNIQUE); } /** * @see Flag * @return If the PRIMARY_KEY flag is set. */ public boolean isPrimaryKey(){ return flags.contains(Flag.PRIMARY_KEY); } /** * @see Flag * @return If the NOT_NULL flag is set. */ public boolean isNotNull(){ return flags.contains(Flag.NOT_NULL); } /** * An enumeration of the Java data types that will transfer (relatively) seamlessly to SQLite. * * <p /> * Types not found in this enumeration should implement * {@link BinarySerializable} and use {@linkplain #BYTE_ARR} * or {@link JSONSerializable} and use {@linkplain #JSON_OBJECT}. * * * @author Logician * */ public static enum Type { BOOLEAN, BYTE_ARR, INTEGER, DOUBLE, FLOAT, LONG, STRING, JSON_OBJECT, JSON_ARRAY } /** * An enumeration of the currently available SQL column constraints. * * @author Logician * */ public static enum Flag{ NOT_NULL, UNIQUE, PRIMARY_KEY } }