sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1587128 - in /sis/branches/JDK8: core/sis-utility/src/main/java/org/apache/sis/internal/jdk8/ core/sis-utility/src/main/java/org/apache/sis/internal/util/ core/sis-utility/src/test/java/org/apache/sis/internal/jdk8/ core/sis-utility/src/te...
Date Mon, 14 Apr 2014 00:19:25 GMT
Author: desruisseaux
Date: Mon Apr 14 00:19:25 2014
New Revision: 1587128

URL: http://svn.apache.org/r1587128
Log:
Move the "date string to ISO 8601" method to the NetCDF module, because it was used only
in the context of NetCDF file. This move will make easier to replace the JDK8 methods by
java.util.time formatter.

Added:
    sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/impl/AttributeTest.java
      - copied, changed from r1585798, sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/internal/jdk8/JDK8Test.java
Removed:
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/internal/jdk8/
Modified:
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/jdk8/JDK8.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/Utilities.java
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
    sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Variable.java
    sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/Attribute.java
    sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java
    sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/test/suite/NetcdfTestSuite.java

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/jdk8/JDK8.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/jdk8/JDK8.java?rev=1587128&r1=1587127&r2=1587128&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/jdk8/JDK8.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/jdk8/JDK8.java
[UTF-8] Mon Apr 14 00:19:25 2014
@@ -23,7 +23,6 @@ import java.util.Locale;
 import java.util.TimeZone;
 import java.util.concurrent.atomic.AtomicReference;
 import javax.xml.bind.DatatypeConverter;
-import org.apache.sis.util.CharSequences;
 
 
 /**
@@ -32,7 +31,7 @@ import org.apache.sis.util.CharSequences
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.3
- * @version 0.4
+ * @version 0.5
  * @module
  */
 public final class JDK8 {
@@ -51,101 +50,18 @@ public final class JDK8 {
 
     /**
      * Parses a date from a string in ISO 8601 format. More specifically, this method expects
the
-     * format defined by <cite>XML Schema Part 2: Datatypes for {@code xsd:dateTime}</cite>,
with
-     * some additional flexibility (e.g. missing minutes or seconds fields are automatically
added).
+     * format defined by <cite>XML Schema Part 2: Datatypes for {@code xsd:dateTime}</cite>.
      *
      * <p>This method will be replaced by {@link java.time.format.DateTimeFormatter}
on the JDK8 branch.</p>
      *
-     * @param  date The date to parse, or {@code null}.
-     * @param  defaultToUTC {@code true} if the default timezone shall be UTL, or {@code
false} for locale default.
-     * @return The parsed date, or {@code null} if the given string was {@code null}.
+     * @param  date The date to parse.
+     * @return The parsed date.
      * @throws IllegalArgumentException if the given date can not be parsed.
      *
      * @see DatatypeConverter#parseDateTime(String)
      */
-    public static Date parseDateTime(String date, final boolean defaultToUTC) throws IllegalArgumentException
{
-        if (date != null) {
-            date = CharSequences.trimWhitespaces(date);
-            if (!date.isEmpty()) {
-                /*
-                 * Check for missing time fields and time zone. For example if the given
date is
-                 * "2005-09-22T00:00", then this block will complete it as "2005-09-22T00:00:00".
-                 * In addition, a 'Z' suffix will be appended if 'defaultToUTC' is true.
-                 */
-                int timeFieldStart  = date.lastIndexOf('T') + 1; // 0 if there is no time
field.
-                int timeFieldEnd    = date.length();             // To be updated if there
is a time field.
-                int missingFields   = 0;                         // Number of missing time
fields.
-                boolean hasTimeZone = date.charAt(timeFieldEnd - 1) == 'Z';
-                if (timeFieldStart != 0) {
-                    if (hasTimeZone) {
-                        timeFieldEnd--;
-                    } else {
-                        final int s = Math.max(date.indexOf('+', timeFieldStart),
-                                               date.indexOf('-', timeFieldStart));
-                        hasTimeZone = (s >= 0);
-                        if (hasTimeZone) {
-                            timeFieldEnd = s;
-                        }
-                    }
-                    missingFields = 2;
-                    for (int i=timeFieldStart; i<timeFieldEnd; i++) {
-                        if (date.charAt(i) == ':') {
-                            if (--missingFields == 0) break;
-                        }
-                    }
-                }
-                /*
-                 * At this point, we have determined if there is some missing fields.
-                 * The timezone will be considered missing only if 'defaultToUTC' is true.
-                 */
-                CharSequence modified = date;
-                hasTimeZone |= !defaultToUTC;
-                if (missingFields != 0 || !hasTimeZone) {
-                    final StringBuilder buffer = new StringBuilder(date);
-                    while (--missingFields >= 0) {
-                        buffer.append(":00");
-                    }
-                    if (!hasTimeZone) {
-                        buffer.append('Z');
-                    }
-                    modified = buffer;
-                }
-                /*
-                 * Now ensure that all numbers have at least two digits.
-                 */
-                int indexOfLastDigit = 0;
-                for (int i=modified.length(); --i >= 0;) {
-                    char c = modified.charAt(i);
-                    final boolean isDigit = (c >= '0' && c <= '9'); // Do not
use Character.isDigit(char).
-                    if (indexOfLastDigit == 0) {
-                        // We were not scaning a number. Check if we are now starting doing
so.
-                        if (isDigit) {
-                            indexOfLastDigit = i;
-                        }
-                    } else {
-                        // We were scaning a number. Check if we found the begining.
-                        if (!isDigit) {
-                            if (indexOfLastDigit - i == 1) {
-                                // Reuse the buffer if it exists, or create a new one otherwise.
-                                final StringBuilder buffer;
-                                if (modified == date) {
-                                    modified = buffer = new StringBuilder(date);
-                                } else {
-                                    buffer = (StringBuilder) modified;
-                                }
-                                buffer.insert(i+1, '0');
-                            }
-                            indexOfLastDigit = 0;
-                        }
-                    }
-                }
-                /*
-                 * Now delegate to the utility method provided in the JAXB packages.
-                 */
-                return DatatypeConverter.parseDateTime(modified.toString()).getTime();
-            }
-        }
-        return null;
+    public static Date parseDateTime(final String date) throws IllegalArgumentException {
+        return DatatypeConverter.parseDateTime(date).getTime();
     }
 
     /**
@@ -154,15 +70,12 @@ public final class JDK8 {
      *
      * <p>This method will be replaced by {@link java.time.format.DateTimeFormatter}
on the JDK8 branch.</p>
      *
-     * @param  date The date to format, or {@code null}.
-     * @return The formatted date, or {@code null} if the given date was null.
+     * @param  date The date to format.
+     * @return The formatted date.
      *
      * @see DatatypeConverter#printDateTime(Calendar)
      */
     public static String printDateTime(final Date date) {
-        if (date == null) {
-            return null;
-        }
         Calendar calendar = CALENDAR.getAndSet(null);
         if (calendar == null) {
             calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"), Locale.US);

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/Utilities.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/Utilities.java?rev=1587128&r1=1587127&r2=1587128&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/Utilities.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/Utilities.java
[UTF-8] Mon Apr 14 00:19:25 2014
@@ -91,7 +91,7 @@ public final class Utilities extends Sta
      * used mostly for debugging purpose.
      *
      * @param  classe     The class to format.
-     * @param  properties The (<var>key</var>=</var>value</var>)
pairs.
+     * @param  properties The (<var>key</var>=<var>value</var>) pairs.
      * @return A string representation of an instance of the given class having the given
properties.
      *
      * @since 0.4

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=1587128&r1=1587127&r2=1587128&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 14 00:19:25 2014
@@ -36,7 +36,6 @@ import org.junit.BeforeClass;
     org.apache.sis.internal.test.XMLComparatorTest.class,
 
     // Most basic functions of SIS library.
-    org.apache.sis.internal.jdk8.JDK8Test.class,
     org.apache.sis.internal.util.NumericsTest.class,
     org.apache.sis.setup.OptionKeyTest.class,
     org.apache.sis.util.ArraysExtTest.class,

Modified: sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Variable.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Variable.java?rev=1587128&r1=1587127&r2=1587128&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Variable.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Variable.java
[UTF-8] Mon Apr 14 00:19:25 2014
@@ -206,6 +206,8 @@ public abstract class Variable {
 
     /**
      * Returns a string representation of this variable for debugging purpose.
+     *
+     * @return A string representation of this variable.
      */
     @Debug
     @Override

Modified: sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/Attribute.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/Attribute.java?rev=1587128&r1=1587127&r2=1587128&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/Attribute.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/Attribute.java
[UTF-8] Mon Apr 14 00:19:25 2014
@@ -19,6 +19,7 @@ package org.apache.sis.internal.netcdf.i
 import java.lang.reflect.Array;
 import org.apache.sis.util.Debug;
 import org.apache.sis.util.Utilities;
+import org.apache.sis.util.CharSequences;
 
 // Related to JDK8
 import java.util.function.Function;
@@ -30,7 +31,7 @@ import java.util.function.Function;
  * @author  Johann Sorel (Geomatys)
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.3
- * @version 0.3
+ * @version 0.5
  * @module
  */
 final class Attribute {
@@ -97,6 +98,99 @@ final class Attribute {
     }
 
     /**
+     * Modifies, if needed, the given date in order to make it compliant with the ISO 8601
format.
+     * For example missing minutes or seconds fields are automatically added. The intend
is to turn
+     * a NetCDF date into something parseable by {@code java.util.time} or {@code javax.xml.bind}.
+     *
+     * @param  date The date to parse, or {@code null}.
+     * @return The date modified if needed or {@code null} if the given string was {@code
null}.
+     *
+     * @since 0.5 (derived from 0.3)
+     */
+    static String dateToISO(String date) {
+        date = CharSequences.trimWhitespaces(date);
+        if (date != null && !date.isEmpty()) {
+            /*
+             * Check for missing time fields and time zone. For example if the given date
is
+             * "2005-09-22T00:00", then this block will complete it as "2005-09-22T00:00:00".
+             * In addition, a 'Z' suffix will be appended if 'defaultToUTC' is true.
+             */
+            int timeFieldStart  = date.lastIndexOf('T') + 1; // 0 if there is no time field.
+            int timeFieldEnd    = date.length();             // To be updated if there is
a time field.
+            int missingFields   = 2;                         // Number of missing time fields.
+            boolean hasTimeZone = date.charAt(timeFieldEnd - 1) == 'Z';
+            if (timeFieldStart != 0) {
+                if (hasTimeZone) {
+                    timeFieldEnd--;
+                } else {
+                    final int s = Math.max(date.indexOf('+', timeFieldStart),
+                                           date.indexOf('-', timeFieldStart));
+                    if (hasTimeZone = (s >= 0)) {
+                        timeFieldEnd = s;
+                    }
+                }
+                for (int i=timeFieldStart; i<timeFieldEnd; i++) {
+                    if (date.charAt(i) == ':') {
+                        if (--missingFields == 0) break;
+                    }
+                }
+            }
+            /*
+             * If we have determined that there is some missing time fields,
+             * append default values for them.
+             */
+            CharSequence modified = date;
+            if (missingFields != 0 || !hasTimeZone) {
+                final StringBuilder buffer = new StringBuilder(date);
+                buffer.setLength(timeFieldEnd);
+                if (timeFieldStart == 0) {
+                    buffer.append("T00");
+                }
+                while (--missingFields >= 0) {
+                    buffer.append(":00");
+                }
+                if (hasTimeZone) {
+                    buffer.append(date, timeFieldEnd, date.length());
+                } else {
+                    buffer.append('Z');
+                }
+                modified = buffer;
+            }
+            /*
+             * Now ensure that all numbers have at least two digits.
+             */
+            int indexOfLastDigit = 0;
+            for (int i=modified.length(); --i >= 0;) {
+                char c = modified.charAt(i);
+                final boolean isDigit = (c >= '0' && c <= '9'); // Do not use
Character.isDigit(char).
+                if (indexOfLastDigit == 0) {
+                    // We were not scaning a number. Check if we are now starting doing so.
+                    if (isDigit) {
+                        indexOfLastDigit = i;
+                    }
+                } else {
+                    // We were scaning a number. Check if we found the begining.
+                    if (!isDigit) {
+                        if (indexOfLastDigit - i == 1) {
+                            // Reuse the buffer if it exists, or create a new one otherwise.
+                            final StringBuilder buffer;
+                            if (modified == date) {
+                                modified = buffer = new StringBuilder(date);
+                            } else {
+                                buffer = (StringBuilder) modified;
+                            }
+                            buffer.insert(i+1, '0');
+                        }
+                        indexOfLastDigit = 0;
+                    }
+                }
+            }
+            date = modified.toString();
+        }
+        return date;
+    }
+
+    /**
      * A string representation of this dimension for debugging purpose only.
      */
     @Debug

Modified: sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java?rev=1587128&r1=1587127&r2=1587128&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java
[UTF-8] Mon Apr 14 00:19:25 2014
@@ -107,12 +107,6 @@ public final class ChannelDecoder extend
      */
     private static final Pattern TIME_UNIT_PATTERN = Pattern.compile("(?i)\\bsince\\b");
 
-    /**
-     * {@code true} if the default timezone is UTC, or {@code false} if it shall be the
-     * {@linkplain java.util.TimeZone#getDefault() system default}.
-     */
-    private static final boolean DEFAULT_TIMEZONE_IS_UTC = true;
-
     /*
      * NOTE: the names of the static constants below this point match the names used in the
Backus-Naur Form (BNF)
      *       definitions in the NetCDF Classic and 64-bit Offset Format (1.0) specification
(link in class javdoc),
@@ -639,7 +633,7 @@ public final class ChannelDecoder extend
         final Attribute attribute = findAttribute(name);
         if (attribute != null) {
             if (attribute.value instanceof String) try {
-                return JDK8.parseDateTime((String) attribute.value, DEFAULT_TIMEZONE_IS_UTC);
+                return JDK8.parseDateTime(Attribute.dateToISO((String) attribute.value));
             } catch (IllegalArgumentException e) {
                 listeners.warning(null, e);
             }
@@ -661,7 +655,7 @@ public final class ChannelDecoder extend
         final String[] parts = TIME_UNIT_PATTERN.split(symbol);
         if (parts.length == 2) try {
             final UnitConverter converter = Units.valueOf(parts[0]).getConverterToAny(Units.MILLISECOND);
-            final long epoch = JDK8.parseDateTime(parts[1], DEFAULT_TIMEZONE_IS_UTC).getTime();
+            final long epoch = JDK8.parseDateTime(Attribute.dateToISO(parts[1])).getTime();
             for (int i=0; i<values.length; i++) {
                 final Number value = values[i];
                 if (value != null) {

Copied: sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/impl/AttributeTest.java
(from r1585798, sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/internal/jdk8/JDK8Test.java)
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/impl/AttributeTest.java?p2=sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/impl/AttributeTest.java&p1=sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/internal/jdk8/JDK8Test.java&r1=1585798&r2=1587128&rev=1587128&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/internal/jdk8/JDK8Test.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/impl/AttributeTest.java
[UTF-8] Mon Apr 14 00:19:25 2014
@@ -14,48 +14,34 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.sis.internal.jdk8;
+package org.apache.sis.internal.netcdf.impl;
 
-import java.util.Date;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
 
 import static org.apache.sis.test.Assert.*;
-import static org.apache.sis.test.TestUtilities.date;
 
 
 /**
- * Tests the {@link JDK8} class.
+ * Tests the {@link Attribute} class.
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @since   0.3
- * @version 0.3
+ * @since   0.5 (derived from 0.3)
+ * @version 0.5
  * @module
  */
-public final strictfp class JDK8Test extends TestCase {
+public final strictfp class AttributeTest extends TestCase {
     /**
-     * Tests the {@link Utilities#parseDateTime(String)} method with an explicit time zone.
+     * Tests the {@link Attribute#dateToISO(String)} method.
      */
     @Test
-    public void testParseDateTime() {
-        final Date expected = date("2009-01-01 05:00:00");
-        assertEquals(expected, JDK8.parseDateTime("2009-01-01T06:00:00+01:00", false));
-        assertEquals(expected, JDK8.parseDateTime("2009-01-01T06:00:00+01:00", true));
-    }
-
-    /**
-     * Tests the {@link Utilities#parseDateTime(String)} method for a date in UTC time zone,
-     * either explicit or implicit.
-     */
-    @Test
-    public void testParseDateTimeUTC() {
-        final Date expected = date("2005-09-22 00:00:00");
-        assertEquals(expected, JDK8.parseDateTime("2005-09-22T00:00:00Z", false));
-        assertEquals(expected, JDK8.parseDateTime("2005-09-22T00:00:00Z", true));
-        assertEquals(expected, JDK8.parseDateTime("2005-09-22T00:00:00",  true));
-        assertEquals(expected, JDK8.parseDateTime("2005-09-22T00:00",     true));
-        assertEquals(expected, JDK8.parseDateTime("2005-09-22T00",        true));
-        assertEquals(expected, JDK8.parseDateTime("2005-09-22",           true));
-        assertEquals(expected, JDK8.parseDateTime("2005-9-22",            true));
+    public void testDateToISO() {
+        assertEquals("2009-01-01T06:00:00+01:00", Attribute.dateToISO("2009-01-01T06:00:00+01:00"));
+        assertEquals("2005-09-22T04:30:15Z",      Attribute.dateToISO("2005-09-22T04:30:15Z"));
+        assertEquals("2005-09-22T04:30:15Z",      Attribute.dateToISO("2005-09-22T04:30:15"));
+        assertEquals("2005-09-22T04:30:00Z",      Attribute.dateToISO("2005-09-22T04:30"));
+        assertEquals("2005-09-22T04:00:00Z",      Attribute.dateToISO("2005-09-22T04"));
+        assertEquals("2005-09-22T00:00:00Z",      Attribute.dateToISO("2005-09-22"));
+        assertEquals("2005-09-22T00:00:00Z",      Attribute.dateToISO("2005-9-22"));
     }
 }

Modified: sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/test/suite/NetcdfTestSuite.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/test/suite/NetcdfTestSuite.java?rev=1587128&r1=1587127&r2=1587128&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/test/suite/NetcdfTestSuite.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/test/suite/NetcdfTestSuite.java
[UTF-8] Mon Apr 14 00:19:25 2014
@@ -35,6 +35,7 @@ import org.junit.BeforeClass;
     org.apache.sis.internal.netcdf.DecoderTest.class,
     org.apache.sis.internal.netcdf.VariableTest.class,
     org.apache.sis.internal.netcdf.GridGeometryTest.class,
+    org.apache.sis.internal.netcdf.impl.AttributeTest.class,
     org.apache.sis.internal.netcdf.impl.ChannelDecoderTest.class,
     org.apache.sis.internal.netcdf.impl.VariableInfoTest.class,
     org.apache.sis.internal.netcdf.impl.GridGeometryInfoTest.class,



Mime
View raw message