From: Mathieu Baudier Date: Sun, 26 Dec 2021 08:24:51 +0000 (+0100) Subject: Add integer standard type X-Git-Tag: argeo-commons-2.3.5~118 X-Git-Url: https://git.argeo.org/?a=commitdiff_plain;h=25a31ea46e5de6ce0de366fdb999588c6788c540;p=lgpl%2Fargeo-commons.git Add integer standard type --- diff --git a/org.argeo.api/src/org/argeo/api/gcr/StandardAttributeType.java b/org.argeo.api/src/org/argeo/api/gcr/StandardAttributeType.java index 6971e0782..ea70880b5 100644 --- a/org.argeo.api/src/org/argeo/api/gcr/StandardAttributeType.java +++ b/org.argeo.api/src/org/argeo/api/gcr/StandardAttributeType.java @@ -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 { - - @Override - public String parse(String str) { - return str; - } - - @Override - public String format(String obj) { - return obj; - } - - } - static class BooleanFormatter implements AttributeFormatter { /** @@ -118,6 +111,13 @@ public enum StandardAttributeType { } } + static class IntegerFormatter implements AttributeFormatter { + @Override + public Integer parse(String str) throws NumberFormatException { + return Integer.parseInt(str); + } + } + static class LongFormatter implements AttributeFormatter { @Override public Long parse(String str) throws NumberFormatException { @@ -165,4 +165,19 @@ public enum StandardAttributeType { } } + + static class StringFormatter implements AttributeFormatter { + + @Override + public String parse(String str) { + return str; + } + + @Override + public String format(String obj) { + return obj; + } + + } + }