sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1790797 - in /sis/branches/JDK8/core/sis-utility/src: main/java/org/apache/sis/measure/ test/java/org/apache/sis/measure/ test/java/org/apache/sis/test/suite/
Date Mon, 10 Apr 2017 10:14:14 GMT
Author: desruisseaux
Date: Mon Apr 10 10:14:13 2017
New Revision: 1790797

URL: http://svn.apache.org/viewvc?rev=1790797&view=rev
Log:
Move the Units.quantity(double, Unit) method in Quantities.create(double, Unit).

Added:
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Quantities.java
  (with props)
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/QuantitiesTest.java
  (with props)
Modified:
    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/UnitServices.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Units.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/package-info.java
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java

Added: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Quantities.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Quantities.java?rev=1790797&view=auto
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Quantities.java
(added)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Quantities.java
[UTF-8] Mon Apr 10 10:14:13 2017
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ *     http://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.apache.sis.measure;
+
+import javax.measure.Unit;
+import javax.measure.Quantity;
+import javax.measure.quantity.Time;
+import javax.measure.quantity.Angle;
+import javax.measure.quantity.Length;
+import javax.measure.format.ParserException;
+import org.apache.sis.util.Static;
+import org.apache.sis.util.ArgumentChecks;
+import org.apache.sis.util.resources.Errors;
+
+
+/**
+ * Provides static methods working on {@link Quantity} instances.
+ * Apache SIS implementation of quantities has the following characteristics:
+ *
+ * <ul>
+ *   <li>Values are stored with {@code double} precision.</li>
+ *   <li>All quantities implement the specific subtype (e.g. {@link Length} instead
of {@code Quantity<Length>}).</li>
+ *   <li>Quantities are immutable, {@link Comparable} and {@link java.io.Serializable}.</li>
+ * </ul>
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @version 0.8
+ * @since   0.8
+ * @module
+ */
+public final class Quantities extends Static {
+    /**
+     * Do not allow instantiation of this class.
+     */
+    private Quantities() {
+    }
+
+    /**
+     * Creates a quantity for the given value and unit of measurement symbol.
+     * This is a convenience method that combines a call to {@link Units#valueOf(String)}
+     * with {@link #create(double, Unit)}.
+     *
+     * @param  value  the quantity magnitude.
+     * @param  unit   symbol of the unit of measurement associated to the given value.
+     * @return a quantity of the given type for the given value and unit of measurement.
+     * @throws ParserException if the given symbol can not be parsed.
+     */
+    public static Quantity<?> create(final double value, final String unit) {
+        return create(value, Units.valueOf(unit));
+    }
+
+    /**
+     * Creates a quantity for the given value and unit 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.
+     * @throws IllegalArgumentException if the given unit class is not a supported implementation.
+     *
+     * @see UnitServices#getQuantityFactory(Class)
+     */
+    public static <Q extends Quantity<Q>> Q create(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()));
+        }
+    }
+
+    /**
+     * Returns the given quantity as an instance of the specific {@code Quantity} subtype.
+     * For example this method can be used for converting a {@code Quantity<Length>}
to a {@link Length}.
+     * If the given quantity already implements the specific interface, then it is returned
as-is.
+     *
+     * @param  <Q>      the quantity type (e.g. {@link Length}, {@link Angle}, {@link
Time}, <i>etc.</i>), or {@code null}.
+     * @param  quantity the quantity to convert to the specific subtype.
+     * @return the given quantity as a specific subtype (may be {@code quantity} itself),
or {@code null} if the given quantity was null.
+     * @throws IllegalArgumentException if the unit class associated to the given quantity
is not a supported implementation.
+     */
+    @SuppressWarnings("unchecked")
+    public static <Q extends Quantity<Q>> Q castOrCopy(final Quantity<Q>
quantity) {
+        if (quantity != null) {
+            final Unit<Q> unit   = quantity.getUnit();
+            final Unit<Q> system = unit.getSystemUnit();
+            if (!(system instanceof SystemUnit<?>)) {
+                throw new IllegalArgumentException(Errors.format(Errors.Keys.UnsupportedImplementation_1,
unit.getClass()));
+            }
+            final Class<Q> type = ((SystemUnit<Q>) system).quantity;
+            if (!type.isInstance(quantity)) {
+                final ScalarFactory<Q> factory = ((SystemUnit<Q>) system).factory;
+                final double value = AbstractConverter.doubleValue(quantity.getValue());
+                if (factory != null) {
+                    return factory.create(value, unit);
+                } else {
+                    return ScalarFallback.factory(value, unit, type);
+                }
+            }
+        }
+        return (Q) quantity;
+    }
+}

Propchange: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Quantities.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Quantities.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

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=1790797&r1=1790796&r2=1790797&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]
Mon Apr 10 10:14:13 2017
@@ -63,10 +63,10 @@ abstract class Scalar<Q extends Quantity
 
     /**
      * 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 Units#quantity(double, Unit)}, but
without the need
+     * This method performs the same work than {@link Quantities#create(double, Unit)}, but
without the need
      * to check for the Apache SIS specific {@link SystemUnit} implementation.
      *
-     * @see Units#quantity(double, Unit)
+     * @see Quantities#create(double, Unit)
      */
     abstract Quantity<Q> create(double newValue, Unit<Q> newUnit);
 
@@ -77,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 Units.quantity(newValue, newUnit);
+            return Quantities.create(newValue, newUnit);
         }
         return this;
     }

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=1790797&r1=1790796&r2=1790797&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] Mon Apr 10 10:14:13 2017
@@ -222,13 +222,13 @@ public class UnitServices extends Servic
 
     /**
      * 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.
+     * {@link Quantities#create(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)
+     * @see Quantities#create(double, Unit)
      */
     @Override
     public <Q extends Quantity<Q>> QuantityFactory<Q> getQuantityFactory(final
Class<Q> type) {

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=1790797&r1=1790796&r2=1790797&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]
Mon Apr 10 10:14:13 2017
@@ -28,7 +28,6 @@ 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;
 
@@ -1723,31 +1722,4 @@ 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/main/java/org/apache/sis/measure/package-info.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/package-info.java?rev=1790797&r1=1790796&r2=1790797&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/package-info.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/package-info.java
[UTF-8] Mon Apr 10 10:14:13 2017
@@ -46,6 +46,8 @@
  * In relation to units of measurement, this package also defines:
  *
  * <ul>
+ *   <li>{@linkplain org.apache.sis.measure.Quantities}
+ *       as a {@code double} value value associated to a {@code Unit} instance.</li>
  *   <li>{@link org.apache.sis.measure.Angle} and its subclasses
  *      ({@link org.apache.sis.measure.Longitude},
  *       {@link org.apache.sis.measure.Latitude},
@@ -54,9 +56,10 @@
  *      ({@link org.apache.sis.measure.NumberRange},
  *       {@link org.apache.sis.measure.MeasurementRange}) or annotation
  *      ({@link org.apache.sis.measure.ValueRange})</li>
- *   <li>Formatters
+ *   <li>Parsers and formatters
  *      ({@link org.apache.sis.measure.AngleFormat},
- *       {@link org.apache.sis.measure.RangeFormat})</li>
+ *       {@link org.apache.sis.measure.RangeFormat},
+ *       {@link org.apache.sis.measure.UnitFormat})</li>
  * </ul>
  *
  * @author  Martin Desruisseaux (MPO, IRD, Geomatys)

Added: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/QuantitiesTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/QuantitiesTest.java?rev=1790797&view=auto
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/QuantitiesTest.java
(added)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/QuantitiesTest.java
[UTF-8] Mon Apr 10 10:14:13 2017
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ *     http://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.apache.sis.measure;
+
+import javax.measure.Unit;
+import javax.measure.Quantity;
+import javax.measure.quantity.Length;
+import org.apache.sis.test.DependsOn;
+import org.apache.sis.test.TestCase;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+
+/**
+ * Tests {@link Quantities}.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @version 0.8
+ * @since   0.8
+ * @module
+ */
+@DependsOn(ScalarTest.class)
+public final strictfp class QuantitiesTest extends TestCase {
+    /**
+     * Tests {@link Quantities#create(double, String)}.
+     */
+    @Test
+    public void testCreate() {
+        final Quantity<?> q = Quantities.create(5, "km");
+        assertEquals("value", 5, q.getValue().doubleValue(), STRICT);
+        assertSame  ("unit", Units.KILOMETRE, q.getUnit());
+    }
+
+    /**
+     * Tests {@link Quantities#castOrCopy(Quantity)}.
+     */
+    @Test
+    public void testCastOrCopy() {
+        Quantity<Length> q = Quantities.create(5, Units.KILOMETRE);
+        assertSame(q, Quantities.castOrCopy(q));
+
+        q = new Quantity<Length>() {
+            @Override public Number           getValue()                         {return
8;}
+            @Override public Unit<Length>     getUnit ()                         {return
Units.CENTIMETRE;}
+            @Override public Quantity<Length> add     (Quantity<Length> ignored)
{return null;}
+            @Override public Quantity<Length> subtract(Quantity<Length> ignored)
{return null;}
+            @Override public Quantity<?>      multiply(Quantity<?>      ignored)
{return null;}
+            @Override public Quantity<?>      divide  (Quantity<?>      ignored)
{return null;}
+            @Override public Quantity<Length> multiply(Number           ignored) {return
null;}
+            @Override public Quantity<Length> divide  (Number           ignored) {return
null;}
+            @Override public Quantity<?>      inverse ()                         {return
null;}
+            @Override public Quantity<Length> to      (Unit<Length>     ignored)
{return null;}
+            @Override public <T extends Quantity<T>> Quantity<T> asType(Class<T>
ignored) {return null;}
+        };
+        final Length c = Quantities.castOrCopy(q);
+        assertNotSame(q, c);
+        assertEquals("value", 8, c.getValue().doubleValue(), STRICT);
+        assertSame  ("unit", Units.CENTIMETRE, c.getUnit());
+    }
+}

Propchange: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/QuantitiesTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/QuantitiesTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Modified: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java?rev=1790797&r1=1790796&r2=1790797&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
[UTF-8] Mon Apr 10 10:14:13 2017
@@ -111,6 +111,7 @@ import org.junit.BeforeClass;
     org.apache.sis.measure.UnitFormatTest.class,
     org.apache.sis.measure.UnitsTest.class,
     org.apache.sis.measure.ScalarTest.class,
+    org.apache.sis.measure.QuantitiesTest.class,
     org.apache.sis.measure.UnitServicesTest.class,
     org.apache.sis.measure.RangeTest.class,
     org.apache.sis.measure.DateRangeTest.class,



Mime
View raw message