sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1760700 - in /sis/branches/JDK8: core/sis-utility/src/main/java/org/apache/sis/internal/util/ core/sis-utility/src/test/java/org/apache/sis/internal/util/ storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ storage/sis-ne...
Date Wed, 14 Sep 2016 12:51:10 GMT
Author: desruisseaux
Date: Wed Sep 14 12:51:10 2016
New Revision: 1760700

URL: http://svn.apache.org/viewvc?rev=1760700&view=rev
Log:
Remove NetCDF Attribute.toIsoDate(String) method, which is replaced by use of java.time.format
on JDK8.

Modified:
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/StandardDateFormat.java
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/internal/util/StandardDateFormatTest.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/internal/netcdf/impl/AttributeTest.java

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/StandardDateFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/StandardDateFormat.java?rev=1760700&r1=1760699&r2=1760700&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/StandardDateFormat.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/StandardDateFormat.java
[UTF-8] Wed Sep 14 12:51:10 2016
@@ -85,14 +85,15 @@ public final class StandardDateFormat ex
      * The thread-safe instance to use for reading and formatting dates.
      * Only the year is mandatory, all other fields are optional.
      */
-    public static final DateTimeFormatter FORMAT =
-            new DateTimeFormatterBuilder()     .appendValue(ChronoField.YEAR,           
 4)    // Proleptic year (use negative number if needed).
+    public static final DateTimeFormatter FORMAT = new DateTimeFormatterBuilder()
+            // parseLenient() is for allowing fields with one digit instead of two.
+            .parseLenient()                    .appendValue(ChronoField.YEAR,           
 4)    // Proleptic year (use negative number if needed).
             .optionalStart().appendLiteral('-').appendValue(ChronoField.MONTH_OF_YEAR,  
 2)
             .optionalStart().appendLiteral('-').appendValue(ChronoField.DAY_OF_MONTH,   
 2)
             .optionalStart().appendLiteral('T').appendValue(ChronoField.HOUR_OF_DAY,    
 2)
             .optionalStart().appendLiteral(':').appendValue(ChronoField.MINUTE_OF_HOUR, 
 2)
             .optionalStart().appendLiteral(':').appendValue(ChronoField.SECOND_OF_MINUTE,
2)
-                            .parseLenient() .appendFraction(ChronoField.MILLI_OF_SECOND,
3, 3, true)
+                                               .appendFraction(ChronoField.MILLI_OF_SECOND,
3, 3, true)
             .optionalEnd().optionalEnd().optionalEnd()    // Move back to the optional block
of HOUR_OF_DAY.
             .optionalStart().appendOffsetId()
             .toFormatter(Locale.ROOT);
@@ -149,15 +150,18 @@ public final class StandardDateFormat ex
         if (temporal == null) {
             return null;
         }
-        final long millis;
+        long millis;
         if (temporal instanceof Instant) {
             millis = ((Instant) temporal).toEpochMilli();
         } else if (temporal.isSupported(ChronoField.INSTANT_SECONDS)) {
-            millis = Math.addExact(Math.multiplyExact(temporal.getLong(ChronoField.INSTANT_SECONDS),
1000),
-                            temporal.get(ChronoField.NANO_OF_SECOND) / 1000000);
+            millis = Math.multiplyExact(temporal.getLong(ChronoField.INSTANT_SECONDS), 1000);
+            millis = Math.addExact(millis, temporal.getLong(ChronoField.NANO_OF_SECOND) /
1000000);
         } else {
             // Note that the timezone may be unknown here. We assume UTC.
             millis = Math.multiplyExact(temporal.getLong(ChronoField.EPOCH_DAY), MILLISECONDS_PER_DAY);
+            if (temporal.isSupported(ChronoField.MILLI_OF_DAY)) {
+                millis = Math.addExact(millis, temporal.getLong(ChronoField.MILLI_OF_DAY));
+            }
         }
         return new Date(millis);
     }

Modified: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/internal/util/StandardDateFormatTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/internal/util/StandardDateFormatTest.java?rev=1760700&r1=1760699&r2=1760700&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/internal/util/StandardDateFormatTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/internal/util/StandardDateFormatTest.java
[UTF-8] Wed Sep 14 12:51:10 2016
@@ -20,6 +20,7 @@ import java.text.ParseException;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
 
+import static org.apache.sis.test.TestUtilities.date;
 import static org.junit.Assert.*;
 
 
@@ -47,5 +48,14 @@ public final strictfp class StandardDate
         assertEquals("millis", day + ((16*60 + 48)*60 + 12)*1000,      f.parse("2016-06-27T16:48:12Z")
  .getTime());
         assertEquals("millis", day,                                    f.parse("2016-06-27")
            .getTime());
         assertEquals("millis", day + (( 3*60 +  2)*60 +  1)*1000 + 90, f.parse("2016-06-27T03:02:01.09Z").getTime());
+
+        assertEquals(date("2009-01-01 05:00:00"), f.parse("2009-01-01T06:00:00+01:00"));
+        assertEquals(date("2005-09-22 04:30:15"), f.parse("2005-09-22T04:30:15Z"));
+        assertEquals(date("2005-09-22 04:30:15"), f.parse("2005-09-22T04:30:15"));
+        assertEquals(date("2005-09-22 04:30:00"), f.parse("2005-09-22T04:30"));
+        assertEquals(date("2005-09-22 04:00:00"), f.parse("2005-09-22T04"));
+        assertEquals(date("2005-09-22 00:00:00"), f.parse("2005-09-22"));
+        assertEquals(date("2005-09-22 00:00:00"), f.parse("2005-9-22"));
+        assertEquals(date("1992-01-01 00:00:00"), f.parse("1992-1-1"));
     }
 }

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=1760700&r1=1760699&r2=1760700&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] Wed Sep 14 12:51:10 2016
@@ -19,7 +19,6 @@ 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;
 
 // Branch-dependent imports
 import java.util.function.Function;
@@ -31,7 +30,7 @@ import java.util.function.Function;
  * @author  Johann Sorel (Geomatys)
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.3
- * @version 0.5
+ * @version 0.8
  * @module
  */
 final class Attribute {
@@ -104,99 +103,6 @@ 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=1760700&r1=1760699&r2=1760700&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] Wed Sep 14 12:51:10 2016
@@ -26,7 +26,6 @@ import java.util.Arrays;
 import java.util.Date;
 import java.util.Locale;
 import java.util.regex.Pattern;
-import java.time.Instant;
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.nio.channels.ReadableByteChannel;
@@ -39,6 +38,7 @@ import org.apache.sis.internal.netcdf.Va
 import org.apache.sis.internal.netcdf.GridGeometry;
 import org.apache.sis.internal.storage.ChannelDataInput;
 import org.apache.sis.internal.util.CollectionsExt;
+import org.apache.sis.internal.util.StandardDateFormat;
 import org.apache.sis.storage.DataStoreException;
 import org.apache.sis.storage.DataStoreContentException;
 import org.apache.sis.util.iso.DefaultNameSpace;
@@ -51,6 +51,7 @@ import org.apache.sis.measure.Units;
 
 // Branch-dependent imports
 import java.util.function.Function;
+import java.time.DateTimeException;
 
 
 /**
@@ -106,7 +107,7 @@ public final class ChannelDecoder extend
      *
      * @see #numberToDate(String, Number[])
      */
-    private static final Pattern TIME_UNIT_PATTERN = Pattern.compile("(?i)\\bsince\\b");
+    private static final Pattern TIME_UNIT_PATTERN = Pattern.compile("\\s+since\\s+", Pattern.CASE_INSENSITIVE);
 
     /*
      * NOTE: the names of the static constants below this point match the names used in the
Backus-Naur Form (BNF)
@@ -638,9 +639,9 @@ public final class ChannelDecoder extend
     public Date dateValue(final String name) throws IOException {
         final Attribute attribute = findAttribute(name);
         if (attribute != null) {
-            if (attribute.value instanceof String) try {
-                return Date.from(Instant.parse(Attribute.dateToISO((String) attribute.value)));
-            } catch (IllegalArgumentException e) {
+            if (attribute.value instanceof CharSequence) try {
+                return StandardDateFormat.toDate(StandardDateFormat.FORMAT.parse((CharSequence)
attribute.value));
+            } catch (DateTimeException | ArithmeticException e) {
                 listeners.warning(null, e);
             }
         }
@@ -651,8 +652,8 @@ public final class ChannelDecoder extend
      * Converts the given numerical values to date, using the information provided in the
given unit symbol.
      * The unit symbol is typically a string like <cite>"days since 1970-01-01T00:00:00Z"</cite>.
      *
-     * @param  values  the values to convert. May contains {@code null} elements.
-     * @return the converted values. May contains {@code null} elements.
+     * @param  values  the values to convert. May contain {@code null} elements.
+     * @return the converted values. May contain {@code null} elements.
      * @throws IOException {@inheritDoc}
      */
     @Override
@@ -661,14 +662,14 @@ 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 = Instant.parse(Attribute.dateToISO(parts[1])).toEpochMilli();
+            final long epoch = StandardDateFormat.toDate(StandardDateFormat.FORMAT.parse(parts[1])).getTime();
             for (int i=0; i<values.length; i++) {
                 final Number value = values[i];
                 if (value != null) {
                     dates[i] = new Date(epoch + Math.round(converter.convert(value.doubleValue())));
                 }
             }
-        } catch (ConversionException | IllegalArgumentException e) {
+        } catch (ConversionException | DateTimeException | ArithmeticException e) {
             listeners.warning(null, e);
         }
         return dates;

Modified: sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/impl/AttributeTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/impl/AttributeTest.java?rev=1760700&r1=1760699&r2=1760700&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/impl/AttributeTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/impl/AttributeTest.java
[UTF-8] Wed Sep 14 12:51:10 2016
@@ -19,7 +19,7 @@ package org.apache.sis.internal.netcdf.i
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
 
-import static org.apache.sis.test.Assert.*;
+import static org.junit.Assert.*;
 
 
 /**
@@ -27,21 +27,17 @@ import static org.apache.sis.test.Assert
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.5 (derived from 0.3)
- * @version 0.5
+ * @version 0.8
  * @module
  */
 public final strictfp class AttributeTest extends TestCase {
     /**
-     * Tests the {@link Attribute#dateToISO(String)} method.
+     * Tests the {@link Attribute#numberValues()} method.
      */
     @Test
-    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"));
+    public void testNumberValues() {
+        final Attribute a = new Attribute("aName", new float[] {10, 20, 1});
+        assertArrayEquals("numberValues", new Number[] { 10f,    20f,    1f  }, a.numberValues());
+        assertArrayEquals("stringValues", new String[] {"10.0", "20.0", "1.0"}, a.stringValues());
     }
 }



Mime
View raw message