Add integer standard type
authorMathieu Baudier <mbaudier@argeo.org>
Sun, 26 Dec 2021 08:24:51 +0000 (09:24 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Sun, 26 Dec 2021 08:24:51 +0000 (09:24 +0100)
org.argeo.api/src/org/argeo/api/gcr/StandardAttributeType.java

index 6971e0782c1b17bc3a40e482dec8e4ba5ca96553..ea70880b55d31801a8b5541069ce94e92d83f951 100644 (file)
@@ -13,9 +13,11 @@ import java.util.UUID;
  */
 public enum StandardAttributeType {
        BOOLEAN(Boolean.class, new BooleanFormatter()), //
-       // TODO Also support INTEGER ?
+       INTEGER(Integer.class, new IntegerFormatter()), //
        LONG(Long.class, new LongFormatter()), //
        DOUBLE(Double.class, new DoubleFormatter()), //
+       // we do not support short and float, like recent additions to Java
+       // (e.g. optional primitives)
        INSTANT(Instant.class, new InstantFormatter()), //
        UUID(UUID.class, new UuidFormatter()), //
        URI(URI.class, new UriFormatter()), //
@@ -40,7 +42,7 @@ public enum StandardAttributeType {
 
        public static Object parse(String str) {
                if (str == null)
-                       return null;
+                       throw new IllegalArgumentException("String cannot be null");
                // order IS important
                try {
                        if (str.length() == 4 || str.length() == 5)
@@ -48,6 +50,11 @@ public enum StandardAttributeType {
                } catch (IllegalArgumentException e) {
                        // silent
                }
+               try {
+                       return INTEGER.getFormatter().parse(str);
+               } catch (IllegalArgumentException e) {
+                       // silent
+               }
                try {
                        return LONG.getFormatter().parse(str);
                } catch (IllegalArgumentException e) {
@@ -88,20 +95,6 @@ public enum StandardAttributeType {
                return STRING.getFormatter().parse(str);
        }
 
-       static class StringFormatter implements AttributeFormatter<String> {
-
-               @Override
-               public String parse(String str) {
-                       return str;
-               }
-
-               @Override
-               public String format(String obj) {
-                       return obj;
-               }
-
-       }
-
        static class BooleanFormatter implements AttributeFormatter<Boolean> {
 
                /**
@@ -118,6 +111,13 @@ public enum StandardAttributeType {
                }
        }
 
+       static class IntegerFormatter implements AttributeFormatter<Integer> {
+               @Override
+               public Integer parse(String str) throws NumberFormatException {
+                       return Integer.parseInt(str);
+               }
+       }
+
        static class LongFormatter implements AttributeFormatter<Long> {
                @Override
                public Long parse(String str) throws NumberFormatException {
@@ -165,4 +165,19 @@ public enum StandardAttributeType {
                }
 
        }
+
+       static class StringFormatter implements AttributeFormatter<String> {
+
+               @Override
+               public String parse(String str) {
+                       return str;
+               }
+
+               @Override
+               public String format(String obj) {
+                       return obj;
+               }
+
+       }
+
 }