1 /*
2  * Copyright 2014-2020 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.data.geo.format;
17
18 import java.text.ParseException;
19 import java.util.Collections;
20 import java.util.LinkedHashMap;
21 import java.util.Locale;
22 import java.util.Map;
23 import java.util.Map.Entry;
24
25 import org.springframework.core.convert.converter.Converter;
26 import org.springframework.data.geo.Distance;
27 import org.springframework.data.geo.Metric;
28 import org.springframework.data.geo.Metrics;
29 import org.springframework.format.Formatter;
30 import org.springframework.lang.Nullable;
31 import org.springframework.util.StringUtils;
32
33 /**
34  * Converter to create {@link Distance} instances from {@link String} representations. The supported format is a decimal
35  * followed by whitespace and a metric abbreviation. We currently support the following abbreviations:
36  * {@value #SUPPORTED_METRICS}.
37  *
38  * @author Oliver Gierke
39  * @author Christoph Strobl
40  */

41 public enum DistanceFormatter implements Converter<String, Distance>, Formatter<Distance> {
42
43     INSTANCE;
44
45     private static final Map<String, Metric> SUPPORTED_METRICS;
46     private static final String INVALID_DISTANCE = "Expected double amount optionally followed by a metrics abbreviation (%s) but got '%s'!";
47
48     static {
49
50         Map<String, Metric> metrics = new LinkedHashMap<>();
51
52         for (Metric metric : Metrics.values()) {
53             metrics.put(metric.getAbbreviation(), metric);
54             metrics.put(metric.toString().toLowerCase(Locale.US), metric);
55         }
56
57         SUPPORTED_METRICS = Collections.unmodifiableMap(metrics);
58     }
59
60     /*
61      * (non-Javadoc)
62      * @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
63      */

64     @Nullable
65     @Override
66     public final Distance convert(String source) {
67         return source == null ? null : doConvert(source.trim().toLowerCase(Locale.US));
68     }
69
70     /*
71      * (non-Javadoc)
72      * @see org.springframework.format.Printer#print(java.lang.Object, java.util.Locale)
73      */

74     @Override
75     public String print(Distance distance, Locale locale) {
76         return distance == null ? null : String.format("%s%s", distance.getValue(), distance.getUnit().toLowerCase(locale));
77     }
78
79     /*
80      * (non-Javadoc)
81      * @see org.springframework.format.Parser#parse(java.lang.String, java.util.Locale)
82      */

83     @Override
84     public Distance parse(String text, Locale locale) throws ParseException {
85         return doConvert(text.trim().toLowerCase(locale));
86     }
87
88     /**
89      * Converts the given {@link String} source into a distance. Expects the source to reflect the {@link Metric} as held
90      * in the {@link #SUPPORTED_METRICS} map.
91      *
92      * @param source must not be {@literal null}.
93      * @return
94      */

95     private static Distance doConvert(String source) {
96
97         for (Entry<String, Metric> metric : SUPPORTED_METRICS.entrySet()) {
98             if (source.endsWith(metric.getKey())) {
99                 return fromString(source, metric);
100             }
101         }
102
103         try {
104             return new Distance(Double.parseDouble(source));
105         } catch (NumberFormatException o_O) {
106             throw new IllegalArgumentException(String.format(INVALID_DISTANCE,
107                     StringUtils.collectionToCommaDelimitedString(SUPPORTED_METRICS.keySet()), source));
108         }
109     }
110
111     /**
112      * Creates a {@link Distance} from the given source String and the {@link Metric} detected.
113      *
114      * @param source the raw source {@link String}, must not be {@literal null} or empty.
115      * @param metric the {@link Metric} detected keyed by the keyword it was detected for, must not be {@literal null}.
116      * @return
117      */

118     private static Distance fromString(String source, Entry<String, Metric> metric) {
119
120         String amountString = source.substring(0, source.indexOf(metric.getKey()));
121
122         try {
123             return new Distance(Double.parseDouble(amountString), metric.getValue());
124         } catch (NumberFormatException o_O) {
125             throw new IllegalArgumentException(String.format(INVALID_DISTANCE,
126                     StringUtils.collectionToCommaDelimitedString(SUPPORTED_METRICS.keySet()), source));
127         }
128     }
129 }
130