sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 02/02: Referencing objects implement java.util.Formattable.
Date Mon, 14 Oct 2019 17:22:30 GMT
This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git

commit 88db100b0eb0676b36225e56493540c3a91e779d
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Mon Oct 14 19:22:00 2019 +0200

    Referencing objects implement java.util.Formattable.
---
 .../sis/referencing/AbstractIdentifiedObject.java  | 37 ++++++++++++++++++++--
 .../org/apache/sis/referencing/CommonCRSTest.java  | 14 +++++++-
 .../main/java/org/apache/sis/measure/Range.java    |  4 +--
 3 files changed, 50 insertions(+), 5 deletions(-)

diff --git a/core/sis-referencing/src/main/java/org/apache/sis/referencing/AbstractIdentifiedObject.java
b/core/sis-referencing/src/main/java/org/apache/sis/referencing/AbstractIdentifiedObject.java
index 9c0f430..c658b21 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/referencing/AbstractIdentifiedObject.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/referencing/AbstractIdentifiedObject.java
@@ -24,6 +24,8 @@ import java.util.AbstractCollection;
 import java.util.Iterator;
 import java.util.Locale;
 import java.util.Objects;
+import java.util.Formattable;
+import java.util.FormattableFlags;
 import java.io.Serializable;
 import javax.xml.bind.annotation.XmlID;
 import javax.xml.bind.annotation.XmlType;
@@ -44,6 +46,7 @@ import org.opengis.referencing.IdentifiedObject;
 import org.apache.sis.internal.jaxb.Context;
 import org.apache.sis.internal.jaxb.UseLegacyMetadata;
 import org.apache.sis.internal.jaxb.referencing.Code;
+import org.apache.sis.internal.util.Strings;
 import org.apache.sis.internal.util.UnmodifiableArrayList;
 import org.apache.sis.internal.metadata.NameToIdentifier;
 import org.apache.sis.internal.referencing.WKTUtilities;
@@ -116,7 +119,7 @@ import static org.apache.sis.internal.util.CollectionsExt.immutableSet;
  * objects and passed between threads without synchronization.
  *
  * @author  Martin Desruisseaux (IRD, Geomatys)
- * @version 1.0
+ * @version 1.1
  * @since   0.4
  * @module
  */
@@ -135,7 +138,7 @@ import static org.apache.sis.internal.util.CollectionsExt.immutableSet;
 })
 @UseLegacyMetadata
 public class AbstractIdentifiedObject extends FormattableObject implements IdentifiedObject,
-        LenientComparable, Deprecable, Serializable
+        Formattable, LenientComparable, Deprecable, Serializable
 {
     /**
      * Serial number for inter-operability with different versions.
@@ -876,6 +879,36 @@ public class AbstractIdentifiedObject extends FormattableObject implements
Ident
         return null;
     }
 
+    /**
+     * Formats the name of identifier of this object using the provider formatter.
+     * This method is invoked when an {@code IdentifiedObject} object is formatted
+     * using the {@code "%s"} conversion specifier of {@link Formatter}.
+     * Users don't need to invoke this method explicitly.
+     *
+     * <p>If the alternate flags is present (as in {@code "%#s"}), then this method
+     * will format the identifier (if present) instead than the object name.</p>
+     *
+     * @param  formatter  the formatter in which to format this identified object.
+     * @param  flags      whether to apply left alignment, use upper-case letters and/or
use alternate form.
+     * @param  width      minimal number of characters to write, padding with {@code ' '}
if necessary.
+     * @param  precision  maximal number of characters to write, or -1 if no limit.
+     *
+     * @see IdentifiedObjects#getName(IdentifiedObject, Citation)
+     * @see IdentifiedObjects#getIdentifierOrName(IdentifiedObject)
+     *
+     * @since 1.1
+     */
+    @Override
+    public void formatTo(final java.util.Formatter formatter, final int flags, final int
width, final int precision) {
+        final String value;
+        if ((flags & FormattableFlags.ALTERNATE) != 0) {
+            value = IdentifiedObjects.getIdentifierOrName(this);
+        } else {
+            value = IdentifiedObjects.getName(this, null);
+        }
+        Strings.formatTo(formatter, flags, width, precision, value);
+    }
+
 
 
 
diff --git a/core/sis-referencing/src/test/java/org/apache/sis/referencing/CommonCRSTest.java
b/core/sis-referencing/src/test/java/org/apache/sis/referencing/CommonCRSTest.java
index e66dcc2..61e7029 100644
--- a/core/sis-referencing/src/test/java/org/apache/sis/referencing/CommonCRSTest.java
+++ b/core/sis-referencing/src/test/java/org/apache/sis/referencing/CommonCRSTest.java
@@ -51,7 +51,7 @@ import static org.apache.sis.test.TestUtilities.*;
  * Tests the {@link CommonCRS} class.
  *
  * @author  Martin Desruisseaux (IRD, Geomatys)
- * @version 1.0
+ * @version 1.1
  * @since   0.4
  * @module
  */
@@ -339,4 +339,16 @@ public final strictfp class CommonCRSTest extends TestCase {
     public void testForEpoch() {
         assertSame(CommonCRS.Temporal.UNIX, CommonCRS.Temporal.forEpoch(Instant.ofEpochMilli(0)));
       // As specified in Javadoc.
     }
+
+    /**
+     * Tests formatting in a {@link java.util.Formatter}.
+     *
+     * @since 1.1
+     */
+    @Test
+    public void testFormat() {
+        assertEquals("World Geodetic System 1984", String.format("%s", CommonCRS.WGS84.datum()));
+        assertEquals("WORLD GEODETIC SYSTEM 1984", String.format("%S", CommonCRS.WGS84.datum()));
+        assertTrue(String.format("%#s", CommonCRS.WGS84.datum()).endsWith(":6326"));
+    }
 }
diff --git a/core/sis-utility/src/main/java/org/apache/sis/measure/Range.java b/core/sis-utility/src/main/java/org/apache/sis/measure/Range.java
index 20dfe34..f7bec2e 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/measure/Range.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/measure/Range.java
@@ -721,7 +721,7 @@ public class Range<E extends Comparable<? super E>> implements
CheckedContainer<
     }
 
     /**
-     * Formats this range using the provider formatter. This method is invoked when an
+     * Formats this range using the provider formatter. This method is invoked when a
      * {@code Range} object is formatted using the {@code "%s"} conversion specifier of
      * {@link Formatter}. Users don't need to invoke this method explicitly.
      *
@@ -729,7 +729,7 @@ public class Range<E extends Comparable<? super E>> implements
CheckedContainer<
      * be formatted using the {@linkplain RangeFormat#isAlternateForm() alternate form}
      * for exclusive bounds.</p>
      *
-     * @param  formatter  the formatter in which to format this angle.
+     * @param  formatter  the formatter in which to format this range.
      * @param  flags      {@link FormattableFlags#LEFT_JUSTIFY} for left alignment, or 0
for right alignment.
      * @param  width      minimal number of characters to write, padding with {@code ' '}
if necessary.
      * @param  precision  maximal number of characters to write, or -1 if no limit.


Mime
View raw message