X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.api%2Fsrc%2Forg%2Fargeo%2Fapi%2Fgcr%2FStandardAttributeType.java;fp=org.argeo.api%2Fsrc%2Forg%2Fargeo%2Fapi%2Fgcr%2FStandardAttributeType.java;h=0000000000000000000000000000000000000000;hb=e5a22cdc7d0f4918f2740c626e1ab6384bd5ee44;hp=ea70880b55d31801a8b5541069ce94e92d83f951;hpb=51efb630db7314b67654a03d1bd983b45aa2f1ed;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.api/src/org/argeo/api/gcr/StandardAttributeType.java b/org.argeo.api/src/org/argeo/api/gcr/StandardAttributeType.java deleted file mode 100644 index ea70880b5..000000000 --- a/org.argeo.api/src/org/argeo/api/gcr/StandardAttributeType.java +++ /dev/null @@ -1,183 +0,0 @@ -package org.argeo.api.gcr; - -import java.net.URI; -import java.net.URISyntaxException; -import java.time.Instant; -import java.time.format.DateTimeParseException; -import java.util.UUID; - -/** - * Minimal standard attribute types that MUST be supported. All related classes - * belong to java.base and can be implicitly derived form a given - * String. - */ -public enum StandardAttributeType { - BOOLEAN(Boolean.class, new BooleanFormatter()), // - 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()), // - STRING(String.class, new StringFormatter()), // - ; - - private StandardAttributeType(Class clss, AttributeFormatter formatter) { - this.clss = clss; - this.formatter = formatter; - } - - private final Class clss; - private final AttributeFormatter formatter; - - public Class getClss() { - return clss; - } - - public AttributeFormatter getFormatter() { - return formatter; - } - - public static Object parse(String str) { - if (str == null) - throw new IllegalArgumentException("String cannot be null"); - // order IS important - try { - if (str.length() == 4 || str.length() == 5) - return BOOLEAN.getFormatter().parse(str); - } catch (IllegalArgumentException e) { - // silent - } - try { - return INTEGER.getFormatter().parse(str); - } catch (IllegalArgumentException e) { - // silent - } - try { - return LONG.getFormatter().parse(str); - } catch (IllegalArgumentException e) { - // silent - } - try { - return DOUBLE.getFormatter().parse(str); - } catch (IllegalArgumentException e) { - // silent - } - try { - return INSTANT.getFormatter().parse(str); - } catch (IllegalArgumentException e) { - // silent - } - try { - if (str.length() == 36) - return UUID.getFormatter().parse(str); - } catch (IllegalArgumentException e) { - // silent - } - try { - java.net.URI uri = (java.net.URI) URI.getFormatter().parse(str); - if (uri.getScheme() != null) - return uri; - String path = uri.getPath(); - if (path.indexOf('/') >= 0) - return uri; - // if it is not clearly a path, we will consider it as a string - // because their is no way to distinguish between 'any_string' - // and 'any_file_name'. - // Note that providing ./any_file_name would result in an equivalent URI - } catch (IllegalArgumentException e) { - // silent - } - - // default - return STRING.getFormatter().parse(str); - } - - static class BooleanFormatter implements AttributeFormatter { - - /** - * @param str must be exactly equals to either 'true' or 'false' (different - * contract than {@link Boolean#parseBoolean(String)}. - */ - @Override - public Boolean parse(String str) throws IllegalArgumentException { - if ("true".equals(str)) - return Boolean.TRUE; - if ("false".equals(str)) - return Boolean.FALSE; - throw new IllegalArgumentException("Argument is neither 'true' or 'false' : " + str); - } - } - - 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 { - return Long.parseLong(str); - } - } - - static class DoubleFormatter implements AttributeFormatter { - - @Override - public Double parse(String str) throws NumberFormatException { - return Double.parseDouble(str); - } - } - - static class InstantFormatter implements AttributeFormatter { - - @Override - public Instant parse(String str) throws IllegalArgumentException { - try { - return Instant.parse(str); - } catch (DateTimeParseException e) { - throw new IllegalArgumentException("Cannot parse '" + str + "' as an instant", e); - } - } - } - - static class UuidFormatter implements AttributeFormatter { - - @Override - public UUID parse(String str) throws IllegalArgumentException { - return java.util.UUID.fromString(str); - } - } - - static class UriFormatter implements AttributeFormatter { - - @Override - public URI parse(String str) throws IllegalArgumentException { - try { - return new URI(str); - } catch (URISyntaxException e) { - throw new IllegalArgumentException("Cannot parse " + str + " as an URI.", e); - } - } - - } - - static class StringFormatter implements AttributeFormatter { - - @Override - public String parse(String str) { - return str; - } - - @Override - public String format(String obj) { - return obj; - } - - } - -}