Changing the way to serialise the type of a property
This commit is contained in:
parent
37074ff322
commit
5fcc630820
|
@ -1,136 +0,0 @@
|
||||||
package org.gcube.informationsystem.types;
|
|
||||||
|
|
||||||
import java.math.BigInteger;
|
|
||||||
import java.net.URI;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.util.Calendar;
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
import org.gcube.informationsystem.model.reference.properties.Property;
|
|
||||||
import org.gcube.informationsystem.utils.TypeVersion;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Luca Frosini (ISTI - CNR)
|
|
||||||
*/
|
|
||||||
public enum BasePropertyType {
|
|
||||||
|
|
||||||
BOOLEAN("Boolean"),
|
|
||||||
|
|
||||||
INTEGER("Integer"),
|
|
||||||
|
|
||||||
SHORT("Short"),
|
|
||||||
|
|
||||||
LONG("Long"),
|
|
||||||
|
|
||||||
FLOAT("Float"),
|
|
||||||
|
|
||||||
DOUBLE("Double"),
|
|
||||||
|
|
||||||
DATETIME("Datetime"),
|
|
||||||
|
|
||||||
STRING("String"),
|
|
||||||
|
|
||||||
BINARY("Binary"),
|
|
||||||
|
|
||||||
PROPERTY("Property"),
|
|
||||||
|
|
||||||
PROPERTYLIST("PropertyList"),
|
|
||||||
|
|
||||||
PROPERTYSET("PropertySet"),
|
|
||||||
|
|
||||||
PROPERTYMAP("PropertyMap"),
|
|
||||||
|
|
||||||
BYTE("Byte");
|
|
||||||
|
|
||||||
private String stringValue;
|
|
||||||
|
|
||||||
BasePropertyType(String stringValue) {
|
|
||||||
this.stringValue = stringValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getStringValue() {
|
|
||||||
return stringValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
protected static final Map<Class<?>,BasePropertyType> BASE_PROPERTY_TYPES_BY_CLASS;
|
|
||||||
|
|
||||||
static {
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS = new HashMap<>();
|
|
||||||
|
|
||||||
// This is made by hand because not all types should be add.
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS.put(Boolean.TYPE, BasePropertyType.BOOLEAN);
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS.put(Boolean.class, BasePropertyType.BOOLEAN);
|
|
||||||
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS.put(Integer.TYPE, BasePropertyType.INTEGER);
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS.put(Integer.class, BasePropertyType.INTEGER);
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS.put(BigInteger.class, BasePropertyType.INTEGER);
|
|
||||||
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS.put(Short.TYPE, BasePropertyType.SHORT);
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS.put(Short.class, BasePropertyType.SHORT);
|
|
||||||
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS.put(Long.TYPE, BasePropertyType.LONG);
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS.put(Long.class, BasePropertyType.LONG);
|
|
||||||
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS.put(Float.TYPE, BasePropertyType.FLOAT);
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS.put(Float.class, BasePropertyType.FLOAT);
|
|
||||||
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS.put(Double.TYPE, BasePropertyType.DOUBLE);
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS.put(Double.class, BasePropertyType.DOUBLE);
|
|
||||||
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS.put(Date.class, BasePropertyType.DATETIME);
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS.put(Calendar.class, BasePropertyType.DATETIME);
|
|
||||||
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS.put(String.class, BasePropertyType.STRING);
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS.put(Character.TYPE, BasePropertyType.STRING);
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS.put(Character.class, BasePropertyType.STRING);
|
|
||||||
|
|
||||||
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS.put(Property.class, BasePropertyType.PROPERTY);
|
|
||||||
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS.put(List.class, BasePropertyType.PROPERTYLIST);
|
|
||||||
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS.put(Set.class, BasePropertyType.PROPERTYSET);
|
|
||||||
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS.put(Map.class, BasePropertyType.PROPERTYMAP);
|
|
||||||
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS.put(Byte.TYPE, BasePropertyType.BYTE);
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS.put(Byte.class, BasePropertyType.BYTE);
|
|
||||||
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS.put(byte[].class, BasePropertyType.BINARY);
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS.put(Byte[].class, BasePropertyType.BINARY);
|
|
||||||
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS.put(Enum.class, BasePropertyType.STRING);
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS.put(URI.class, BasePropertyType.STRING);
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS.put(URL.class, BasePropertyType.STRING);
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS.put(UUID.class, BasePropertyType.STRING);
|
|
||||||
BASE_PROPERTY_TYPES_BY_CLASS.put(TypeVersion.class, BasePropertyType.STRING);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the correspondent type by checking the "assignability" of the
|
|
||||||
* class received as parameter.
|
|
||||||
*
|
|
||||||
* @param clazz Class to check
|
|
||||||
* @return BasePropertyType instance if found, otherwise null
|
|
||||||
*/
|
|
||||||
public static BasePropertyType getBasePropertyTypeByClass(final Class<?> clazz) {
|
|
||||||
if(clazz == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
BasePropertyType type = BASE_PROPERTY_TYPES_BY_CLASS.get(clazz);
|
|
||||||
|
|
||||||
if(type == null && Enum.class.isAssignableFrom(clazz)) {
|
|
||||||
type = BASE_PROPERTY_TYPES_BY_CLASS.get(Enum.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
return type;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,272 @@
|
||||||
|
package org.gcube.informationsystem.types;
|
||||||
|
|
||||||
|
import java.math.BigInteger;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import org.gcube.informationsystem.base.reference.properties.PropertyElement;
|
||||||
|
import org.gcube.informationsystem.utils.TypeVersion;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Luca Frosini (ISTI - CNR)
|
||||||
|
*/
|
||||||
|
public class PropertyTypeName {
|
||||||
|
|
||||||
|
public enum BaseType {
|
||||||
|
|
||||||
|
BOOLEAN("Boolean"),
|
||||||
|
|
||||||
|
INTEGER("Integer"),
|
||||||
|
|
||||||
|
SHORT("Short"),
|
||||||
|
|
||||||
|
LONG("Long"),
|
||||||
|
|
||||||
|
FLOAT("Float"),
|
||||||
|
|
||||||
|
DOUBLE("Double"),
|
||||||
|
|
||||||
|
DATE("Date"),
|
||||||
|
|
||||||
|
STRING("String"),
|
||||||
|
|
||||||
|
BINARY("Binary"),
|
||||||
|
|
||||||
|
BYTE("Byte"),
|
||||||
|
|
||||||
|
PROPERTY("Property"),
|
||||||
|
|
||||||
|
LIST("List"),
|
||||||
|
|
||||||
|
SET("Set"),
|
||||||
|
|
||||||
|
MAP("Map");
|
||||||
|
|
||||||
|
private final String stringValue;
|
||||||
|
|
||||||
|
BaseType(final String stringValue) {
|
||||||
|
this.stringValue = stringValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return stringValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static final Map<String, BaseType> map;
|
||||||
|
|
||||||
|
static {
|
||||||
|
map = new HashMap<>();
|
||||||
|
for(BaseType baseType : BaseType.values()) {
|
||||||
|
map.put(baseType.stringValue, baseType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BaseType getBaseTypeFromString(String type) {
|
||||||
|
BaseType baseType = map.get(type);
|
||||||
|
if(baseType==null) {
|
||||||
|
throw new IllegalArgumentException("No BaseType having '" + type + "' as string value. Allowed values are " + map.keySet());
|
||||||
|
}
|
||||||
|
return baseType;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static final Map<Class<?>,BaseType> BASE_PROPERTY_TYPES_BY_CLASS;
|
||||||
|
|
||||||
|
static {
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS = new HashMap<>();
|
||||||
|
|
||||||
|
// This is made by hand because not all types should be add.
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS.put(Boolean.TYPE, BaseType.BOOLEAN);
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS.put(Boolean.class, BaseType.BOOLEAN);
|
||||||
|
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS.put(Integer.TYPE, BaseType.INTEGER);
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS.put(Integer.class, BaseType.INTEGER);
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS.put(BigInteger.class, BaseType.INTEGER);
|
||||||
|
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS.put(Short.TYPE, BaseType.SHORT);
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS.put(Short.class, BaseType.SHORT);
|
||||||
|
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS.put(Long.TYPE, BaseType.LONG);
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS.put(Long.class, BaseType.LONG);
|
||||||
|
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS.put(Float.TYPE, BaseType.FLOAT);
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS.put(Float.class, BaseType.FLOAT);
|
||||||
|
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS.put(Double.TYPE, BaseType.DOUBLE);
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS.put(Double.class, BaseType.DOUBLE);
|
||||||
|
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS.put(Date.class, BaseType.DATE);
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS.put(Calendar.class, BaseType.DATE);
|
||||||
|
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS.put(String.class, BaseType.STRING);
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS.put(Character.TYPE, BaseType.STRING);
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS.put(Character.class, BaseType.STRING);
|
||||||
|
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS.put(Byte.TYPE, BaseType.BYTE);
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS.put(Byte.class, BaseType.BYTE);
|
||||||
|
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS.put(byte[].class, BaseType.BINARY);
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS.put(Byte[].class, BaseType.BINARY);
|
||||||
|
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS.put(PropertyElement.class, BaseType.PROPERTY);
|
||||||
|
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS.put(List.class, BaseType.LIST);
|
||||||
|
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS.put(Set.class, BaseType.SET);
|
||||||
|
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS.put(Map.class, BaseType.MAP);
|
||||||
|
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS.put(Enum.class, BaseType.STRING);
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS.put(URI.class, BaseType.STRING);
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS.put(URL.class, BaseType.STRING);
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS.put(UUID.class, BaseType.STRING);
|
||||||
|
BASE_PROPERTY_TYPES_BY_CLASS.put(TypeVersion.class, BaseType.STRING);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the correspondent type by checking the "assignability" of the
|
||||||
|
* class received as parameter.
|
||||||
|
*
|
||||||
|
* @param clazz Class to check
|
||||||
|
* @return BasePropertyType instance if found, otherwise null
|
||||||
|
*/
|
||||||
|
public static BaseType getBaseTypeByClass(final Class<?> clazz) {
|
||||||
|
if(clazz == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
BaseType type = BASE_PROPERTY_TYPES_BY_CLASS.get(clazz);
|
||||||
|
|
||||||
|
if(type == null && Enum.class.isAssignableFrom(clazz)) {
|
||||||
|
type = BASE_PROPERTY_TYPES_BY_CLASS.get(Enum.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected BaseType baseType;
|
||||||
|
|
||||||
|
protected boolean genericType;
|
||||||
|
protected BaseType genericBaseType;
|
||||||
|
protected String genericClassName;
|
||||||
|
|
||||||
|
public PropertyTypeName(String type) {
|
||||||
|
setType(type);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setType(String type) {
|
||||||
|
int indexOfMinor = type.indexOf("<");
|
||||||
|
if(indexOfMinor == -1) {
|
||||||
|
genericType = false;
|
||||||
|
}else {
|
||||||
|
genericType = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!genericType) {
|
||||||
|
try {
|
||||||
|
baseType = BaseType.getBaseTypeFromString(type);
|
||||||
|
if(baseType.ordinal()>BaseType.PROPERTY.ordinal()) {
|
||||||
|
throw new IllegalArgumentException("Set, List and Map must specify a generic type, e.g. Set<Property>, List<Integer>.");
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}catch (IllegalArgumentException e) {
|
||||||
|
baseType = BaseType.PROPERTY;
|
||||||
|
genericClassName = type;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String cleanTypeFromGeneric = type.substring(0,indexOfMinor);
|
||||||
|
baseType = BaseType.getBaseTypeFromString(cleanTypeFromGeneric);
|
||||||
|
if(genericType) {
|
||||||
|
if(baseType.ordinal()<=BaseType.PROPERTY.ordinal()) {
|
||||||
|
throw new IllegalArgumentException("Only Set, List and Map can be generic. Map can only have String as Key");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Getting the pat inside <....>
|
||||||
|
String generic = type.substring(indexOfMinor+1, type.length()-1);
|
||||||
|
|
||||||
|
if(baseType == BaseType.MAP) {
|
||||||
|
int l = generic.length();
|
||||||
|
generic = generic.replace(BaseType.STRING.toString() + ",", "");
|
||||||
|
if(l!=(BaseType.STRING.stringValue.length() + 1 + generic.length())) {
|
||||||
|
throw new IllegalArgumentException("A Map can only has String as key e.g. Map<String,Integer>");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
genericBaseType = BaseType.getBaseTypeFromString(generic);
|
||||||
|
if(genericBaseType.ordinal()>BaseType.PROPERTY.ordinal()) {
|
||||||
|
throw new IllegalArgumentException("The generic of a " + baseType.toString() + " cannot be a Set, a List or a Map.");
|
||||||
|
}
|
||||||
|
}catch (IllegalArgumentException e) {
|
||||||
|
genericClassName = generic;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getType() {
|
||||||
|
StringBuffer stringBuffer = new StringBuffer();
|
||||||
|
|
||||||
|
switch (baseType) {
|
||||||
|
|
||||||
|
case PROPERTY:
|
||||||
|
stringBuffer.append(genericClassName);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SET: case LIST: case MAP:
|
||||||
|
stringBuffer.append(baseType.toString());
|
||||||
|
stringBuffer.append("<");
|
||||||
|
if(baseType == BaseType.MAP) {
|
||||||
|
stringBuffer.append(BaseType.STRING.toString());
|
||||||
|
stringBuffer.append(",");
|
||||||
|
}
|
||||||
|
if(genericBaseType!=null) {
|
||||||
|
stringBuffer.append(genericBaseType.toString());
|
||||||
|
}else {
|
||||||
|
stringBuffer.append(genericClassName);
|
||||||
|
}
|
||||||
|
stringBuffer.append(">");
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
stringBuffer.append(baseType.toString());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return stringBuffer.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return getType();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public BaseType getBaseType() {
|
||||||
|
return baseType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isGenericType() {
|
||||||
|
return genericType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BaseType getGenericBaseType() {
|
||||||
|
return genericBaseType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getGenericClassName() {
|
||||||
|
return genericClassName;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -11,7 +11,7 @@
|
||||||
|
|
||||||
<logger name="org.gcube" level="INFO" />
|
<logger name="org.gcube" level="INFO" />
|
||||||
<logger name="org.gcube.informationsystem" level="TRACE" />
|
<logger name="org.gcube.informationsystem" level="TRACE" />
|
||||||
<logger name="org.gcube.informationsystem.types" level="INFO" />
|
<logger name="org.gcube.informationsystem.types" level="TRACE" />
|
||||||
<logger name="org.gcube.informationsystem.resourceregistry.dbinitialization" level="INFO" />
|
<logger name="org.gcube.informationsystem.resourceregistry.dbinitialization" level="INFO" />
|
||||||
<logger name=" org.gcube.informationsystem.impl.utils.discovery" level="INFO" />
|
<logger name=" org.gcube.informationsystem.impl.utils.discovery" level="INFO" />
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue