-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
MetricConversion.java
183 lines (154 loc) · 4.62 KB
/
MetricConversion.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* Copyright 2016-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.core.query;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metric;
import org.springframework.data.geo.Metrics;
/**
* {@link Metric} and {@link Distance} conversions using the metric system.
*
* @author Mark Paluch
* @author Christoph Strobl
* @since 2.2
*/
public class MetricConversion {
private static final BigDecimal METERS_MULTIPLIER = new BigDecimal(Metrics.KILOMETERS.getMultiplier())
.multiply(new BigDecimal(1000));
// to achieve a calculation that is accurate to 0.3 meters
private static final int PRECISION = 8;
/**
* Return meters to {@code metric} multiplier.
*
* @param metric
* @return
*/
public static double getMetersToMetricMultiplier(Metric metric) {
ConversionMultiplier conversionMultiplier = ConversionMultiplier.builder().from(METERS_MULTIPLIER).to(metric)
.build();
return conversionMultiplier.multiplier().doubleValue();
}
/**
* Return {@code distance} in meters.
*
* @param distance
* @return
*/
public static double getDistanceInMeters(Distance distance) {
return new BigDecimal(distance.getValue()).multiply(getMetricToMetersMultiplier(distance.getMetric()))
.doubleValue();
}
/**
* Return {@code distance} in radians (on an earth like sphere).
*
* @param distance must not be {@literal null}.
* @return distance in radians.
* @since 3.4.4
*/
public static double toRadians(Distance distance) {
return metersToRadians(getDistanceInMeters(distance));
}
/**
* Return {@code distance} in radians (on an earth like sphere).
*
* @param meters
* @return distance in radians.
* @since 3.4.4
*/
public static double metersToRadians(double meters) {
return BigDecimal.valueOf(meters).divide(METERS_MULTIPLIER, MathContext.DECIMAL64).doubleValue();
}
/**
* Return {@code metric} to meters multiplier.
*
* @param metric
* @return
*/
private static BigDecimal getMetricToMetersMultiplier(Metric metric) {
ConversionMultiplier conversionMultiplier = ConversionMultiplier.builder().from(metric).to(METERS_MULTIPLIER)
.build();
return conversionMultiplier.multiplier();
}
/**
* Provides a multiplier to convert between various metrics. Metrics must share the same base scale and provide a
* multiplier to convert between the base scale and its own metric.
*
* @author Mark Paluch
*/
private static class ConversionMultiplier {
private final BigDecimal source;
private final BigDecimal target;
ConversionMultiplier(Number source, Number target) {
if (source instanceof BigDecimal) {
this.source = (BigDecimal) source;
} else {
this.source = new BigDecimal(source.doubleValue());
}
if (target instanceof BigDecimal) {
this.target = (BigDecimal) target;
} else {
this.target = new BigDecimal(target.doubleValue());
}
}
/**
* Returns the multiplier to convert a number from the {@code source} metric to the {@code target} metric.
*
* @return
*/
BigDecimal multiplier() {
return target.divide(source, PRECISION, RoundingMode.HALF_UP);
}
/**
* Creates a new {@link ConversionMultiplierBuilder}.
*
* @return
*/
static ConversionMultiplierBuilder builder() {
return new ConversionMultiplierBuilder();
}
}
/**
* Builder for {@link ConversionMultiplier}.
*
* @author Mark Paluch
*/
private static class ConversionMultiplierBuilder {
private Number from;
private Number to;
ConversionMultiplierBuilder() {}
ConversionMultiplierBuilder from(Number from) {
this.from = from;
return this;
}
ConversionMultiplierBuilder from(Metric from) {
this.from = from.getMultiplier();
return this;
}
ConversionMultiplierBuilder to(Number to) {
this.to = to;
return this;
}
ConversionMultiplierBuilder to(Metric to) {
this.to = to.getMultiplier();
return this;
}
ConversionMultiplier build() {
return new ConversionMultiplier(this.from, this.to);
}
}
}