1 package org
.argeo
.api
.gcr
;
4 import java
.net
.URISyntaxException
;
5 import java
.time
.Instant
;
6 import java
.time
.format
.DateTimeParseException
;
10 * Minimal standard attribute types that MUST be supported. All related classes
11 * belong to java.base and can be implicitly derived form a given
14 public enum StandardAttributeType
{
15 BOOLEAN(Boolean
.class, new BooleanFormatter()), //
16 INTEGER(Integer
.class, new IntegerFormatter()), //
17 LONG(Long
.class, new LongFormatter()), //
18 DOUBLE(Double
.class, new DoubleFormatter()), //
19 // we do not support short and float, like recent additions to Java
20 // (e.g. optional primitives)
21 INSTANT(Instant
.class, new InstantFormatter()), //
22 UUID(UUID
.class, new UuidFormatter()), //
23 URI(URI
.class, new UriFormatter()), //
24 STRING(String
.class, new StringFormatter()), //
27 private <T
> StandardAttributeType(Class
<T
> clss
, AttributeFormatter
<T
> formatter
) {
29 this.formatter
= formatter
;
32 private final Class
<?
> clss
;
33 private final AttributeFormatter
<?
> formatter
;
35 public Class
<?
> getClss() {
39 public AttributeFormatter
<?
> getFormatter() {
43 public static Object
parse(String str
) {
45 throw new IllegalArgumentException("String cannot be null");
48 if (str
.length() == 4 || str
.length() == 5)
49 return BOOLEAN
.getFormatter().parse(str
);
50 } catch (IllegalArgumentException e
) {
54 return INTEGER
.getFormatter().parse(str
);
55 } catch (IllegalArgumentException e
) {
59 return LONG
.getFormatter().parse(str
);
60 } catch (IllegalArgumentException e
) {
64 return DOUBLE
.getFormatter().parse(str
);
65 } catch (IllegalArgumentException e
) {
69 return INSTANT
.getFormatter().parse(str
);
70 } catch (IllegalArgumentException e
) {
74 if (str
.length() == 36)
75 return UUID
.getFormatter().parse(str
);
76 } catch (IllegalArgumentException e
) {
80 java
.net
.URI uri
= (java
.net
.URI
) URI
.getFormatter().parse(str
);
81 if (uri
.getScheme() != null)
83 String path
= uri
.getPath();
84 if (path
.indexOf('/') >= 0)
86 // if it is not clearly a path, we will consider it as a string
87 // because their is no way to distinguish between 'any_string'
88 // and 'any_file_name'.
89 // Note that providing ./any_file_name would result in an equivalent URI
90 } catch (IllegalArgumentException e
) {
95 return STRING
.getFormatter().parse(str
);
98 static class BooleanFormatter
implements AttributeFormatter
<Boolean
> {
101 * @param str must be exactly equals to either 'true' or 'false' (different
102 * contract than {@link Boolean#parseBoolean(String)}.
105 public Boolean
parse(String str
) throws IllegalArgumentException
{
106 if ("true".equals(str
))
108 if ("false".equals(str
))
109 return Boolean
.FALSE
;
110 throw new IllegalArgumentException("Argument is neither 'true' or 'false' : " + str
);
114 static class IntegerFormatter
implements AttributeFormatter
<Integer
> {
116 public Integer
parse(String str
) throws NumberFormatException
{
117 return Integer
.parseInt(str
);
121 static class LongFormatter
implements AttributeFormatter
<Long
> {
123 public Long
parse(String str
) throws NumberFormatException
{
124 return Long
.parseLong(str
);
128 static class DoubleFormatter
implements AttributeFormatter
<Double
> {
131 public Double
parse(String str
) throws NumberFormatException
{
132 return Double
.parseDouble(str
);
136 static class InstantFormatter
implements AttributeFormatter
<Instant
> {
139 public Instant
parse(String str
) throws IllegalArgumentException
{
141 return Instant
.parse(str
);
142 } catch (DateTimeParseException e
) {
143 throw new IllegalArgumentException("Cannot parse '" + str
+ "' as an instant", e
);
148 static class UuidFormatter
implements AttributeFormatter
<UUID
> {
151 public UUID
parse(String str
) throws IllegalArgumentException
{
152 return java
.util
.UUID
.fromString(str
);
156 static class UriFormatter
implements AttributeFormatter
<URI
> {
159 public URI
parse(String str
) throws IllegalArgumentException
{
162 } catch (URISyntaxException e
) {
163 throw new IllegalArgumentException("Cannot parse " + str
+ " as an URI.", e
);
169 static class StringFormatter
implements AttributeFormatter
<String
> {
172 public String
parse(String str
) {
177 public String
format(String obj
) {