/** * Returns the numeric value of the coordinate operation parameter in the specified unit of * measure. This convenience method apply unit conversion on the fly as needed. * * @param unit The unit of measure for the value to be returned. * @return The numeric value represented by this parameter after conversion to type {@code * double} and conversion to {@code unit}. * @throws InvalidParameterTypeException if the value is not a numeric type. * @throws IllegalArgumentException if the specified unit is invalid for this parameter. * @see #getUnit * @see #setValue(double,Unit) * @see #doubleValueList(Unit) */ public double doubleValue(final Unit<?> unit) throws InvalidParameterTypeException { if (this.unit == null) { throw unitlessParameter(descriptor); } ensureNonNull("unit", unit); final int expectedID = getUnitMessageID(this.unit); if (getUnitMessageID(unit) != expectedID) { throw new IllegalArgumentException(Errors.format(expectedID, unit)); } return Units.getConverterToAny(this.unit, unit).convert(doubleValue()); }
/** * Tests integer and floating point values in a wide range of values. Some on those values are * cached (e.g. 0, 90, 360) because frequently used. It should be transparent to the user. Test * also unit conversions (degrees to radians in this case). */ @Test public void testSequence() { for (int i = -1000; i <= 1000; i++) { assertEquals("new (Integer, ...)", i, Parameter.create("Integer", i).intValue()); assertEquals( "new (Double, ...)", i, Parameter.create("Double", i, null).doubleValue(), 0.0); assertEquals( "new (Double, ...)", i, Parameter.create("Double", i, AbstractUnit.ONE).doubleValue(), 0.0); assertEquals( "new (Double, ...)", Math.toRadians(i), Parameter.create("Double", i, NonSI.DEGREE_ANGLE).doubleValue(SI.RADIAN), 1E-6); } }
param = new Parameter(DefaultParameterDescriptor.create("Range", 15.0, -30.0, +40.0, null)); assertEquals("intValue", 15, param.intValue()); assertEquals("doubleValue", 15, param.doubleValue(), 0.0); param.setValue(12.0); assertEquals("intValue", 12, param.intValue()); assertEquals("doubleValue", 12, param.doubleValue(), 0.0); try { param.setValue(50.0);
param = new Parameter(DefaultParameterDescriptor.create("Range", 15, -30, +40)); assertEquals("intValue", 15, param.intValue()); assertEquals("doubleValue", 15, param.doubleValue(), 0.0); param.setValue(12); assertEquals("intValue", 12, param.intValue()); assertEquals("doubleValue", 12, param.doubleValue(), 0.0); try { param.setValue(50);
assertNull("unit", parameter.getUnit()); assertEquals("intValue", 14, parameter.intValue()); assertEquals("doubleValue", 14, parameter.doubleValue(), 0); assertEquals("type", Integer.class, descriptor.getValueClass()); assertEquals("name", "Test", descriptor.getName().getCode()); assertNull("validValues", descriptor.getValidValues()); try { parameter.doubleValue(SI.METRE); fail("doubleValue(METER)"); } catch (IllegalStateException exception) { descriptor = (ParameterDescriptor) parameter.getDescriptor(); assertEquals("intValue", 3, parameter.intValue()); assertEquals("doubleValue", 3, parameter.doubleValue(), 0); assertEquals("doubleValue", 300, parameter.doubleValue(MetricPrefix.CENTI(SI.METRE)), 0); assertEquals("name", "Test", descriptor.getName().getCode()); assertEquals("unit", SI.METRE, descriptor.getUnit()); validValues); try { parameter.doubleValue(); fail("doubleValue should not be allowed on AxisDirection"); } catch (InvalidParameterTypeException exception) {