All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.permazen.ENUM-HACKS.patch Maven / Gradle / Ivy

There is a newer version: 5.1.0
Show newest version
diff --git a/permazen-main/src/main/java/io/permazen/JClass.java b/permazen-main/src/main/java/io/permazen/JClass.java
index 0335a95..3020b51 100644
--- a/permazen-main/src/main/java/io/permazen/JClass.java
+++ b/permazen-main/src/main/java/io/permazen/JClass.java
@@ -17,6 +17,7 @@ import io.permazen.core.ListField;
 import io.permazen.core.MapField;
 import io.permazen.core.SetField;
 import io.permazen.core.UnknownFieldException;
+import io.permazen.core.type.EnumFieldType;
 import io.permazen.kv.KeyRanges;
 import io.permazen.schema.SchemaCompositeIndex;
 import io.permazen.schema.SchemaField;
@@ -631,12 +632,18 @@ public class JClass extends JSchemaObject {
             }
         }
 
-        // Detect enum types
+        // Detect enum types and enum array types, when 
         final Class> enumType = Enum.class.isAssignableFrom(fieldRawType) ?
           (Class>)fieldRawType.asSubclass(Enum.class) : null;
-
-        // If field type neither refers to a JClass type, nor is a registered field type, nor is an enum type, fail
-        if (!isReferenceType && nonReferenceType == null && enumType == null) {
+        if (enumType != null) {
+            if (nonReferenceType != null)
+                throw new IllegalArgumentException("invalid " + description + ": custom FieldType's for Enum types not supported");
+            nonReferenceType = this.createEnumFieldType(enumType);
+        } else if (nonReferenceType == null)
+            nonReferenceType = this.getEnumArrayType(fieldRawType);
+
+        // If field type neither refers to a JClass type, nor is a registered field type, nor is an enum or enum array type, fail
+        if (!isReferenceType && nonReferenceType == null) {
             throw new IllegalArgumentException("invalid " + description + ": an explicit type() must be specified"
               + " because no known type supports values of type " + fieldTypeToken);
         }
@@ -684,5 +691,22 @@ public class JClass extends JSchemaObject {
             throw new IllegalArgumentException("invalid " + description + ": " + e.getMessage(), e);
         }
     }
-}
 
+    // Get the FieldType corresponding to the given Enum array type
+    private FieldType getEnumArrayType(Class type) {
+        Preconditions.checkArgument(name != null, "null type");
+        if (type.isEnum())
+            return this.createEnumFieldType(type.asSubclass(Enum.class));
+        if (!type.isArray())
+            return null;
+        final FieldType elementFieldType = this.getEnumArrayType(type.getComponentType());
+        if (elementFieldType == null)
+            return null;
+        return this.jdb.db.getFieldTypeRegistry().getArrayType(elementFieldType);
+    }
+
+    // This method exists solely to bind the generic type parameters
+    private > EnumFieldType createEnumFieldType(Class enumType) {
+        return new EnumFieldType(enumType);
+    }
+}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy