sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1467349 - in /sis/branches/JDK7/sis-utility/src: main/java/org/apache/sis/internal/util/ main/java/org/apache/sis/measure/ main/java/org/apache/sis/util/iso/ test/java/org/apache/sis/measure/ test/java/org/apache/sis/util/iso/
Date Fri, 12 Apr 2013 16:47:02 GMT
Author: desruisseaux
Date: Fri Apr 12 16:47:02 2013
New Revision: 1467349

URL: http://svn.apache.org/r1467349
Log:
Range implements Formattable.

Modified:
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/Utilities.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/Angle.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/Range.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/RangeFormat.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/iso/AbstractInternationalString.java
    sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/measure/RangeFormatTest.java
    sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/measure/RangeTest.java
    sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultInternationalStringTest.java
    sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/iso/SimpleInternationalStringTest.java

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/Utilities.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/Utilities.java?rev=1467349&r1=1467348&r2=1467349&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/Utilities.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/Utilities.java
[UTF-8] Fri Apr 12 16:47:02 2013
@@ -123,18 +123,38 @@ public final class Utilities extends Sta
      * @param formatter The formatter in which to format the value.
      * @param flags     The formatting flags.
      * @param width     Minimal number of characters to write, padding with {@code ' '} if
necessary.
+     * @param precision Number of characters to keep before truncation, or -1 if no limit.
      * @param value     The text to format.
      */
-    public static void formatTo(final Formatter formatter, final int flags, int width, String
value) {
+    public static void formatTo(final Formatter formatter, final int flags,
+            int width, int precision, String value)
+    {
         final String format;
         final Object[] args;
         boolean isUpperCase = (flags & FormattableFlags.UPPERCASE) != 0;
         if (isUpperCase && width > 0) {
             // May change the string length in some locales.
             value = value.toUpperCase(formatter.locale());
-            isUpperCase = false;
+            isUpperCase = false; // Because conversion has already been done.
+        }
+        int length = value.length();
+        if (precision >= 0) {
+            for (int i=0,n=0; i<length; i += n) {
+                if (--precision < 0) {
+                    // Found the amount of characters to keep. The 'n' variable can be
+                    // zero only if precision == 0, in which case the string is empty.
+                    if (n == 0) {
+                        value = "";
+                    } else {
+                        length = (i -= n) + 1;
+                        final StringBuilder buffer = new StringBuilder(length);
+                        value = buffer.append(value, 0, i).append('…').toString();
+                    }
+                    break;
+                }
+                n = Character.charCount(value.codePointAt(i));
+            }
         }
-        final int length = value.length();
         // Double check since length() is faster than codePointCount(...).
         if (width > length && (width -= value.codePointCount(0, length)) >
0) {
             format = "%s%s";

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/Angle.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/Angle.java?rev=1467349&r1=1467348&r2=1467349&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/Angle.java [UTF-8]
(original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/Angle.java [UTF-8]
Fri Apr 12 16:47:02 2013
@@ -24,9 +24,10 @@ import java.text.Format;
 import java.text.ParseException;
 import java.io.Serializable;
 import net.jcip.annotations.Immutable;
-import org.apache.sis.math.MathFunctions;
+import org.apache.sis.internal.util.Utilities;
 
 import static java.lang.Double.doubleToLongBits;
+import static org.apache.sis.math.MathFunctions.isNegative;
 
 
 /**
@@ -204,7 +205,7 @@ public class Angle implements Comparable
         double m = Math.abs(θ);
         final boolean isSmall = m <= (1 / 3600E+3); // 1E-3 arc-second.
         if (isSmall || m > maximum()) {
-            final char h = hemisphere(MathFunctions.isNegative(θ));
+            final char h = hemisphere(isNegative(θ));
             if (h == 0) {
                 m = θ;  // Restore the sign.
             }
@@ -255,9 +256,7 @@ public class Angle implements Comparable
      *   <li>If the precision is 0, then this method formats an empty string.</li>
      *   <li>If the precision is 1 and this angle is a {@link Latitude} or {@link Longitude},
      *       then this method formats only the hemisphere symbol.</li>
-     *   <li>Otherwise the precision, if positive, is given to {@link AngleFormat#setMaximumWidth(int)}.
-     *       That formatter will try to respect the precision limit, but the formatted angle
may
-     *       still be wider if the precision is too small or the angle magnitude too large.</li>
+     *   <li>Otherwise the precision, if positive, is given to {@link AngleFormat#setMaximumWidth(int)}.</li>
      * </ul>
      *
      * @param formatter The formatter in which to format this angle.
@@ -266,24 +265,23 @@ public class Angle implements Comparable
      * @param precision Maximal number of characters to write, or -1 if no limit.
      */
     @Override
-    public void formatTo(final Formatter formatter, final int flags, final int width, int
precision) {
+    public void formatTo(final Formatter formatter, final int flags, final int width, final
int precision) {
         final String value;
         if (precision == 0) {
             value = "";
         } else {
-            if (precision > 0) {
-                final char h = hemisphere(MathFunctions.isNegative(θ));
-                if (h != 0 && --precision == 0) {
-                    formatter.format("%c", h);
-                    return;
+            final char h;
+            int w = precision; // To be decremented only if we may truncate and an hemisphere
symbol exist.
+            if (w > 0 && (h = hemisphere(isNegative(θ))) != 0 && --w
== 0) {
+                value = Character.toString(h);
+            } else {
+                final AngleFormat format = new AngleFormat(formatter.locale());
+                if (w > 0) {
+                    format.setMaximumWidth(w);
                 }
+                value = format.format(this, new StringBuffer(), null).toString();
             }
-            final AngleFormat format = new AngleFormat(formatter.locale());
-            if (precision > 0) {
-                format.setMaximumWidth(precision);
-            }
-            value = format.format(this, new StringBuffer(), null).toString();
         }
-        org.apache.sis.internal.util.Utilities.formatTo(formatter, flags, width, value);
+        Utilities.formatTo(formatter, flags, width, precision, value);
     }
 }

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/Range.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/Range.java?rev=1467349&r1=1467348&r2=1467349&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/Range.java [UTF-8]
(original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/Range.java [UTF-8]
Fri Apr 12 16:47:02 2013
@@ -16,9 +16,13 @@
  */
 package org.apache.sis.measure;
 
+import java.util.Formatter;
+import java.util.Formattable;
+import java.util.FormattableFlags;
 import java.io.Serializable;
 import javax.measure.unit.Unit;
 import net.jcip.annotations.Immutable;
+import org.apache.sis.internal.util.Utilities;
 import org.apache.sis.util.collection.CheckedContainer;
 import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.Numbers;
@@ -83,7 +87,7 @@ import java.util.Objects;
  * @see org.apache.sis.util.collection.RangeSet
  */
 @Immutable
-public class Range<E extends Comparable<? super E>> implements CheckedContainer<E>,
Serializable {
+public class Range<E extends Comparable<? super E>> implements CheckedContainer<E>,
Formattable, Serializable {
     /**
      * For cross-version compatibility.
      */
@@ -653,4 +657,31 @@ public class Range<E extends Comparable<
         }
         return buffer.toString();
     }
+
+    /**
+     * Formats this range using the provider formatter. This method is invoked when an
+     * {@code Range} object is formatted using the {@code "%s"} conversion specifier of
+     * {@link Formatter}. Users don't need to invoke this method explicitely.
+     *
+     * <p>If the alternate flags is present (as in {@code "%#s"}), then the range will
+     * be formatted using the {@linkplain RangeFormat#isAlternateForm() alternate form}
+     * for exclusive bounds.</p>
+     *
+     * @param formatter The formatter in which to format this angle.
+     * @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.
+     */
+    @Override
+    public void formatTo(final Formatter formatter, final int flags, final int width, int
precision) {
+        final String value;
+        if (precision == 0) {
+            value = "";
+        } else {
+            final RangeFormat format = new RangeFormat(formatter.locale(), elementType);
+            format.setAlternateForm((flags & FormattableFlags.ALTERNATE) != 0);
+            value = format.format(this, new StringBuffer(), null).toString();
+        }
+        Utilities.formatTo(formatter, flags, width, precision, value);
+    }
 }

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/RangeFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/RangeFormat.java?rev=1467349&r1=1467348&r2=1467349&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/RangeFormat.java [UTF-8]
(original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/RangeFormat.java [UTF-8]
Fri Apr 12 16:47:02 2013
@@ -249,6 +249,14 @@ public class RangeFormat extends Format 
     private final String infinity;
 
     /**
+     * {@code true} if {@code RangeFormat} shall use the alternate form at formatting time.
+     * This flag as no effect on parsing, since both forms are accepted.
+     *
+     * @see #isAlternateForm()
+     */
+    private boolean alternateForm;
+
+    /**
      * The type of the range components. Valid types are {@link Number}, {@link Angle},
      * {@link Date} or a subclass of those types. This value determines the kind of range
      * to be created by the parse method:
@@ -430,6 +438,29 @@ public class RangeFormat extends Format 
     }
 
     /**
+     * Returns {@code true} if this {@code RangeFormat} shall use the alternate form at
+     * formatting time. The alternate form expresses open intervals like {@code ]a…b[}
+     * instead of {@code (a…b)}.
+     *
+     * <p>This flag as no effect on parsing, since the parser accepts both forms.</p>
+     *
+     * @return {@code true} for using the alternate format instead of the default format.
+     */
+    public boolean isAlternateForm() {
+        return alternateForm;
+    }
+
+    /**
+     * Sets whether this {@code RangeFormat} shall use the alternate form at formatting time.
+     *
+     * @param alternateForm {@code true} for using the alternate format, or {@code false}
for
+     *        using the default format.
+     */
+    public void setAlternateForm(final boolean alternateForm) {
+        this.alternateForm = alternateForm;
+    }
+
+    /**
      * Returns the {@code *_FIELD} constant for the given field position, or -1 if none.
      */
     private static int getField(final FieldPosition position) {
@@ -521,8 +552,11 @@ public class RangeFormat extends Format 
             }
             field = MAX_VALUE_FIELD;
         }
-        toAppendTo.appendCodePoint(isSingleton ? openSet :
-                range.isMinIncluded() ? openInclusive : openExclusive);
+        toAppendTo.appendCodePoint( // Select the char for the first condition to be true
below:
+                isSingleton           ? openSet :
+                range.isMinIncluded() ? openInclusive :
+                alternateForm         ? openExclusiveAlt :
+                /* otherwise */         openExclusive);
         for (; field <= UNIT_FIELD; field++) {
             final Object value;
             switch (field) {
@@ -569,9 +603,12 @@ public class RangeFormat extends Format 
                     toAppendTo.append(' ').append(separator).append(' ');
                     break;
                 }
-                case MAX_VALUE_FIELD: {
-                    toAppendTo.appendCodePoint(isSingleton ? closeSet :
-                            range.isMaxIncluded() ? closeInclusive : closeExclusive);
+                case MAX_VALUE_FIELD: { // Select the char for the first condition to be
true below:
+                    toAppendTo.appendCodePoint(
+                            isSingleton           ? closeSet :
+                            range.isMaxIncluded() ? closeInclusive :
+                            alternateForm         ? closeExclusiveAlt :
+                            /* otherwise */         closeExclusive);
                     break;
                 }
             }

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/iso/AbstractInternationalString.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/iso/AbstractInternationalString.java?rev=1467349&r1=1467348&r2=1467349&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/iso/AbstractInternationalString.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/iso/AbstractInternationalString.java
[UTF-8] Fri Apr 12 16:47:02 2013
@@ -177,28 +177,8 @@ public abstract class AbstractInternatio
      *                  or -1 for no restriction.
      */
     @Override
-    public void formatTo(final Formatter formatter, int flags, final int width, int precision)
{
-        final Locale locale = formatter.locale();
-        String value = toString(locale);
-        if (precision >= 0) {
-            if ((flags & FormattableFlags.UPPERCASE) != 0) {
-                value = value.toUpperCase(locale); // May change the length in some locales.
-                flags &= ~FormattableFlags.UPPERCASE;
-            }
-            final int length = value.length();
-            if (precision < length) {
-                try {
-                    precision = value.offsetByCodePoints(0, precision);
-                } catch (IndexOutOfBoundsException e) {
-                    precision = length;
-                    // Happen if the string has fewer code-points than 'precision'. We could
-                    // avoid the try-catch block by checking value.codePointCount(…), but
it
-                    // would result in scanning the string twice.
-                }
-                value = value.substring(0, precision);
-            }
-        }
-        Utilities.formatTo(formatter, flags, width, value);
+    public void formatTo(final Formatter formatter, final int flags, final int width, final
int precision) {
+        Utilities.formatTo(formatter, flags, width, precision, toString(formatter.locale()));
     }
 
     /**

Modified: sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/measure/RangeFormatTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/measure/RangeFormatTest.java?rev=1467349&r1=1467348&r2=1467349&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/measure/RangeFormatTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/measure/RangeFormatTest.java
[UTF-8] Fri Apr 12 16:47:02 2013
@@ -192,6 +192,22 @@ public final strictfp class RangeFormatT
     }
 
     /**
+     * Tests the {@link RangeFormat#format(Object, StringBuffer, FieldPosition)} method
+     * using the alternate format.
+     */
+    @Test
+    public void testAlternateFormat() {
+        format = new RangeFormat(Locale.CANADA);
+        minPos = new FieldPosition(RangeFormat.Field.MIN_VALUE);
+        maxPos = new FieldPosition(RangeFormat.Field.MAX_VALUE);
+        format.setAlternateForm(true);
+
+        assertEquals("[-10 … 20]", format(NumberRange.create(-10, true, 20, true)));
+        assertEquals("]-3 … 4[",   format(NumberRange.create( -3, false, 4, false)));
+        assertEquals("[2 … 8[",    format(NumberRange.create(  2, true,  8, false)));
+    }
+
+    /**
      * Tests the parsing method on ranges of numbers. This test fixes the type to
      * {@code Integer.class}.  A different test will let the parser determine the
      * type itself.

Modified: sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/measure/RangeTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/measure/RangeTest.java?rev=1467349&r1=1467348&r2=1467349&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/measure/RangeTest.java [UTF-8]
(original)
+++ sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/measure/RangeTest.java [UTF-8]
Fri Apr 12 16:47:02 2013
@@ -16,6 +16,7 @@
  */
 package org.apache.sis.measure;
 
+import java.util.Locale;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
 
@@ -306,6 +307,19 @@ public final strictfp class RangeTest ex
     }
 
     /**
+     * Tests the {@link Range#formatTo(Formatter, int, int, int)} method.
+     */
+    @Test
+    public void testFormatTo() {
+        final Range<Integer> range = new Range<>(Integer.class, 10, true,  20,
false);
+        assertEquals("[10 … 20)",    String.format(Locale.CANADA, "%s", range));
+        assertEquals("[10 … 20)",    String.format(Locale.CANADA, "%4s", range));
+        assertEquals("[10 … 20)   ", String.format(Locale.CANADA, "%-12s", range));
+        assertEquals("   [10 … 20)", String.format(Locale.CANADA, "%12s", range));
+        assertEquals("[10 … 20[   ", String.format(Locale.CANADA, "%#-12s", range));
+    }
+
+    /**
      * Tests the {@link Range#equals(Object)} method.
      */
     @Test

Modified: sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultInternationalStringTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultInternationalStringTest.java?rev=1467349&r1=1467348&r2=1467349&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultInternationalStringTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultInternationalStringTest.java
[UTF-8] Fri Apr 12 16:47:02 2013
@@ -108,11 +108,11 @@ public final strictfp class DefaultInter
         assertEquals("English message:",     MESSAGE_en, String.format(Locale.ENGLISH, "%s",
toTest));
         assertEquals("French message:",      MESSAGE_fr, String.format(Locale.FRENCH,  "%s",
toTest));
 
-        assertEquals("  This", String.format(Locale.ROOT,    "%6.4s",  toTest));
-        assertEquals("  This", String.format(Locale.ENGLISH, "%6.4s",  toTest));
-        assertEquals(" Voici", String.format(Locale.FRENCH,  "%6.5s",  toTest));
-        assertEquals("THIS  ", String.format(Locale.ROOT,    "%-6.5S", toTest));
-        assertEquals("THIS  ", String.format(Locale.ENGLISH, "%-6.5S", toTest));
-        assertEquals("VOICI ", String.format(Locale.FRENCH,  "%-6.5S", toTest));
+        assertEquals("  Thi…", String.format(Locale.ROOT,    "%6.4s",  toTest));
+        assertEquals("  Thi…", String.format(Locale.ENGLISH, "%6.4s",  toTest));
+        assertEquals(" Voic…", String.format(Locale.FRENCH,  "%6.5s",  toTest));
+        assertEquals("THIS… ", String.format(Locale.ROOT,    "%-6.5S", toTest));
+        assertEquals("THIS… ", String.format(Locale.ENGLISH, "%-6.5S", toTest));
+        assertEquals("VOIC… ", String.format(Locale.FRENCH,  "%-6.5S", toTest));
     }
 }

Modified: sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/iso/SimpleInternationalStringTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/iso/SimpleInternationalStringTest.java?rev=1467349&r1=1467348&r2=1467349&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/iso/SimpleInternationalStringTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/iso/SimpleInternationalStringTest.java
[UTF-8] Fri Apr 12 16:47:02 2013
@@ -60,4 +60,17 @@ public final strictfp class SimpleIntern
         assertEquals(MESSAGE, after.toString(Locale.JAPANESE));
         validate(after);
     }
+
+    /**
+     * Tests the formatting in a {@code printf} statement.
+     */
+    @Test
+    public void testPrintf() {
+        final SimpleInternationalString toTest = new SimpleInternationalString(MESSAGE);
+        assertEquals(MESSAGE,                               String.format("%s", toTest));
+        assertEquals("    This is an unlocalized message.", String.format("%35s", toTest));
+        assertEquals("This is an unlocalized message.    ", String.format("%-35s", toTest));
+        assertEquals("This is a…",                          String.format("%1.10s", toTest));
+        assertEquals("This is a…  ",                        String.format("%-12.10s", toTest));
+    }
 }



Mime
View raw message