1 | /*
|
---|
2 | * GeoTools - The Open Source Java GIS Toolkit
|
---|
3 | * http://geotools.org
|
---|
4 | *
|
---|
5 | * (C) 2004-2008, Open Source Geospatial Foundation (OSGeo)
|
---|
6 | *
|
---|
7 | * This library is free software; you can redistribute it and/or
|
---|
8 | * modify it under the terms of the GNU Lesser General Public
|
---|
9 | * License as published by the Free Software Foundation;
|
---|
10 | * version 2.1 of the License.
|
---|
11 | *
|
---|
12 | * This library is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
15 | * Lesser General Public License for more details.
|
---|
16 | *
|
---|
17 | * This package contains documentation from OpenGIS specifications.
|
---|
18 | * OpenGIS consortium's work is fully acknowledged here.
|
---|
19 | */
|
---|
20 | package org.geotools.parameter;
|
---|
21 |
|
---|
22 | import javax.measure.unit.Unit;
|
---|
23 |
|
---|
24 | import org.geotools.resources.i18n.ErrorKeys;
|
---|
25 | import org.geotools.resources.i18n.Errors;
|
---|
26 | import org.opengis.parameter.InvalidParameterTypeException;
|
---|
27 | import org.opengis.parameter.InvalidParameterValueException;
|
---|
28 | import org.opengis.parameter.ParameterDescriptor;
|
---|
29 | import org.opengis.parameter.ParameterValue;
|
---|
30 |
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * A parameter value as a floating point (double precision) number.
|
---|
34 | * This class provides the same functionalities than {@link Parameter}, except that:
|
---|
35 | * <ul>
|
---|
36 | * <li>Values are always floating point numbers of type {@code double}.</li>
|
---|
37 | * <li>Units are the same than the {@linkplain ParameterDescriptor#getUnit default units}.</li>
|
---|
38 | * </ul>
|
---|
39 | * When those conditions are meet, {@code ParameterRealValue} is slightly more efficient
|
---|
40 | * than {@code ParameterValue} since it avoid the creation of {@link Double} objects.
|
---|
41 | *
|
---|
42 | * @since 2.1
|
---|
43 | *
|
---|
44 | * @source $URL: http://svn.osgeo.org/geotools/branches/2.7.x/modules/library/referencing/src/main/java/org/geotools/parameter/FloatParameter.java $
|
---|
45 | * @version $Id: FloatParameter.java 37299 2011-05-25 05:21:24Z mbedward $
|
---|
46 | * @author Martin Desruisseaux (IRD)
|
---|
47 | *
|
---|
48 | * @see DefaultParameterDescriptor
|
---|
49 | * @see ParameterGroup
|
---|
50 | */
|
---|
51 | public class FloatParameter extends AbstractParameter implements ParameterValue<Double> {
|
---|
52 | /**
|
---|
53 | * Serial number for interoperability with different versions.
|
---|
54 | */
|
---|
55 | private static final long serialVersionUID = 9027797654033417816L;
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * The value.
|
---|
59 | */
|
---|
60 | private double value;
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Constructs a parameter from the specified descriptor. The descriptor
|
---|
64 | * {@linkplain ParameterDescriptor#getValueClass() value class}
|
---|
65 | * must be <code>{@linkplain Double}.class</code>.
|
---|
66 | *
|
---|
67 | * @param descriptor The abstract definition of this parameter.
|
---|
68 | * @throws IllegalArgumentException if the value class is not {@code Double.class}.
|
---|
69 | */
|
---|
70 | public FloatParameter(final ParameterDescriptor<Double> descriptor) {
|
---|
71 | super(descriptor);
|
---|
72 | final Class type = descriptor.getValueClass();
|
---|
73 | final Class expected = Double.class;
|
---|
74 | if (!expected.equals(type) && !Double.TYPE.equals(type)) {
|
---|
75 | throw new IllegalArgumentException(Errors.format(
|
---|
76 | ErrorKeys.ILLEGAL_CLASS_$2, type, expected));
|
---|
77 | }
|
---|
78 | final Number value = descriptor.getDefaultValue();
|
---|
79 | this.value = (value!=null) ? value.doubleValue() : Double.NaN;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Constructs a parameter from the specified descriptor and value. This convenience
|
---|
84 | * constructor is equivalents to the one-argument constructor followed by a call to
|
---|
85 | * {@link #setValue(double)}.
|
---|
86 | *
|
---|
87 | * @param descriptor The abstract definition of this parameter.
|
---|
88 | * @param value The parameter value.
|
---|
89 | * @throws IllegalArgumentException if the value class is not {@code Double.class}.
|
---|
90 | */
|
---|
91 | public FloatParameter(final ParameterDescriptor<Double> descriptor, final double value) {
|
---|
92 | this(descriptor);
|
---|
93 | setValue(value);
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Returns the abstract definition of this parameter.
|
---|
98 | */
|
---|
99 | @Override
|
---|
100 | @SuppressWarnings("unchecked") // Type should has been checked by the constructor.
|
---|
101 | public ParameterDescriptor<Double> getDescriptor() {
|
---|
102 | return (ParameterDescriptor) super.getDescriptor();
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Returns the unit of measure of the {@linkplain #doubleValue() parameter value}. The default
|
---|
107 | * implementation always delegates to {@link ParameterDescriptor#getUnit}.
|
---|
108 | *
|
---|
109 | * @return The unit of measure, or {@code null} if none.
|
---|
110 | */
|
---|
111 | public Unit<?> getUnit() {
|
---|
112 | return ((ParameterDescriptor) descriptor).getUnit();
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Returns the numeric value of the coordinate operation parameter in the specified unit
|
---|
117 | * of measure. This convenience method apply unit conversion on the fly as needed.
|
---|
118 | *
|
---|
119 | * @param unit The unit of measure for the value to be returned.
|
---|
120 | * @return The numeric value represented by this parameter after conversion to type
|
---|
121 | * {@code double} and conversion to {@code unit}.
|
---|
122 | * @throws IllegalArgumentException if the specified unit is invalid for this parameter.
|
---|
123 | */
|
---|
124 | public double doubleValue(final Unit<?> unit) throws IllegalArgumentException {
|
---|
125 | ensureNonNull("unit", unit);
|
---|
126 | final Unit<?> thisUnit = getUnit();
|
---|
127 | if (thisUnit == null) {
|
---|
128 | throw unitlessParameter(descriptor);
|
---|
129 | }
|
---|
130 | final int expectedID = Parameter.getUnitMessageID(thisUnit);
|
---|
131 | if (Parameter.getUnitMessageID(unit) != expectedID) {
|
---|
132 | throw new IllegalArgumentException(Errors.format(expectedID, unit));
|
---|
133 | }
|
---|
134 | return thisUnit.getConverterTo(unit).convert(value);
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Returns the numeric value of the coordinate operation parameter with its
|
---|
139 | * associated {@linkplain #getUnit unit of measure}.
|
---|
140 | *
|
---|
141 | * @return The numeric value represented by this parameter after conversion to type {@code double}.
|
---|
142 | */
|
---|
143 | public double doubleValue() {
|
---|
144 | return value;
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Returns the numeric value rounded to the nearest integer.
|
---|
149 | *
|
---|
150 | * @return The numeric value represented by this parameter after conversion to type {@code int}.
|
---|
151 | */
|
---|
152 | public int intValue() {
|
---|
153 | return (int) Math.round(value);
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Format an error message for illegal method call for the current value type.
|
---|
158 | */
|
---|
159 | private static String getClassTypeError() {
|
---|
160 | return Errors.format(ErrorKeys.ILLEGAL_OPERATION_FOR_VALUE_CLASS_$1, Double.class);
|
---|
161 | }
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Returns the parameter value as {{@link Double},
|
---|
165 | *
|
---|
166 | * @return The parameter value as an object.
|
---|
167 | */
|
---|
168 | public Double getValue() {
|
---|
169 | return Double.valueOf(value);
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Set the parameter value as a floating point and its associated unit.
|
---|
174 | *
|
---|
175 | * @param value The parameter value.
|
---|
176 | * @param unit The unit for the specified value.
|
---|
177 | * @throws InvalidParameterValueException if the value is illegal for some reason
|
---|
178 | * (for example a value out of range).
|
---|
179 | */
|
---|
180 | public void setValue(double value, final Unit<?> unit) throws InvalidParameterValueException {
|
---|
181 | ensureNonNull("unit", unit);
|
---|
182 | @SuppressWarnings("unchecked") // Checked by constructor.
|
---|
183 | final ParameterDescriptor<Double> descriptor = (ParameterDescriptor) this.descriptor;
|
---|
184 | final Unit<?> thisUnit = descriptor.getUnit();
|
---|
185 | if (thisUnit == null) {
|
---|
186 | throw unitlessParameter(descriptor);
|
---|
187 | }
|
---|
188 | final int expectedID = Parameter.getUnitMessageID(thisUnit);
|
---|
189 | if (Parameter.getUnitMessageID(unit) != expectedID) {
|
---|
190 | throw new IllegalArgumentException(Errors.format(expectedID, unit));
|
---|
191 | }
|
---|
192 | value = unit.getConverterTo(thisUnit).convert(value);
|
---|
193 | this.value = Parameter.ensureValidValue(descriptor, Double.valueOf(value));
|
---|
194 | }
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Set the parameter value as a floating point.
|
---|
198 | *
|
---|
199 | * @param value The parameter value.
|
---|
200 | * @throws InvalidParameterValueException if the value is illegal for some reason
|
---|
201 | * (for example a value out of range).
|
---|
202 | */
|
---|
203 | public void setValue(final double value) throws InvalidParameterValueException {
|
---|
204 | @SuppressWarnings("unchecked") // Checked by constructor.
|
---|
205 | final ParameterDescriptor<Double> descriptor = (ParameterDescriptor) this.descriptor;
|
---|
206 | this.value = Parameter.ensureValidValue(descriptor, Double.valueOf(value));
|
---|
207 | }
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Set the parameter value as an integer.
|
---|
211 | *
|
---|
212 | * @param value The parameter value.
|
---|
213 | * @throws InvalidParameterValueException if the value is illegal for some reason
|
---|
214 | * (for example a value out of range).
|
---|
215 | */
|
---|
216 | public void setValue(final int value) throws InvalidParameterValueException {
|
---|
217 | setValue((double) value);
|
---|
218 | }
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * Set the parameter value as a {@link Double} object.
|
---|
222 | *
|
---|
223 | * @param value The parameter value.
|
---|
224 | * @throws InvalidParameterValueException if the type of {@code value} is inappropriate
|
---|
225 | * for this parameter, or if the value is illegal for some other reason (for example
|
---|
226 | * the value is numeric and out of range).
|
---|
227 | */
|
---|
228 | public void setValue(final Object value) throws InvalidParameterValueException {
|
---|
229 | @SuppressWarnings("unchecked") // Checked by constructor.
|
---|
230 | final ParameterDescriptor<Double> descriptor = (ParameterDescriptor) this.descriptor;
|
---|
231 | this.value = Parameter.ensureValidValue(descriptor, value);
|
---|
232 | }
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * Always throws an exception, since this parameter is not an array.
|
---|
236 | */
|
---|
237 | public void setValue(double[] values, final Unit<?> unit)
|
---|
238 | throws InvalidParameterValueException
|
---|
239 | {
|
---|
240 | throw new InvalidParameterTypeException(getClassTypeError(),
|
---|
241 | Parameter.getName(descriptor));
|
---|
242 | }
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * Compares the specified object with this parameter for equality.
|
---|
246 | *
|
---|
247 | * @param object The object to compare to {@code this}.
|
---|
248 | * @return {@code true} if both objects are equal.
|
---|
249 | */
|
---|
250 | @Override
|
---|
251 | public boolean equals(final Object object) {
|
---|
252 | if (super.equals(object)) {
|
---|
253 | final FloatParameter that = (FloatParameter) object;
|
---|
254 | return Double.doubleToLongBits(this.value) ==
|
---|
255 | Double.doubleToLongBits(that.value);
|
---|
256 | }
|
---|
257 | return false;
|
---|
258 | }
|
---|
259 |
|
---|
260 | /**
|
---|
261 | * Returns a hash value for this parameter.
|
---|
262 | *
|
---|
263 | * @return The hash code value. This value doesn't need to be the same
|
---|
264 | * in past or future versions of this class.
|
---|
265 | */
|
---|
266 | @Override
|
---|
267 | public int hashCode() {
|
---|
268 | final long code = Double.doubleToLongBits(value);
|
---|
269 | return (int)code ^ (int)(code >>> 32) + super.hashCode()*37;
|
---|
270 | }
|
---|
271 |
|
---|
272 | /**
|
---|
273 | * Returns a clone of this parameter.
|
---|
274 | */
|
---|
275 | @Override
|
---|
276 | public FloatParameter clone() {
|
---|
277 | return (FloatParameter) super.clone();
|
---|
278 | }
|
---|
279 | }
|
---|