Skip to content

Commit

Permalink
Remove restriction on what data types can exist in a model class #79
Browse files Browse the repository at this point in the history
  • Loading branch information
sai-pullabhotla committed Nov 4, 2016
1 parent baa9267 commit 65167eb
Show file tree
Hide file tree
Showing 14 changed files with 286 additions and 303 deletions.
11 changes: 7 additions & 4 deletions src/main/java/com/jmethods/catatumbo/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import com.jmethods.catatumbo.impl.DataType;

/**
* Specifies that a class is an entity. Classes with this annotation can be
* managed by the {@link EntityManager} to save objects to the Cloud Datastore
Expand All @@ -48,8 +46,13 @@
* will hold the full key to the parent entity.</li>
* <li>Field annotated with @{@link ParentKey} must have a data type of
* {@link DatastoreKey}.</li>
* <li>All other fields can be any of the types defined in the {@link DataType}
* enum and may have @{@link Property} annotation.</li>
* <li>May have zero or more embedded objects with an annotation
* of @{@link Embedded}.
* <li>All other fields can be any of the types for which a {@link Mapper}
* exists. The framework provides Mappers for various commonly used types.
* CustomMappers can be specified using
* {@link MapperFactory#setDefaultMapper(java.lang.reflect.Type, Mapper)} or
* {@link PropertyMapper} annotation.</li>
* </ul>
*
* @author Sai Pullabhotla
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/com/jmethods/catatumbo/MapperFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,7 @@ private Mapper createMapper(Type type) {
*/
private Mapper createMapper(Class<?> clazz) {
Mapper mapper;
if (Date.class.isAssignableFrom(clazz)) {
mapper = new DateMapper();
} else if (Calendar.class.isAssignableFrom(clazz)) {
mapper = new CalendarMapper();
} else if (Enum.class.isAssignableFrom(clazz)) {
if (Enum.class.isAssignableFrom(clazz)) {
mapper = new EnumMapper(clazz);
} else if (List.class.isAssignableFrom(clazz)) {
mapper = new ListMapper(clazz);
Expand Down Expand Up @@ -269,6 +265,8 @@ private void createDefaultMappers() {
cache.put(BigDecimal.class, new BigDecimalMapper());
cache.put(byte[].class, new ByteArrayMapper());
cache.put(char[].class, new CharArrayMapper());
cache.put(Date.class, new DateMapper());
cache.put(Calendar.class, new CalendarMapper());
cache.put(GeoLocation.class, new GeoLocationMapper());
cache.put(DatastoreKey.class, new KeyMapper());
}
Expand Down
188 changes: 0 additions & 188 deletions src/main/java/com/jmethods/catatumbo/impl/DataType.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.jmethods.catatumbo.DatastoreKey;
import com.jmethods.catatumbo.DefaultDatastoreKey;
import com.jmethods.catatumbo.EntityManagerException;
import com.jmethods.catatumbo.impl.IdentifierMetadata.DataType;

/**
* Utility methods.
Expand Down
51 changes: 8 additions & 43 deletions src/main/java/com/jmethods/catatumbo/impl/EntityIntrospector.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.jmethods.catatumbo.DatastoreKey;
import com.jmethods.catatumbo.Embedded;
import com.jmethods.catatumbo.Entity;
import com.jmethods.catatumbo.EntityManagerException;
Expand Down Expand Up @@ -51,16 +51,6 @@ public class EntityIntrospector {
*/
private static Cache<Class<?>, EntityMetadata> cache = new Cache<>(64);

/**
* Data types that are valid for identifiers
*/
private static final DataType[] VALID_IDENTIFIER_TYPES = { DataType.LONG, DataType.LONG_OBJECT, DataType.STRING };

static {
// Sort the valid types so we can do binary search.
Arrays.sort(VALID_IDENTIFIER_TYPES);
}

/**
* The class to introspect
*/
Expand Down Expand Up @@ -224,20 +214,10 @@ private List<Field> getAllFields() {
* the identifier field
*/
private void processIdentifierField(Field field) {
String fieldName = field.getName();
Identifier identifier = field.getAnnotation(Identifier.class);
boolean autoGenerated = identifier.autoGenerated();
Class<?> type = field.getType();
DataType dataType = DataType.forClass(type);
if (dataType == null) {
String message = String.format("Unknown or unsupported type, %s, for field %s in class %s. ", type,
fieldName, entityClass.getName());
throw new EntityManagerException(message);
}
if (!isValidIdentifierType(dataType)) {
throw new EntityManagerException(String.format("Invalid identifier type: %s. ", type));
}
IdentifierMetadata identifierMetadata = new IdentifierMetadata(field, dataType, autoGenerated);
IdentifierMetadata identifierMetadata = new IdentifierMetadata(field, autoGenerated);
String readMethodName = IntrospectionUtils.getReadMethodName(field);
Method readMethod = IntrospectionUtils.getReadMethod(entityClass, readMethodName, type);
identifierMetadata.setReadMethod(readMethod);
Expand All @@ -258,8 +238,7 @@ private void processIdentifierField(Field field) {
private void processKeyField(Field field) {
String fieldName = field.getName();
Class<?> type = field.getType();
DataType dataType = DataType.KEY;
if (!type.equals(dataType.getDataClass())) {
if (!type.equals(DatastoreKey.class)) {
String message = String.format("Invalid type, %s, for Key field %s in class %s. ", type, fieldName,
entityClass);
throw new EntityManagerException(message);
Expand All @@ -285,8 +264,7 @@ private void processKeyField(Field field) {
private void processParentKeyField(Field field) {
String fieldName = field.getName();
Class<?> type = field.getType();
DataType dataType = DataType.KEY;
if (!type.equals(dataType.getDataClass())) {
if (!type.equals(DatastoreKey.class)) {
String message = String.format("Invalid type, %s, for ParentKey field %s in class %s. ", type, fieldName,
entityClass);
throw new EntityManagerException(message);
Expand Down Expand Up @@ -331,11 +309,11 @@ private void processField(Field field) {
* the metadata of the field that has the Version annotation.
*/
private void processVersionField(PropertyMetadata propertyMetadata) {
DataType dataType = propertyMetadata.getDataType();
if (dataType != DataType.LONG) {
Class<?> dataClass = propertyMetadata.getDeclaredType();
if (!long.class.equals(dataClass)) {
String messageFormat = "Field %s in class %s must be of type %s";
throw new EntityManagerException(String.format(messageFormat, propertyMetadata.getField(), entityClass,
DataType.LONG.getDataClass()));
throw new EntityManagerException(
String.format(messageFormat, propertyMetadata.getField().getName(), entityClass, long.class));
}
entityMetadata.setVersionMetadata(propertyMetadata);
}
Expand Down Expand Up @@ -375,19 +353,6 @@ private void processEmbeddedField(Field field) {
entityMetadata.putEmbeddedMetadata(embeddedMetadata);
}

/**
* Checks to see if the given data type is valid to be used as an
* identifier.
*
* @param dataType
* the data type to check
* @return true, if the given data type is valid to be used as an
* identifier; false, otherwise.
*/
private static boolean isValidIdentifierType(DataType dataType) {
return Arrays.binarySearch(VALID_IDENTIFIER_TYPES, dataType) >= 0;
}

/**
* Convenient method for getting the metadata of the field used for
* optimistic locking.
Expand Down
Loading

0 comments on commit 65167eb

Please sign in to comment.