Massive package refactoring
[lgpl/argeo-commons.git] / org.argeo.api.acr / src / org / argeo / api / acr / CrAttributeType.java
index 0bbf63e710142bd881c18c6815e0181b57cbe1f6..3e12fb1c87067fd188bfa34cac4c44794c9ec5d2 100644 (file)
@@ -9,6 +9,8 @@ import java.time.format.DateTimeParseException;
 import java.util.Arrays;
 import java.util.Base64;
 import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
 import java.util.UUID;
 
 import javax.xml.namespace.QName;
@@ -16,7 +18,7 @@ import javax.xml.namespace.QName;
 /**
  * Minimal standard attribute types that MUST be supported. All related classes
  * belong to java.base and can be implicitly derived form a given
- * <code>String<code>.
+ * <code>String</code>.
  */
 public enum CrAttributeType {
        BOOLEAN(Boolean.class, W3C_XML_SCHEMA_NS_URI, "boolean", new BooleanFormatter()), //
@@ -26,7 +28,7 @@ public enum CrAttributeType {
        // we do not support short and float, like recent additions to Java
        // (e.g. optional primitives)
        DATE_TIME(Instant.class, W3C_XML_SCHEMA_NS_URI, "dateTime", new InstantFormatter()), //
-       UUID(UUID.class, CrName.CR_NAMESPACE_URI, "uuid", new UuidFormatter()), //
+       UUID(UUID.class, ArgeoNamespace.CR_NAMESPACE_URI, "uuid", new UuidFormatter()), //
        ANY_URI(URI.class, W3C_XML_SCHEMA_NS_URI, "anyUri", new UriFormatter()), //
        STRING(String.class, W3C_XML_SCHEMA_NS_URI, "string", new StringFormatter()), //
        ;
@@ -34,7 +36,7 @@ public enum CrAttributeType {
        private final Class<?> clss;
        private final AttributeFormatter<?> formatter;
 
-       private ContentName qName;
+       private final ContentName qName;
 
        private <T> CrAttributeType(Class<T> clss, String namespaceUri, String localName, AttributeFormatter<T> formatter) {
                this.clss = clss;
@@ -47,10 +49,6 @@ public enum CrAttributeType {
                return qName;
        }
 
-       public void setqName(ContentName qName) {
-               this.qName = qName;
-       }
-
        public Class<?> getClss() {
                return clss;
        }
@@ -134,6 +132,41 @@ public enum CrAttributeType {
                return STRING.getFormatter().parse(str);
        }
 
+       /**
+        * Cast well know java types based on {@link Object#toString()} of the provided
+        * object.
+        * 
+        */
+       @SuppressWarnings("unchecked")
+       public static <T> Optional<T> cast(Class<T> clss, Object value) {
+               // TODO Or should we?
+               Objects.requireNonNull(value, "Cannot cast a null value");
+               if (String.class.isAssignableFrom(clss)) {
+                       return Optional.of((T) value.toString());
+               }
+               // Numbers
+               else if (Long.class.isAssignableFrom(clss)) {
+                       if (value instanceof Long)
+                               return Optional.of((T) value);
+                       return Optional.of((T) Long.valueOf(value.toString()));
+               } else if (Integer.class.isAssignableFrom(clss)) {
+                       if (value instanceof Integer)
+                               return Optional.of((T) value);
+                       return Optional.of((T) Integer.valueOf(value.toString()));
+               } else if (Double.class.isAssignableFrom(clss)) {
+                       if (value instanceof Double)
+                               return Optional.of((T) value);
+                       return Optional.of((T) Double.valueOf(value.toString()));
+               }
+               // Numbers
+//             else if (Number.class.isAssignableFrom(clss)) {
+//                     if (value instanceof Number)
+//                             return Optional.of((T) value);
+//                     return Optional.of((T) Number.valueOf(value.toString()));
+//             }
+               return Optional.empty();
+       }
+
        /** Utility to convert a data: URI to bytes. */
        public static byte[] bytesFromDataURI(URI uri) {
                if (!"data".equals(uri.getScheme()))