]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.api/src/org/argeo/api/gcr/StandardAttributeType.java
Move unit tests
[lgpl/argeo-commons.git] / org.argeo.api / src / org / argeo / api / gcr / StandardAttributeType.java
1 package org.argeo.api.gcr;
2
3 import java.net.URI;
4 import java.net.URISyntaxException;
5 import java.time.Instant;
6 import java.time.format.DateTimeParseException;
7 import java.util.UUID;
8
9 /**
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
12 * <code>String<code>.
13 */
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()), //
23 ;
24
25 private <T> StandardAttributeType(Class<T> clss, AttributeFormatter<T> formatter) {
26 this.clss = clss;
27 this.formatter = formatter;
28 }
29
30 private final Class<?> clss;
31 private final AttributeFormatter<?> formatter;
32
33 public Class<?> getClss() {
34 return clss;
35 }
36
37 public AttributeFormatter<?> getFormatter() {
38 return formatter;
39 }
40
41 static Object parse(String str) {
42 if (str == null)
43 return null;
44 // order IS important
45 try {
46 if (str.length() == 4 || str.length() == 5)
47 return BOOLEAN.getFormatter().parse(str);
48 } catch (IllegalArgumentException e) {
49 // silent
50 }
51 try {
52 return LONG.getFormatter().parse(str);
53 } catch (IllegalArgumentException e) {
54 // silent
55 }
56 try {
57 return DOUBLE.getFormatter().parse(str);
58 } catch (IllegalArgumentException e) {
59 // silent
60 }
61 try {
62 return INSTANT.getFormatter().parse(str);
63 } catch (IllegalArgumentException e) {
64 // silent
65 }
66 try {
67 if (str.length() == 36)
68 return UUID.getFormatter().parse(str);
69 } catch (IllegalArgumentException e) {
70 // silent
71 }
72 try {
73 java.net.URI uri = (java.net.URI) URI.getFormatter().parse(str);
74 if (uri.getScheme() != null)
75 return uri;
76 String path = uri.getPath();
77 if (path.indexOf('/') >= 0)
78 return uri;
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) {
84 // silent
85 }
86
87 // default
88 return STRING.getFormatter().parse(str);
89 }
90
91 static class StringFormatter implements AttributeFormatter<String> {
92
93 @Override
94 public String parse(String str) {
95 return str;
96 }
97
98 @Override
99 public String format(String obj) {
100 return obj;
101 }
102
103 }
104
105 static class BooleanFormatter implements AttributeFormatter<Boolean> {
106
107 /**
108 * @param str must be exactly equals to either 'true' or 'false' (different
109 * contract than {@link Boolean#parseBoolean(String)}.
110 */
111 @Override
112 public Boolean parse(String str) throws IllegalArgumentException {
113 if ("true".equals(str))
114 return Boolean.TRUE;
115 if ("false".equals(str))
116 return Boolean.FALSE;
117 throw new IllegalArgumentException("Argument is neither 'true' or 'false' : " + str);
118 }
119 }
120
121 static class LongFormatter implements AttributeFormatter<Long> {
122 @Override
123 public Long parse(String str) throws NumberFormatException {
124 return Long.parseLong(str);
125 }
126 }
127
128 static class DoubleFormatter implements AttributeFormatter<Double> {
129
130 @Override
131 public Double parse(String str) throws NumberFormatException {
132 return Double.parseDouble(str);
133 }
134 }
135
136 static class InstantFormatter implements AttributeFormatter<Instant> {
137
138 @Override
139 public Instant parse(String str) throws IllegalArgumentException {
140 try {
141 return Instant.parse(str);
142 } catch (DateTimeParseException e) {
143 throw new IllegalArgumentException("Cannot parse '" + str + "' as an instant", e);
144 }
145 }
146 }
147
148 static class UuidFormatter implements AttributeFormatter<UUID> {
149
150 @Override
151 public UUID parse(String str) throws IllegalArgumentException {
152 return java.util.UUID.fromString(str);
153 }
154 }
155
156 static class UriFormatter implements AttributeFormatter<URI> {
157
158 @Override
159 public URI parse(String str) throws IllegalArgumentException {
160 try {
161 return new URI(str);
162 } catch (URISyntaxException e) {
163 throw new IllegalArgumentException("Cannot parse " + str + " as an URI.", e);
164 }
165 }
166
167 }
168 }