sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1457162 - in /sis/branches/JDK7/sis-utility/src: main/java/org/apache/sis/internal/converter/ test/java/org/apache/sis/internal/converter/ test/java/org/apache/sis/test/suite/
Date Fri, 15 Mar 2013 23:38:59 GMT
Author: desruisseaux
Date: Fri Mar 15 23:38:58 2013
New Revision: 1457162

URL: http://svn.apache.org/r1457162
Log:
Consolidation of File, URL and URI converters in a single PathConverter class.

Added:
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/PathConverter.java
      - copied, changed from r1457152, sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/FileConverter.java
    sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/internal/converter/PathConverterTest.java
      - copied, changed from r1457152, sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/internal/converter/FileConverterTest.java
Removed:
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/FileConverter.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/InjectiveConverter.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/URIConverter.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/URLConverter.java
    sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/internal/converter/FileConverterTest.java
    sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/internal/converter/URIConverterTest.java
    sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/internal/converter/URLConverterTest.java
Modified:
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/SurjectiveConverter.java
    sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java

Copied: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/PathConverter.java
(from r1457152, sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/FileConverter.java)
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/PathConverter.java?p2=sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/PathConverter.java&p1=sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/FileConverter.java&r1=1457152&r2=1457162&rev=1457162&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/FileConverter.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/PathConverter.java
[UTF-8] Fri Mar 15 23:38:58 2013
@@ -16,17 +16,21 @@
  */
 package org.apache.sis.internal.converter;
 
+import java.util.Set;
+import java.util.EnumSet;
 import java.io.File;
-import java.io.Serializable;
-import java.io.ObjectStreamException;
+import java.net.URL;
+import java.net.URI;
 import java.net.MalformedURLException;
+import java.net.URISyntaxException;
 import net.jcip.annotations.Immutable;
+import org.apache.sis.math.FunctionProperty;
 import org.apache.sis.util.ObjectConverter;
 import org.apache.sis.util.UnconvertibleObjectException;
 
 
 /**
- * Handles conversions from {@link File} to various objects.
+ * Handles conversions between {@link Path}, {@link File}, {@link URI} and {@link URL} objects.
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.3 (derived from geotk-3.01)
@@ -34,85 +38,173 @@ import org.apache.sis.util.Unconvertible
  * @module
  */
 @Immutable
-abstract class FileConverter<T> extends InjectiveConverter<File,T> implements
Serializable {
+abstract class PathConverter<S,T> extends SystemConverter<S,T> {
     /**
      * For cross-version compatibility.
      */
     private static final long serialVersionUID = -2150865427977735620L;
 
     /**
-     * For inner classes only.
+     * Creates a path converter from the given source class to the given  target class.
      */
-    FileConverter() {
+    PathConverter(final Class<S> sourceClass, final Class<T> targetClass) {
+        super(sourceClass, targetClass);
     }
 
     /**
-     * Returns the source class, which is always {@link File}.
+     * Returns a predefined instance for the given target class, or {@code null} if none.
+     * This method does not create any new instance.
+     *
+     * @param  <T> The target class.
+     * @param  targetClass The target class.
+     * @return An instance for the given target class, or {@code null} if none.
+     */
+    @SuppressWarnings({"unchecked","rawtypes"})
+    static <S,T> PathConverter<S,T> getInstance(final Class<S> sourceClass,
final Class<T> targetClass) {
+        if (sourceClass == File.class) {
+            if (targetClass == URI.class) return (PathConverter<S,T>) FileURI.INSTANCE;
+            if (targetClass == URL.class) return (PathConverter<S,T>) FileURL.INSTANCE;
+        } else if (sourceClass == URL.class) {
+            if (targetClass == File.class) return (PathConverter<S,T>) URLFile.INSTANCE;
+            if (targetClass == URI .class) return (PathConverter<S,T>) URL_URI.INSTANCE;
+        } else if (sourceClass == URI.class) {
+            if (targetClass == File.class) return (PathConverter<S,T>) URIFile.INSTANCE;
+            if (targetClass == URL .class) return (PathConverter<S,T>) URI_URL.INSTANCE;
+        }
+        return null;
+    }
+
+    /**
+     * Returns the singleton instance on deserialization, if any.
      */
     @Override
-    public final Class<File> getSourceClass() {
-        return File.class;
+    public final ObjectConverter<S, T> unique() {
+        final PathConverter<S,T> instance = getInstance(sourceClass, targetClass);
+        return (instance != null) ? instance : super.unique();
     }
 
     /**
-     * Converter from {@link File} to {@link java.net.URI}.
-     * Note that this converter change relative paths to absolute paths.
+     * Returns the properties of this converter.
      */
-    @Immutable
-    static final class URI extends FileConverter<java.net.URI> {
-        /** Cross-version compatibility. */ static final long serialVersionUID = 1032598133849975567L;
-        /** The unique, shared instance. */ static final URI INSTANCE = new URI();
-        /** For {@link #INSTANCE} only.  */ private URI() {}
+    @Override
+    public final Set<FunctionProperty> properties() {
+        return EnumSet.of(FunctionProperty.INJECTIVE, FunctionProperty.INVERTIBLE);
+    }
 
-        @Override public Class<java.net.URI> getTargetClass() {
-            return java.net.URI.class;
+    /**
+     * Converts the given path to the target type of this converter.
+     * This method verifies that the given path is non-null,
+     * then delegates to {@link #doConvert(S)}.
+     *
+     * @param  source The path to convert, or {@code null}.
+     * @return The converted value, or {@code null} if the given path was null.
+     * @throws UnconvertibleObjectException If an error occurred during the conversion.
+     */
+    @Override
+    public final T convert(final S source) throws UnconvertibleObjectException {
+        if (source == null) {
+            return null;
+        }
+        try {
+            return doConvert(source);
+        } catch (Exception e) {
+            throw new UnconvertibleObjectException(formatErrorMessage(source), e);
         }
+    }
 
-        @Override public java.net.URI convert(final File source) {
-            return (source != null) ? source.toURI() : null;
-        }
+    /**
+     * Invoked by {@link #convert(S)} for converting the given path to the target
+     * type of this converter.
+     *
+     * @param  source The path to convert, guaranteed to be non-null.
+     * @return The converted path.
+     * @throws Exception If an error occurred during the conversion.
+     */
+    abstract T doConvert(S source) throws Exception;
 
-        @Override public ObjectConverter<java.net.URI, File> inverse() {
-            return URIConverter.File.INSTANCE;
+    /**
+     * Converter from {@link File} to {@link URI}.
+     * This converter changes relative paths to absolute paths.
+     */
+    private static final class FileURI extends PathConverter<File,URI> {
+        private static final long serialVersionUID = 1032598133849975567L;
+        static final FileURI INSTANCE = new FileURI();
+        private FileURI() {super(File.class, URI.class);}
+
+        @Override public ObjectConverter<URI,File> inverse() {return URIFile.INSTANCE;}
+        @Override public URI doConvert(final File source) {
+            return source.toURI();
         }
+    }
 
-        /** Returns the singleton instance on deserialization. */
-        Object readResolve() throws ObjectStreamException {
-            return INSTANCE;
+    /**
+     * Converter from {@link File} to {@link URL}.
+     */
+    private static final class FileURL extends PathConverter<File,URL> {
+        private static final long serialVersionUID = 621496099287330756L;
+        static final FileURL INSTANCE = new FileURL();
+        private FileURL() {super(File.class, URL.class);}
+
+        @Override public ObjectConverter<URL,File> inverse() {return URLFile.INSTANCE;}
+        @Override public URL doConvert(final File source) throws MalformedURLException {
+            return source.toURI().toURL();
         }
     }
 
     /**
-     * Converter from {@link File} to {@link java.net.URL}.
+     * Converter from {@link URL} to {@link File}.
      */
-    @Immutable
-    static final class URL extends FileConverter<java.net.URL> {
-        /** Cross-version compatibility. */ static final long serialVersionUID = 621496099287330756L;
-        /** The unique, shared instance. */ static final URL INSTANCE = new URL();
-        /** For {@link #INSTANCE} only.  */ private URL() {}
-
-        @Override public Class<java.net.URL> getTargetClass() {
-            return java.net.URL.class;
+    private static final class URLFile extends PathConverter<URL,File> {
+        private static final long serialVersionUID = 1228852836485762335L;
+        static final URLFile INSTANCE = new URLFile();
+        private URLFile() {super(URL.class, File.class);}
+
+        @Override public ObjectConverter<File,URL> inverse() {return FileURL.INSTANCE;}
+        @Override public File doConvert(final URL source) throws URISyntaxException {
+            return new File(source.toURI());
         }
+    }
 
-        @Override public java.net.URL convert(final File source) throws UnconvertibleObjectException
{
-            if (source == null) {
-                return null;
-            }
-            try {
-                return source.toURI().toURL();
-            } catch (MalformedURLException e) {
-                throw new UnconvertibleObjectException(formatErrorMessage(source), e);
-            }
+    /**
+     * Converter from {@link URL} to {@link File}.
+     */
+    private static final class URIFile extends PathConverter<URI,File> {
+        private static final long serialVersionUID = 5289256237146366469L;
+        static final URIFile INSTANCE = new URIFile();
+        private URIFile() {super(URI.class, File.class);}
+
+        @Override public ObjectConverter<File,URI> inverse() {return FileURI.INSTANCE;}
+        @Override public File doConvert(final URI source) throws IllegalArgumentException
{
+            return new File(source);
         }
+    }
 
-        @Override public ObjectConverter<java.net.URL, File> inverse() {
-            return URLConverter.File.INSTANCE;
+    /**
+     * Converter from {@link URL} to {@link URI}.
+     */
+    @Immutable
+    static final class URL_URI extends PathConverter<URL,URI> {
+        private static final long serialVersionUID = -1653233667050600894L;
+        static final URL_URI INSTANCE = new URL_URI();
+        private URL_URI() {super(URL.class, URI.class);}
+
+        @Override public ObjectConverter<URI, URL> inverse() {return URI_URL.INSTANCE;}
+        @Override public URI doConvert(final URL source) throws URISyntaxException {
+            return source.toURI();
         }
+    }
 
-        /** Returns the singleton instance on deserialization. */
-        Object readResolve() throws ObjectStreamException {
-            return INSTANCE;
+    /**
+     * Converter from {@link URI} to {@link URL}.
+     */
+    static final class URI_URL extends PathConverter<URI,URL> {
+        private static final long serialVersionUID = -7866572007304228474L;
+        static final URI_URL INSTANCE = new URI_URL();
+        private URI_URL() {super(URI.class, URL.class);}
+
+        @Override public ObjectConverter<URL, URI> inverse() {return URL_URI.INSTANCE;}
+        @Override public URL doConvert(final URI source) throws MalformedURLException {
+            return source.toURL();
         }
     }
 }

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/SurjectiveConverter.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/SurjectiveConverter.java?rev=1457162&r1=1457161&r2=1457162&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/SurjectiveConverter.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/SurjectiveConverter.java
[UTF-8] Fri Mar 15 23:38:58 2013
@@ -71,16 +71,6 @@ public abstract class SurjectiveConverte
     }
 
     /**
-     * Formats an error message for a value that can not be converted.
-     *
-     * @param  value The value that can not be converted.
-     * @return The error message.
-     */
-    final String formatErrorMessage(final S value) {
-        return Errors.format(Errors.Keys.CanNotConvertValue_2, value, getTargetClass());
-    }
-
-    /**
      * Returns a string representation of this converter for debugging purpose.
      */
     @Override

Copied: sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/internal/converter/PathConverterTest.java
(from r1457152, sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/internal/converter/FileConverterTest.java)
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/internal/converter/PathConverterTest.java?p2=sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/internal/converter/PathConverterTest.java&p1=sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/internal/converter/FileConverterTest.java&r1=1457152&r2=1457162&rev=1457162&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/internal/converter/FileConverterTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/internal/converter/PathConverterTest.java
[UTF-8] Fri Mar 15 23:38:58 2013
@@ -34,14 +34,14 @@ import static org.apache.sis.test.Assert
 
 
 /**
- * Tests the various {@link FileConverter} implementations.
+ * Tests the various {@link PathConverter} implementations.
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.3 (derived from geotk-3.01)
  * @version 0.3
  * @module
  */
-public final strictfp class FileConverterTest extends TestCase {
+public final strictfp class PathConverterTest extends TestCase {
     /**
      * Assumes that the platform file system has a Unix-style root.
      * Windows platform has driver letters instead, like "C:\\",
@@ -55,8 +55,8 @@ public final strictfp class FileConverte
      * Asserts that conversion of the given {@code source} value produces
      * the given {@code target} value, and tests the inverse conversion.
      */
-    private static <T> void runInvertibleConversion(final ObjectConverter<File,T>
c,
-            final File source, final T target) throws UnconvertibleObjectException
+    private static <S,T> void runInvertibleConversion(final ObjectConverter<S,T>
c,
+            final S source, final T target) throws UnconvertibleObjectException
     {
         assertEquals("Forward conversion.", target, c.convert(source));
         assertEquals("Inverse conversion.", source, c.inverse().convert(target));
@@ -66,40 +66,118 @@ public final strictfp class FileConverte
     }
 
     /**
-     * Tests conversions to string values.
+     * Tests conversions from File to string values.
      */
     @Test
-    public void testString() {
+    public void testFile_String() {
         final ObjectConverter<File,String> c = StringConverter.getInstance(File.class).inverse();
         runInvertibleConversion(c, new File("home/user/index.txt"), "home/user/index.txt".replace("/",
File.separator));
         assertSame("Deserialization shall resolves to the singleton instance.", c, assertSerializedEquals(c));
     }
 
     /**
-     * Tests conversions to URI values.
+     * Tests conversions from File to URI values.
      *
      * @throws URISyntaxException Should never happen.
      */
     @Test
     @PlatformDependentTest
-    public void testURI() throws URISyntaxException {
+    public void testFile_URI() throws URISyntaxException {
         assumeUnixRoot();
-        final ObjectConverter<File,URI> c = FileConverter.URI.INSTANCE;
+        final ObjectConverter<File,URI> c = PathConverter.getInstance(File.class, URI.class);
         runInvertibleConversion(c, new File("/home/user/index.txt"), new URI("file:/home/user/index.txt"));
         assertSame("Deserialization shall resolves to the singleton instance.", c, assertSerializedEquals(c));
     }
 
     /**
-     * Tests conversions to URL values.
+     * Tests conversions from File to URL values.
      *
      * @throws MalformedURLException Should never happen.
      */
     @Test
     @PlatformDependentTest
-    public void testURL() throws MalformedURLException {
+    public void testFile_URL() throws MalformedURLException {
         assumeUnixRoot();
-        final ObjectConverter<File,URL> c = FileConverter.URL.INSTANCE;
+        final ObjectConverter<File,URL> c = PathConverter.getInstance(File.class, URL.class);
         runInvertibleConversion(c, new File("/home/user/index.txt"), new URL("file:/home/user/index.txt"));
         assertSame("Deserialization shall resolves to the singleton instance.", c, assertSerializedEquals(c));
     }
+
+    /**
+     * Tests conversions from URI to string values.
+     *
+     * @throws URISyntaxException Should never happen.
+     */
+    @Test
+    public void testURI_String() throws URISyntaxException {
+        final ObjectConverter<URI,String> c = StringConverter.getInstance(URI.class).inverse();
+        runInvertibleConversion(c, new URI("file:/home/user/index.txt"), "file:/home/user/index.txt");
+        assertSame("Deserialization shall resolves to the singleton instance.", c, assertSerializedEquals(c));
+    }
+
+    /**
+     * Tests conversions from URI to URL values.
+     *
+     * @throws MalformedURLException Should never happen.
+     * @throws URISyntaxException Should never happen.
+     */
+    @Test
+    public void testURI_URL() throws MalformedURLException, URISyntaxException {
+        final ObjectConverter<URI,URL> c = PathConverter.getInstance(URI.class, URL.class);
+        runInvertibleConversion(c, new URI("file:/home/user/index.txt"), new URL("file:/home/user/index.txt"));
+        assertSame("Deserialization shall resolves to the singleton instance.", c, assertSerializedEquals(c));
+    }
+
+    /**
+     * Tests conversions from URI to File values.
+     *
+     * @throws URISyntaxException Should never happen.
+     */
+    @Test
+    @PlatformDependentTest
+    public void testURI_File() throws URISyntaxException {
+        PathConverterTest.assumeUnixRoot();
+        final ObjectConverter<URI,File> c = PathConverter.getInstance(URI.class, File.class);
+        runInvertibleConversion(c, new URI("file:/home/user/index.txt"), new File("/home/user/index.txt"));
+        assertSame("Deserialization shall resolves to the singleton instance.", c, assertSerializedEquals(c));
+    }
+
+    /**
+     * Tests conversions from URL to string values.
+     *
+     * @throws MalformedURLException Should never happen.
+     */
+    @Test
+    public void testURL_String() throws MalformedURLException {
+        final ObjectConverter<URL,String> c = StringConverter.getInstance(URL.class).inverse();
+        runInvertibleConversion(c, new URL("file:/home/user/index.txt"), "file:/home/user/index.txt");
+        assertSame("Deserialization shall resolves to the singleton instance.", c, assertSerializedEquals(c));
+    }
+
+    /**
+     * Tests conversions from URL to URI values.
+     *
+     * @throws MalformedURLException Should never happen.
+     * @throws URISyntaxException Should never happen.
+     */
+    @Test
+    public void testURL_URI() throws MalformedURLException, URISyntaxException {
+        final ObjectConverter<URL,URI> c = PathConverter.getInstance(URL.class, URI.class);
+        runInvertibleConversion(c, new URL("file:/home/user/index.txt"), new URI("file:/home/user/index.txt"));
+        assertSame("Deserialization shall resolves to the singleton instance.", c, assertSerializedEquals(c));
+    }
+
+    /**
+     * Tests conversions from URL to File values.
+     *
+     * @throws MalformedURLException Should never happen.
+     */
+    @Test
+    @PlatformDependentTest
+    public void testURL_File() throws MalformedURLException {
+        PathConverterTest.assumeUnixRoot();
+        final ObjectConverter<URL,File> c = PathConverter.getInstance(URL.class, File.class);
+        runInvertibleConversion(c, new URL("file:/home/user/index.txt"), new File("/home/user/index.txt"));
+        assertSame("Deserialization shall resolves to the singleton instance.", c, assertSerializedEquals(c));
+    }
 }

Modified: sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java?rev=1457162&r1=1457161&r2=1457162&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
[UTF-8] Fri Mar 15 23:38:58 2013
@@ -91,9 +91,7 @@ import org.junit.runners.Suite;
     org.apache.sis.util.collection.RangeSetTest.class,
 
     // Converters
-    org.apache.sis.internal.converter.URIConverterTest.class,
-    org.apache.sis.internal.converter.URLConverterTest.class,
-    org.apache.sis.internal.converter.FileConverterTest.class,
+    org.apache.sis.internal.converter.PathConverterTest.class,
     org.apache.sis.internal.converter.StringConverterTest.class,
     org.apache.sis.internal.converter.FallbackConverterTest.class,
     org.apache.sis.internal.converter.ConverterRegistryTest.class,



Mime
View raw message