sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1773525 - in /sis/branches/JDK8/core/sis-utility/src: main/java/org/apache/sis/internal/util/ main/java/org/apache/sis/measure/ test/java/org/apache/sis/measure/
Date Sat, 10 Dec 2016 17:22:06 GMT
Author: desruisseaux
Date: Sat Dec 10 17:22:06 2016
New Revision: 1773525

URL: http://svn.apache.org/viewvc?rev=1773525&view=rev
Log:
Override Format.clone(). No matter if we use it or not, it is required by the clone() contract
since this method is made public by java.text.Format.

Modified:
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/Cloner.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/RangeFormat.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitFormat.java
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/RangeFormatTest.java
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/UnitFormatTest.java

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/Cloner.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/Cloner.java?rev=1773525&r1=1773524&r2=1773525&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/Cloner.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/Cloner.java
[UTF-8] Sat Dec 10 17:22:06 2016
@@ -61,7 +61,7 @@ public class Cloner {
      * <p>The default implementation returns {@code true} in every cases.
      * Subclasses can override this method if they need a different behavior.</p>
      *
-     * @param  object The object that can not be cloned.
+     * @param  object  the object that can not be cloned.
      * @return {@code true} if the problem shall be considered a clone failure.
      */
     protected boolean isCloneRequired(final Object object) {
@@ -78,9 +78,9 @@ public class Cloner {
      *   <li>Otherwise the given object is returned.</li>
      * </ul>
      *
-     * @param  object The object to clone, or {@code null}.
-     * @return A clone of the given object, or {@code null} if {@code object} was null.
-     * @throws CloneNotSupportedException If the given object can not be cloned.
+     * @param  object  the object to clone, or {@code null}.
+     * @return a clone of the given object, or {@code null} if {@code object} was null.
+     * @throws CloneNotSupportedException if the given object can not be cloned.
      */
     public Object clone(final Object object) throws CloneNotSupportedException {
         if (object == null) {
@@ -136,7 +136,7 @@ public class Cloner {
      * or an unchecked exception, or do nothing otherwise. If this method returns normally,
      * then it is caller's responsibility to throw an other exception.
      *
-     * @param cause The value of {@link InvocationTargetException#getCause()}.
+     * @param  cause  the value of {@link InvocationTargetException#getCause()}.
      */
     private static void rethrow(final Throwable cause) throws CloneNotSupportedException
{
         if (cause instanceof CloneNotSupportedException) {
@@ -154,9 +154,9 @@ public class Cloner {
     /**
      * Returns an exception telling that the object can not be cloned because of the given
error.
      *
-     * @param  cause The cause for the failure to clone an object.
-     * @param  type  The type of object that we failed to clone.
-     * @return An exception with an error message and the given cause.
+     * @param  cause  the cause for the failure to clone an object.
+     * @param  type   the type of object that we failed to clone.
+     * @return an exception with an error message and the given cause.
      */
     private static CloneNotSupportedException fail(final Throwable cause, final Class<?>
type) {
         return (CloneNotSupportedException) new CloneNotSupportedException(
@@ -168,8 +168,8 @@ public class Cloner {
      * This method may be convenient when there is only one object to clone, otherwise instantiating
a new
      * {@code Cloner} object is more efficient.
      *
-     * @param  object The object to clone, or {@code null}.
-     * @return The given object (which may be {@code null}) or a clone of the given object.
+     * @param  object  the object to clone, or {@code null}.
+     * @return the given object (which may be {@code null}) or a clone of the given object.
      * @throws CloneNotSupportedException if the call to {@link Object#clone()} failed.
      *
      * @since 0.6

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/RangeFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/RangeFormat.java?rev=1773525&r1=1773524&r2=1773525&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/RangeFormat.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/RangeFormat.java
[UTF-8] Sat Dec 10 17:22:06 2016
@@ -1016,4 +1016,33 @@ public class RangeFormat extends Format
         }
         return convert(value);
     }
+
+    /**
+     * Returns a clone of this range format.
+     *
+     * @return a clone of this range format.
+     */
+    @Override
+    public RangeFormat clone() {
+        final RangeFormat f = (RangeFormat) super.clone();
+        try {
+            f.setFinal("elementFormat", elementFormat);
+            f.setFinal("unitFormat",    unitFormat);
+        } catch (ReflectiveOperationException e) {
+            throw new AssertionError(e);
+        }
+        return f;
+    }
+
+    /**
+     * Sets final field to a clone of the given format.
+     */
+    private void setFinal(final String name, Format value) throws ReflectiveOperationException
{
+        if (value != null) {
+            value = (Format) value.clone();
+            java.lang.reflect.Field f = RangeFormat.class.getDeclaredField(name);
+            f.setAccessible(true);
+            f.set(this, value);
+        }
+    }
 }

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitFormat.java?rev=1773525&r1=1773524&r2=1773525&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitFormat.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitFormat.java
[UTF-8] Sat Dec 10 17:22:06 2016
@@ -256,7 +256,7 @@ public class UnitFormat extends Format i
     }
 
     /**
-     * Symbols or names to use for formatting unit in replacement to the default unit symbols
or names.
+     * Symbols or names to use for formatting units in replacement to the default unit symbols
or names.
      *
      * @see #label(Unit, String)
      */
@@ -1219,4 +1219,37 @@ scan:   for (int n; i < end; i += n) {
             return null;
         }
     }
+
+    /**
+     * Returns a clone of this unit format. The new unit format will be initialized to the
same
+     * {@linkplain #getLocale() locale} and {@linkplain #label(Unit, String) labels} than
this format.
+     *
+     * @return a clone of this unit format.
+     */
+    @Override
+    public UnitFormat clone() {
+        final UnitFormat f = (UnitFormat) super.clone();
+        try {
+            f.setFinal("unitToLabel", unitToLabel);
+            f.setFinal("labelToUnit", labelToUnit);
+        } catch (ReflectiveOperationException e) {
+            throw new AssertionError(e);
+        }
+        return f;
+    }
+
+    /**
+     * Sets final field to a clone of the given map. The given map shall be either
+     * a {@link HashMap} or the instance returned by {@link Collections#emptyMap()}.
+     */
+    private void setFinal(final String name, Map<?,?> value) throws ReflectiveOperationException
{
+        if (value instanceof HashMap<?,?>) {
+            value = (Map<?,?>) ((HashMap<?,?>) value).clone();
+        } else {
+            value = new HashMap<>();
+        }
+        java.lang.reflect.Field f = UnitFormat.class.getDeclaredField(name);
+        f.setAccessible(true);
+        f.set(this, value);
+    }
 }

Modified: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/RangeFormatTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/RangeFormatTest.java?rev=1773525&r1=1773524&r2=1773525&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/RangeFormatTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/RangeFormatTest.java
[UTF-8] Sat Dec 10 17:22:06 2016
@@ -349,4 +349,17 @@ public final strictfp class RangeFormatT
         assertEquals(9, minPos.getEndIndex());
         assertEquals(range, parse(text));
     }
+
+    /**
+     * Tests {@link RangeFormat#clone()}.
+     */
+    @Test
+    public void testClone() {
+        final RangeFormat f1 = new RangeFormat(Locale.FRANCE);
+        f1.setElementPattern("#0.###", false);
+        final RangeFormat f2 = f1.clone();
+        f2.setElementPattern("#0.00#", false);
+        assertEquals("#0.###", f1.getElementPattern(false));
+        assertEquals("#0.00#", f2.getElementPattern(false));
+    }
 }

Modified: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/UnitFormatTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/UnitFormatTest.java?rev=1773525&r1=1773524&r2=1773525&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/UnitFormatTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/UnitFormatTest.java
[UTF-8] Sat Dec 10 17:22:06 2016
@@ -393,4 +393,22 @@ public final strictfp class UnitFormatTe
         assertSame(Units.VOLT,   f.parse("kg⋅m²∕(s³⋅A)"));
         assertSame(Units.VOLT,   f.parse("(kg)m²∕(s³⋅A)"));
     }
+
+    /**
+     * Tests {@link UnitFormat#clone()}.
+     */
+    @Test
+    public void testClone() {
+        final UnitFormat f1 = new UnitFormat(Locale.FRANCE);
+        f1.label(Units.METRE,  "myMeterLabel");
+        f1.label(Units.SECOND, "mySecondLabel");
+        final UnitFormat f2 = f1.clone();
+        f2.label(Units.METRE, "otherMeterLabel");
+        assertSame  (Locale.FRANCE,     f1.getLocale());
+        assertSame  (Locale.FRANCE,     f2.getLocale());
+        assertEquals("myMeterLabel",    f1.format(Units.METRE));
+        assertEquals("mySecondLabel",   f1.format(Units.SECOND));
+        assertEquals("otherMeterLabel", f2.format(Units.METRE));
+        assertEquals("mySecondLabel",   f2.format(Units.SECOND));
+    }
 }



Mime
View raw message