You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2013/09/19 22:18:36 UTC

svn commit: r1524822 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/io/ test/java/org/apache/commons/configuration/io/

Author: oheger
Date: Thu Sep 19 20:18:35 2013
New Revision: 1524822

URL: http://svn.apache.org/r1524822
Log:
Deleted FileLocator interface and renamed FileLocatorImpl to FileLocator.

There is no need for an interface. An immutable FileLocator class is
sufficient.

Removed:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocatorImpl.java
Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileHandler.java
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocator.java
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocatorUtils.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileLocator.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileHandler.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileHandler.java?rev=1524822&r1=1524821&r2=1524822&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileHandler.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileHandler.java Thu Sep 19 20:18:35 2013
@@ -33,7 +33,7 @@ import java.util.concurrent.CopyOnWriteA
 import java.util.concurrent.atomic.AtomicReference;
 
 import org.apache.commons.configuration.ConfigurationException;
-import org.apache.commons.configuration.io.FileLocatorImpl.FileLocatorBuilder;
+import org.apache.commons.configuration.io.FileLocator.FileLocatorBuilder;
 import org.apache.commons.configuration.sync.LockMode;
 import org.apache.commons.configuration.sync.NoOpSynchronizer;
 import org.apache.commons.configuration.sync.Synchronizer;

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocator.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocator.java?rev=1524822&r1=1524821&r2=1524822&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocator.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocator.java Thu Sep 19 20:18:35 2013
@@ -18,55 +18,319 @@ package org.apache.commons.configuration
 
 import java.net.URL;
 
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.builder.EqualsBuilder;
+import org.apache.commons.lang3.builder.HashCodeBuilder;
+import org.apache.commons.lang3.builder.ToStringBuilder;
+
 /**
  * <p>
- * An interface describing the location of a file.
+ * A class describing the location of a file.
+ * </p>
+ * <p>
+ * An instance of this class provides information for locating and accessing a
+ * file. The file location can be defined
+ * <ul>
+ * <li>as a URL; this identifies a file in a unique way</li>
+ * <li>as a combination of base path and file name; if this variant is used,
+ * there may be an additional location step required in order to identify the
+ * referenced file (for instance, the file name may be interpreted as the name
+ * of a resource to be loaded from class path).</li>
+ * </ul>
+ * In addition, other properties are available which are also needed for loading
+ * or saving a file, like the encoding or the underlying {@link FileSystem}.
  * </p>
  * <p>
- * An object implementing this interface can be used to obtain information about
- * the storage location of a file. It allows querying the typical components
- * used by {@link FileHandler} to locate a file.
+ * Instances of this class are immutable and thus can be safely shared between
+ * arbitrary components. {@link FileHandler} also uses an instance to reference
+ * the associated file. Instances are created using a <em>builder</em>.
+ * {@link FileLocatorUtils} offers convenience methods for obtaining such a
+ * builder.
  * </p>
  *
  * @version $Id$
  * @since 2.0
  */
-public interface FileLocator
+public final class FileLocator
 {
+    /** The file name. */
+    private final String fileName;
+
+    /** The base path. */
+    private final String basePath;
+
+    /** The source URL. */
+    private final URL sourceURL;
+
+    /** The encoding. */
+    private final String encoding;
+
+    /** The file system. */
+    private final FileSystem fileSystem;
+
+    /**
+     * Creates a new instance of {@code FileLocatorImpl} and initializes it from
+     * the given builder instance
+     *
+     * @param builder the builder
+     */
+    public FileLocator(FileLocatorBuilder builder)
+    {
+        fileName = builder.fileName;
+        basePath = builder.basePath;
+        sourceURL = builder.sourceURL;
+        encoding = builder.encoding;
+        fileSystem = builder.fileSystem;
+    }
+
     /**
-     * Returns the name of the represented file.
+     * Returns the file name stored in this locator or <b>null</b> if it is
+     * undefined.
      *
-     * @return the file name only
+     * @return the file name
      */
-    String getFileName();
+    public String getFileName()
+    {
+        return fileName;
+    }
 
     /**
-     * Returns the base path of the represented file. This is typically the
-     * directory in which the file is stored.
+     * Returns the base path stored in this locator or <b>null</b> if it is
+     * undefined.
      *
      * @return the base path
      */
-    String getBasePath();
+    public String getBasePath()
+    {
+        return basePath;
+    }
 
     /**
-     * Returns a full URL to the represented file.
+     * Returns the URL pointing to the referenced source file or <b>null</b> if
+     * it is undefined.
      *
-     * @return the URL pointing to the file
+     * @return the source URL
      */
-    URL getSourceURL();
+    public URL getSourceURL()
+    {
+        return sourceURL;
+    }
 
     /**
-     * Returns the file system which is used for resolving files to be loaded.
+     * Returns the encoding stored in this locator or <b>null</b> if it is
+     * undefined.
+     *
+     * @return the encoding
+     */
+    public String getEncoding()
+    {
+        return encoding;
+    }
+
+    /**
+     * Returns the {@code FileSystem} to be used for accessing the file
+     * referenced by this locator or <b>null</b> if it is undefined.
      *
      * @return the {@code FileSystem}
      */
-    FileSystem getFileSystem();
+    public FileSystem getFileSystem()
+    {
+        return fileSystem;
+    }
 
     /**
-     * Returns the encoding of the represented file if known. Result can be
-     * <b>null</b>, then default encoding should be assumed.
+     * Returns a hash code for this object.
      *
-     * @return the encoding
+     * @return a hash code for this object
+     */
+    @Override
+    public int hashCode()
+    {
+        return new HashCodeBuilder().append(getFileName())
+                .append(getBasePath()).append(sourceURLAsString())
+                .append(getEncoding()).append(getFileSystem()).toHashCode();
+    }
+
+    /**
+     * Compares this object with another one. Two instances of
+     * {@code FileLocatorImpl} are considered equal if all of their properties
+     * are equal.
+     *
+     * @param obj the object to compare to
+     * @return a flag whether these objects are equal
+     */
+    @Override
+    public boolean equals(Object obj)
+    {
+        if (this == obj)
+        {
+            return true;
+        }
+        if (!(obj instanceof FileLocator))
+        {
+            return false;
+        }
+
+        FileLocator c = (FileLocator) obj;
+        return new EqualsBuilder().append(getFileName(), c.getFileName())
+                .append(getBasePath(), c.getBasePath())
+                .append(sourceURLAsString(), c.sourceURLAsString())
+                .append(getEncoding(), c.getEncoding())
+                .append(getFileSystem(), c.getFileSystem()).isEquals();
+    }
+
+    /**
+     * Returns a string representation of this object. This string contains the
+     * values of all properties.
+     *
+     * @return a string for this object
+     */
+    @Override
+    public String toString()
+    {
+        return new ToStringBuilder(this).append("fileName", getFileName())
+                .append("basePath", getBasePath())
+                .append("sourceURL", sourceURLAsString())
+                .append("encoding", getEncoding())
+                .append("fileSystem", getFileSystem()).toString();
+    }
+
+    /**
+     * Returns the source URL as a string. Result is never null. Comparisons are
+     * done on this string to avoid blocking network calls.
+     *
+     * @return the source URL as a string (not null)
+     */
+    private String sourceURLAsString()
+    {
+        return (sourceURL != null) ? sourceURL.toExternalForm()
+                : StringUtils.EMPTY;
+    }
+
+    /**
+     * A typical <em>builder</em> implementation for creating
+     * {@code FileLocator} objects. An instance of this class is returned by the
+     * {@code fileLocator()} method of {link FileLocatorUtils}. It can be used
+     * to define the various components of the {@code FileLocator} object. By
+     * calling {@code create()} the new immutable {@code FileLocator} instance
+     * is created.
      */
-    String getEncoding();
+    public static final class FileLocatorBuilder
+    {
+        /** The file name. */
+        private String fileName;
+
+        /** The base path. */
+        private String basePath;
+
+        /** The source URL. */
+        private URL sourceURL;
+
+        /** The encoding. */
+        private String encoding;
+
+        /** The file system. */
+        private FileSystem fileSystem;
+
+        /**
+         * Creates a new instance of {@code FileLocatorBuilder} and initializes
+         * the builder's properties from the passed in {@code FileLocator}
+         * object.
+         *
+         * @param src the source {@code FileLocator} (may be <b>null</b>)
+         */
+        FileLocatorBuilder(FileLocator src)
+        {
+            if (src != null)
+            {
+                initBuilder(src);
+            }
+        }
+
+        /**
+         * Specifies the encoding of the new {@code FileLocator}.
+         *
+         * @param enc the encoding
+         * @return a reference to this builder for method chaining
+         */
+        public FileLocatorBuilder encoding(String enc)
+        {
+            encoding = enc;
+            return this;
+        }
+
+        /**
+         * Specifies the {@code FileSystem} of the new {@code FileLocator}.
+         *
+         * @param fs the {@code FileSystem}
+         * @return a reference to this builder for method chaining
+         */
+        public FileLocatorBuilder fileSystem(FileSystem fs)
+        {
+            fileSystem = fs;
+            return this;
+        }
+
+        /**
+         * Specifies the base path of the new {@code FileLocator}.
+         *
+         * @param path the base path
+         * @return a reference to this builder for method chaining
+         */
+        public FileLocatorBuilder basePath(String path)
+        {
+            basePath = path;
+            return this;
+        }
+
+        /**
+         * Specifies the file name of the new {@code FileLocator}.
+         *
+         * @param name the file name
+         * @return a reference to this builder for method chaining
+         */
+        public FileLocatorBuilder fileName(String name)
+        {
+            fileName = name;
+            return this;
+        }
+
+        /**
+         * Specifies the source URL of the new {@code FileLocator}.
+         *
+         * @param url the source URL
+         * @return a reference to this builder for method chaining
+         */
+        public FileLocatorBuilder sourceURL(URL url)
+        {
+            sourceURL = url;
+            return this;
+        }
+
+        /**
+         * Creates a new immutable {@code FileLocatorImpl} object based on the
+         * properties set so far for this builder.
+         *
+         * @return the newly created {@code FileLocator} object
+         */
+        public FileLocator create()
+        {
+            return new FileLocator(this);
+        }
+
+        /**
+         * Initializes the properties of this builder from the passed in locator
+         * object.
+         *
+         * @param src the source {@code FileLocator}
+         */
+        private void initBuilder(FileLocator src)
+        {
+            basePath = src.getBasePath();
+            fileName = src.getFileName();
+            sourceURL = src.getSourceURL();
+            encoding = src.getEncoding();
+            fileSystem = src.getFileSystem();
+        }
+    }
 }

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocatorUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocatorUtils.java?rev=1524822&r1=1524821&r2=1524822&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocatorUtils.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocatorUtils.java Thu Sep 19 20:18:35 2013
@@ -89,7 +89,7 @@ public final class FileLocatorUtils
      * </pre>
      * @return a builder object for defining a {@code FileLocator}
      */
-    public static FileLocatorImpl.FileLocatorBuilder fileLocator()
+    public static FileLocator.FileLocatorBuilder fileLocator()
     {
         return fileLocator(null);
     }
@@ -109,9 +109,9 @@ public final class FileLocatorUtils
      * @param src the source {@code FileLocator} (may be <b>null</b>)
      * @return an initialized builder object for defining a {@code FileLocator}
      */
-    public static FileLocatorImpl.FileLocatorBuilder fileLocator(FileLocator src)
+    public static FileLocator.FileLocatorBuilder fileLocator(FileLocator src)
     {
-        return new FileLocatorImpl.FileLocatorBuilder(src);
+        return new FileLocator.FileLocatorBuilder(src);
     }
 
     /**

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileLocator.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileLocator.java?rev=1524822&r1=1524821&r2=1524822&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileLocator.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileLocator.java Thu Sep 19 20:18:35 2013
@@ -31,7 +31,7 @@ import org.junit.BeforeClass;
 import org.junit.Test;
 
 /**
- * Test class for {@code FileLocatorImpl}.
+ * Test class for {@code FileLocator}.
  *
  * @version $Id: $
  */
@@ -66,7 +66,7 @@ public class TestFileLocator
     @Test
     public void testCreateFileLocatorUndefined()
     {
-        FileLocatorImpl locator = FileLocatorUtils.fileLocator().create();
+        FileLocator locator = FileLocatorUtils.fileLocator().create();
         assertNull("Got a base path", locator.getBasePath());
         assertNull("Got a file name", locator.getFileName());
         assertNull("Got a URL", locator.getSourceURL());
@@ -79,7 +79,7 @@ public class TestFileLocator
      *
      * @param locator the locator to check
      */
-    private static void checkLocator(FileLocatorImpl locator)
+    private static void checkLocator(FileLocator locator)
     {
         assertEquals("Wrong base path", BASE_PATH, locator.getBasePath());
         assertEquals("Wrong file name", FILE_NAME, locator.getFileName());
@@ -95,7 +95,7 @@ public class TestFileLocator
     @Test
     public void testCreateFileLocator()
     {
-        FileLocatorImpl locator =
+        FileLocator locator =
                 FileLocatorUtils.fileLocator().basePath(BASE_PATH)
                         .fileName(FILE_NAME).encoding(ENCODING)
                         .fileSystem(fileSystem).sourceURL(sourceURL).create();
@@ -108,11 +108,11 @@ public class TestFileLocator
     @Test
     public void testCreateFileLocatorFromSource()
     {
-        FileLocatorImpl locatorSrc =
+        FileLocator locatorSrc =
                 FileLocatorUtils.fileLocator().basePath(BASE_PATH)
                         .fileName("someFile").encoding(ENCODING)
                         .fileSystem(fileSystem).sourceURL(sourceURL).create();
-        FileLocatorImpl locator =
+        FileLocator locator =
                 FileLocatorUtils.fileLocator(locatorSrc).fileName(FILE_NAME)
                         .create();
         checkLocator(locator);
@@ -125,9 +125,9 @@ public class TestFileLocator
     @Test
     public void testFileLocatorEqualsTrue()
     {
-        FileLocatorImpl loc1 = FileLocatorUtils.fileLocator().create();
+        FileLocator loc1 = FileLocatorUtils.fileLocator().create();
         ConfigurationAssert.checkEquals(loc1, loc1, true);
-        FileLocatorImpl loc2 = FileLocatorUtils.fileLocator().create();
+        FileLocator loc2 = FileLocatorUtils.fileLocator().create();
         ConfigurationAssert.checkEquals(loc1, loc2, true);
         loc1 =
                 FileLocatorUtils.fileLocator().basePath(BASE_PATH)
@@ -147,11 +147,11 @@ public class TestFileLocator
     @Test
     public void testFileLocatorEqualsFalse()
     {
-        FileLocatorImpl loc1 =
+        FileLocator loc1 =
                 FileLocatorUtils.fileLocator().basePath(BASE_PATH)
                         .fileName(FILE_NAME).encoding(ENCODING)
                         .fileSystem(fileSystem).sourceURL(sourceURL).create();
-        FileLocatorImpl loc2 =
+        FileLocator loc2 =
                 FileLocatorUtils.fileLocator(loc1)
                         .basePath(BASE_PATH + "_other").create();
         ConfigurationAssert.checkEquals(loc1, loc2, false);
@@ -184,7 +184,7 @@ public class TestFileLocator
     @Test
     public void testFileLocatorEqualsNull()
     {
-        FileLocatorImpl loc =
+        FileLocator loc =
                 FileLocatorUtils.fileLocator().fileName(FILE_NAME).create();
         assertFalse("Wrong result", loc.equals(null));
     }
@@ -195,7 +195,7 @@ public class TestFileLocator
     @Test
     public void testFileLocatorEqualsOtherClass()
     {
-        FileLocatorImpl loc =
+        FileLocator loc =
                 FileLocatorUtils.fileLocator().fileName(FILE_NAME).create();
         assertFalse("Wrong result", loc.equals(this));
     }
@@ -206,7 +206,7 @@ public class TestFileLocator
     @Test
     public void testFileLocatorToString()
     {
-        FileLocatorImpl loc =
+        FileLocator loc =
                 FileLocatorUtils.fileLocator().basePath(BASE_PATH)
                         .fileName(FILE_NAME).encoding(ENCODING)
                         .fileSystem(fileSystem).sourceURL(sourceURL).create();