]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.api/src/org/argeo/api/gcr/StandardAttributeType.java
SLF4J implementation.
[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 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()), //
25 ;
26
27 private <T> StandardAttributeType(Class<T> clss, AttributeFormatter<T> formatter) {
28 this.clss = clss;
29 this.formatter = formatter;
30 }
31
32 private final Class<?> clss;
33 private final AttributeFormatter<?> formatter;
34
35 public Class<?> getClss() {
36 return clss;
37 }
38
39 public AttributeFormatter<?> getFormatter() {
40 return formatter;
41 }
42
43 public static Object parse(String str) {
44 if (str == null)
45 throw new IllegalArgumentException("String cannot be null");
46 // order IS important
47 try {
48 if (str.length() == 4 || str.length() == 5)
49 return BOOLEAN.getFormatter().parse(str);
50 } catch (IllegalArgumentException e) {
51 // silent
52 }
53 try {
54 return INTEGER.getFormatter().parse(str);
55 } catch (IllegalArgumentException e) {
56 // silent
57 }
58 try {
59 return LONG.getFormatter().parse(str);
60 } catch (IllegalArgumentException e) {
61 // silent
62 }
63 try {
64 return DOUBLE.getFormatter().parse(str);
65 } catch (IllegalArgumentException e) {
66 // silent
67 }
68 try {
69 return INSTANT.getFormatter().parse(str);
70 } catch (IllegalArgumentException e) {
71 // silent
72 }
73 try {
74 if (str.length() == 36)
75 return UUID.getFormatter().parse(str);
76 } catch (IllegalArgumentException e) {
77 // silent
78 }
79 try {
80 java.net.URI uri = (java.net.URI) URI.getFormatter().parse(str);
81 if (uri.getScheme() != null)
82 return uri;
83 String path = uri.getPath();
84 if (path.indexOf('/') >= 0)
85 return uri;
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) {
91 // silent
92 }
93
94 // default
95 return STRING.getFormatter().parse(str);
96 }
97
98 static class BooleanFormatter implements AttributeFormatter<Boolean> {
99
100 /**
101 * @param str must be exactly equals to either 'true' or 'false' (different
102 * contract than {@link Boolean#parseBoolean(String)}.
103 */
104 @Override
105 public Boolean parse(String str) throws IllegalArgumentException {
106 if ("true".equals(str))
107 return Boolean.TRUE;
108 if ("false".equals(str))
109 return Boolean.FALSE;
110 throw new IllegalArgumentException("Argument is neither 'true' or 'false' : " + str);
111 }
112 }
113
114 static class IntegerFormatter implements AttributeFormatter<Integer> {
115 @Override
116 public Integer parse(String str) throws NumberFormatException {
117 return Integer.parseInt(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
169 static class StringFormatter implements AttributeFormatter<String> {
170
171 @Override
172 public String parse(String str) {
173 return str;
174 }
175
176 @Override
177 public String format(String obj) {
178 return obj;
179 }
180
181 }
182
183 }