/*
* Copyright 2010 Christian Matzat and others
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.matzat.android.hiberdroid.mapping;
import net.matzat.android.hiberdroid.exception.ColumnTypeNotSupportedException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* This Enum matches the Java and Database value types.
*/
public enum ColumnType {
STRING("TEXT", String.class),
LONG("INTEGER", Long.class, long.class),
INTEGER("INTEGER", Integer.class, int.class),
SHORT("INTEGER", Short.class, short.class),
BYTE("INTEGER", Byte.class, byte.class),
FLOAT("REAL", Float.class, float.class),
DOUBLE("REAL", Double.class, double.class),
DATE("STRING", Date.class),
BLOB("BLOB", byte[].class),
BOOLEAN("BOOLEAN", Boolean.class, boolean.class);
private List<Class> typeClasses;
private String sqlType;
ColumnType(String sqlType, Class... typeClasses) {
this.sqlType = sqlType;
this.typeClasses = new ArrayList<Class>();
for (Class typeClass : typeClasses) {
this.typeClasses.add(typeClass);
}
}
/**
* resolves the matching ColumnType for the given String
*
* @param valueToSearch String to search for
* @return the matching ColumnType
*/
public static ColumnType getByValue(String valueToSearch) {
for (ColumnType value : ColumnType.values()) {
if (value.name().equalsIgnoreCase(valueToSearch.trim())) {
return value;
}
}
throw new ColumnTypeNotSupportedException(String.format("columnType %s is not supported.", valueToSearch));
}
/**
* resolves the matching ColumnType for the given Class
*
* @param valueToSearch Class to search for
* @return the matching ColumnType
*/
public static ColumnType getByType(Class valueToSearch) {
for (ColumnType value : ColumnType.values()) {
for (Class typeClass : value.typeClasses) {
if (typeClass.equals(valueToSearch)) {
return value;
}
}
}
throw new ColumnTypeNotSupportedException(String.format("columnType %s is not supported.", valueToSearch.getSimpleName()));
}
/**
* gets the main type class (Object) of this ColumnType
*
* @return type class of the ColumnType
*/
public Class getTypeClass() {
return typeClasses.get(0);
}
/**
* gets the SQL type for this ColumnType
*
* @return SQL type for this ColumnType
*/
public String getSqlType() {
return sqlType;
}
}
|