sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1790749 - in /sis/branches/JDK8/core/sis-utility/src: main/java/org/apache/sis/measure/ test/java/org/apache/sis/measure/
Date Sun, 09 Apr 2017 15:50:55 GMT
Author: desruisseaux
Date: Sun Apr  9 15:50:55 2017
New Revision: 1790749

URL: http://svn.apache.org/viewvc?rev=1790749&view=rev
Log:
Add a Units.quantity(double, Unit) method and provide a fallback implementation for UnitServices.getQuantityFactory(Class).

Modified:
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/AbstractConverter.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/LinearConverter.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Scalar.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/ScalarFactory.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/ScalarFallback.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/SystemUnit.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitServices.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Units.java
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/ScalarTest.java

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/AbstractConverter.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/AbstractConverter.java?rev=1790749&r1=1790748&r2=1790749&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/AbstractConverter.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/AbstractConverter.java
[UTF-8] Sun Apr  9 15:50:55 2017
@@ -18,6 +18,7 @@ package org.apache.sis.measure;
 
 import java.io.Serializable;
 import javax.measure.UnitConverter;
+import org.apache.sis.math.DecimalFunctions;
 
 
 /**
@@ -67,4 +68,12 @@ abstract class AbstractConverter impleme
         }
         return Double.NaN;
     }
+
+    /**
+     * Returns the value of the given number, with special handling for {@link Float} value
on the assumption
+     * that the original value was written in base 10. This is usually the case for unit
conversion factors.
+     */
+    static double doubleValue(final Number n) {
+        return (n instanceof Float) ? DecimalFunctions.floatToDouble(n.floatValue()) : n.doubleValue();
+    }
 }

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/LinearConverter.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/LinearConverter.java?rev=1790749&r1=1790748&r2=1790749&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/LinearConverter.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/LinearConverter.java
[UTF-8] Sun Apr  9 15:50:55 2017
@@ -27,7 +27,6 @@ import org.apache.sis.util.ArgumentCheck
 import org.apache.sis.util.StringBuilders;
 import org.apache.sis.util.ComparisonMode;
 import org.apache.sis.util.LenientComparable;
-import org.apache.sis.math.DecimalFunctions;
 import org.apache.sis.math.MathFunctions;
 import org.apache.sis.math.Fraction;
 import org.apache.sis.internal.util.Numerics;
@@ -333,14 +332,7 @@ final class LinearConverter extends Abst
                 }
                 value = ((BigDecimal) value).multiply(scale10).add(offset10);
             } else {
-                final double x;
-                if (value instanceof Float) {
-                    // Because unit conversion factors are usually defined in base 10.
-                    x = DecimalFunctions.floatToDouble((Float) value);
-                } else {
-                    x = value.doubleValue();
-                }
-                value = convert(x);
+                value = convert(doubleValue(value));
             }
         }
         return value;

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Scalar.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Scalar.java?rev=1790749&r1=1790748&r2=1790749&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Scalar.java [UTF-8]
(original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Scalar.java [UTF-8]
Sun Apr  9 15:50:55 2017
@@ -21,7 +21,6 @@ import javax.measure.Unit;
 import javax.measure.Quantity;
 import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.StringBuilders;
-import org.apache.sis.util.resources.Errors;
 
 
 /**
@@ -63,28 +62,11 @@ abstract class Scalar<Q extends Quantity
     }
 
     /**
-     * Creates a quantity of for the given value.
-     */
-    static <Q extends Quantity<Q>> Quantity<Q> factory(final double value,
final Unit<Q> unit) {
-        final Unit<Q> system = unit.getSystemUnit();
-        if (system instanceof SystemUnit<?>) {
-            final ScalarFactory<Q> factory = ((SystemUnit<Q>) system).factory;
-            if (factory != null) {
-                return factory.create(value, unit);
-            } else {
-                return ScalarFallback.factory(value, unit, ((SystemUnit<Q>) system).quantity);
-            }
-        } else {
-            throw new IllegalArgumentException(Errors.format(Errors.Keys.UnsupportedImplementation_1,
unit.getClass()));
-        }
-    }
-
-    /**
      * Creates a new quantity of same type than this quantity but with a different value
and/or unit.
-     * This method performs the same work than {@link #factory(double, Unit)}, but without
the need
+     * This method performs the same work than {@link Units#quantity(double, Unit)}, but
without the need
      * to check for the Apache SIS specific {@link SystemUnit} implementation.
      *
-     * @see #factory(double, Unit)
+     * @see Units#quantity(double, Unit)
      */
     abstract Quantity<Q> create(double newValue, Unit<Q> newUnit);
 
@@ -95,7 +77,7 @@ abstract class Scalar<Q extends Quantity
      */
     private Quantity<?> of(final double newValue, final Unit<?> newUnit) {
         if (unit != newUnit || Double.doubleToRawLongBits(value) != Double.doubleToRawLongBits(newValue))
{
-            return factory(newValue, newUnit);
+            return Units.quantity(newValue, newUnit);
         }
         return this;
     }

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/ScalarFactory.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/ScalarFactory.java?rev=1790749&r1=1790748&r2=1790749&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/ScalarFactory.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/ScalarFactory.java
[UTF-8] Sun Apr  9 15:50:55 2017
@@ -39,5 +39,5 @@ interface ScalarFactory<Q extends Quanti
      * @param  unit   the unit of measurement associated to the given value.
      * @return a quantity with the given value and unit of measurement.
      */
-    Quantity<Q> create(double value, Unit<Q> unit);
+    Q create(double value, Unit<Q> unit);
 }

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/ScalarFallback.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/ScalarFallback.java?rev=1790749&r1=1790748&r2=1790749&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/ScalarFallback.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/ScalarFallback.java
[UTF-8] Sun Apr  9 15:50:55 2017
@@ -64,7 +64,7 @@ final class ScalarFallback<Q extends Qua
      * Creates a new {@link ScalarFallback} instance implementing the given quantity type.
      */
     @SuppressWarnings("unchecked")
-    static <Q extends Quantity<Q>> Quantity<Q> factory(final double value,
final Unit<Q> unit, final Class<Q> type) {
+    static <Q extends Quantity<Q>> Q factory(final double value, final Unit<Q>
unit, final Class<Q> type) {
         final Class<?> pc = Proxy.getProxyClass(Scalar.class.getClassLoader(), new
Class<?>[] {type});
         final Constructor<? extends Q> constructor;
         try {

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/SystemUnit.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/SystemUnit.java?rev=1790749&r1=1790748&r2=1790749&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/SystemUnit.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/SystemUnit.java
[UTF-8] Sun Apr  9 15:50:55 2017
@@ -32,7 +32,6 @@ import org.apache.sis.util.ObjectConvert
 import org.apache.sis.util.ComparisonMode;
 import org.apache.sis.util.resources.Errors;
 import org.apache.sis.internal.converter.SurjectiveConverter;
-import org.apache.sis.math.DecimalFunctions;
 import org.apache.sis.math.Fraction;
 
 
@@ -527,12 +526,7 @@ final class SystemUnit<Q extends Quantit
      */
     @Override
     public Quantity<Q> create(final Number value, final Unit<Q> unit) {
-        final double v;
-        if (value instanceof Float) {
-            v = DecimalFunctions.floatToDouble(value.floatValue());
-        } else {
-            v = value.doubleValue();
-        }
+        final double v = AbstractConverter.doubleValue(value);
         if (factory != null) {
             return factory.create(v, unit);
         } else {

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitServices.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitServices.java?rev=1790749&r1=1790748&r2=1790749&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitServices.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitServices.java
[UTF-8] Sun Apr  9 15:50:55 2017
@@ -20,6 +20,7 @@ import java.util.Collection;
 import java.util.HashSet;
 import java.util.Locale;
 import java.util.Set;
+import javax.measure.Unit;
 import javax.measure.Quantity;
 import javax.measure.format.UnitFormat;
 import javax.measure.spi.QuantityFactory;
@@ -220,14 +221,31 @@ public class UnitServices extends Servic
     }
 
     /**
-     * Return a factory for the given {@code Quantity} type, or {@code null} if none.
+     * Return a factory for the given {@code Quantity} type. In the particular case of Apache
SIS implementation,
+     * {@link Units#quantity(double, Unit)} provides a more direct way to instantiate quantities.
      *
      * @param  <Q>   compile-time value of the {@code type} argument.
      * @param  type  type of the desired the quantity.
      * @return the service to obtain {@link Quantity} instances, or {@code null} if none.
+     *
+     * @see Units#quantity(double, Unit)
      */
     @Override
     public <Q extends Quantity<Q>> QuantityFactory<Q> getQuantityFactory(final
Class<Q> type) {
-        return Units.get(type);
+        QuantityFactory<Q> factory = Units.get(type);
+        if (factory == null) {
+            factory = new QuantityFactory<Q>() {
+                @Override
+                public Quantity<Q> create(final Number value, final Unit<Q> unit)
{
+                    return ScalarFallback.factory(AbstractConverter.doubleValue(value), unit,
type);
+                }
+
+                @Override
+                public Unit<Q> getSystemUnit() {
+                    return null;
+                }
+            };
+        }
+        return factory;
     }
 }

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Units.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Units.java?rev=1790749&r1=1790748&r2=1790749&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Units.java [UTF-8]
(original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Units.java [UTF-8]
Sun Apr  9 15:50:55 2017
@@ -28,6 +28,7 @@ import org.opengis.referencing.cs.AxisDi
 
 import org.apache.sis.util.Static;
 import org.apache.sis.util.Workaround;
+import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.resources.Errors;
 import org.apache.sis.internal.util.Constants;
 
@@ -1722,4 +1723,31 @@ public final class Units extends Static
         }
         return null;
     }
+
+    /**
+     * Creates a quantity for the given value and units of measurement.
+     *
+     * @param  <Q>    the quantity type (e.g. {@link Length}, {@link Angle}, {@link
Time}, <i>etc.</i>).
+     * @param  value  the quantity magnitude.
+     * @param  unit   the unit of measurement associated to the given value.
+     * @return a quantity of the given type for the given value and unit of measurement.
+     *
+     * @since 0.8
+     *
+     * @see UnitServices#getQuantityFactory(Class)
+     */
+    public static <Q extends Quantity<Q>> Q quantity(final double value, final
Unit<Q> unit) {
+        ArgumentChecks.ensureNonNull("unit", unit);
+        final Unit<Q> system = unit.getSystemUnit();
+        if (system instanceof SystemUnit<?>) {
+            final ScalarFactory<Q> factory = ((SystemUnit<Q>) system).factory;
+            if (factory != null) {
+                return factory.create(value, unit);
+            } else {
+                return ScalarFallback.factory(value, unit, ((SystemUnit<Q>) system).quantity);
+            }
+        } else {
+            throw new IllegalArgumentException(Errors.format(Errors.Keys.UnsupportedImplementation_1,
unit.getClass()));
+        }
+    }
 }

Modified: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/ScalarTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/ScalarTest.java?rev=1790749&r1=1790748&r2=1790749&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/ScalarTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/ScalarTest.java
[UTF-8] Sun Apr  9 15:50:55 2017
@@ -82,7 +82,7 @@ public final strictfp class ScalarTest e
 
     /**
      * Tests {@link Scalar#multiply(Quantity)}, {@link Scalar#divide(Quantity)} and {@link
Quantity#inverse()}.
-     * Those tests depend on proper working of {@link Scalar#factory(double, Unit)}, which
depends in turn on
+     * Those tests depend on proper working of {@link Units#quantity(double, Unit)}, which
depends in turn on
      * proper declarations of {@link ScalarFactory} in {@link Units} initialization.
      */
     @Test



Mime
View raw message