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 // TODO Also support INTEGER ?
17 LONG(Long
.class, new LongFormatter()), //
18 DOUBLE(Double
.class, new DoubleFormatter()), //
19 INSTANT(Instant
.class, new InstantFormatter()), //
20 UUID(UUID
.class, new UuidFormatter()), //
21 URI(URI
.class, new UriFormatter()), //
22 STRING(String
.class, new StringFormatter()), //
25 private <T
> StandardAttributeType(Class
<T
> clss
, AttributeFormatter
<T
> formatter
) {
27 this.formatter
= formatter
;
30 private final Class
<?
> clss
;
31 private final AttributeFormatter
<?
> formatter
;
33 public Class
<?
> getClss() {
37 public AttributeFormatter
<?
> getFormatter() {
41 static Object
parse(String str
) {
46 if (str
.length() == 4 || str
.length() == 5)
47 return BOOLEAN
.getFormatter().parse(str
);
48 } catch (IllegalArgumentException e
) {
52 return LONG
.getFormatter().parse(str
);
53 } catch (IllegalArgumentException e
) {
57 return DOUBLE
.getFormatter().parse(str
);
58 } catch (IllegalArgumentException e
) {
62 return INSTANT
.getFormatter().parse(str
);
63 } catch (IllegalArgumentException e
) {
67 if (str
.length() == 36)
68 return UUID
.getFormatter().parse(str
);
69 } catch (IllegalArgumentException e
) {
73 java
.net
.URI uri
= (java
.net
.URI
) URI
.getFormatter().parse(str
);
74 if (uri
.getScheme() != null)
76 String path
= uri
.getPath();
77 if (path
.indexOf('/') >= 0)
79 // if it is not clearly a path, we will consider it as a string
80 // because their is no way to distinguish between 'any_string'
81 // and 'any_file_name'.
82 // Note that providing ./any_file_name would result in an equivalent URI
83 } catch (IllegalArgumentException e
) {
88 return STRING
.getFormatter().parse(str
);
91 static class StringFormatter
implements AttributeFormatter
<String
> {
94 public String
parse(String str
) {
99 public String
format(String obj
) {
105 static class BooleanFormatter
implements AttributeFormatter
<Boolean
> {
108 * @param str must be exactly equals to either 'true' or 'false' (different
109 * contract than {@link Boolean#parseBoolean(String)}.
112 public Boolean
parse(String str
) throws IllegalArgumentException
{
113 if ("true".equals(str
))
115 if ("false".equals(str
))
116 return Boolean
.FALSE
;
117 throw new IllegalArgumentException("Argument is neither 'true' or 'false' : " + 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
);