Author: rgoers
Date: Thu Nov 25 21:31:30 2010
New Revision: 1039191

URL: http://svn.apache.org/viewvc?rev=1039191&view=rev
Log:
Workaround for VFS-245 - equals(), hashcode() and compareTo() will return the 
same results regardless of the FileType changing

Modified:
    
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/AbstractFileName.java
    commons/proper/vfs/trunk/src/changes/changes.xml

Modified: 
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/AbstractFileName.java
URL: 
http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/AbstractFileName.java?rev=1039191&r1=1039190&r2=1039191&view=diff
==============================================================================
--- 
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/AbstractFileName.java
 (original)
+++ 
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/AbstractFileName.java
 Thu Nov 25 21:31:30 2010
@@ -42,8 +42,7 @@ public abstract class AbstractFileName i
     private String extension;
     private String decodedAbsPath;
 
-    private boolean calculateHashCode = true;
-    private int calculatedHashCode;
+    private String key = null;
 
     public AbstractFileName(final String scheme, final String absPath, 
FileType type)
     {
@@ -67,35 +66,27 @@ public abstract class AbstractFileName i
         }
     }
 
-    /**
-     * Returns the hashcode for this name.
-     * @return The hashCode.
-     */
     @Override
-    public int hashCode()
+    public boolean equals(Object o)
     {
-        if (calculateHashCode)
+        if (this == o)
         {
-            calculatedHashCode = getRootURI().hashCode() ^ 
getPath().hashCode();
-            calculateHashCode = false;
+            return true;
+        }
+        if (o == null || getClass() != o.getClass())
+        {
+            return false;
         }
-        return calculatedHashCode;
+
+        AbstractFileName that = (AbstractFileName) o;
+
+        return (getKey().equals(that.getKey()));
     }
 
-    /**
-     * Determines if this object is equal to another.
-     * @param obj The object to compare.
-     * @return true if equal, false if not.
-     */
     @Override
-    public boolean equals(final Object obj)
+    public int hashCode()
     {
-        if (!(obj instanceof AbstractFileName))
-        {
-            return false;
-        }
-        final AbstractFileName name = (AbstractFileName) obj;
-        return getRootURI().equals(name.getRootURI()) && 
getPath().equals(name.getPath());
+        return getKey().hashCode();
     }
 
     /**
@@ -107,21 +98,7 @@ public abstract class AbstractFileName i
     public int compareTo(FileName obj)
     {
         final AbstractFileName name = (AbstractFileName) obj;
-        int ret = getRootURI().compareTo(name.getRootURI());
-        if (ret != 0)
-        {
-            return ret;
-        }
-
-        // return absPath.compareTo(name.absPath);
-        try
-        {
-            return getPathDecoded().compareTo(name.getPathDecoded());
-        }
-        catch (FileSystemException e)
-        {
-            throw new RuntimeException(e.getMessage());
-        }
+        return getKey().compareTo(name.getKey());
     }
 
     /**
@@ -270,12 +247,40 @@ public abstract class AbstractFileName i
 
     protected String createURI()
     {
+        return createURI(false, true);
+    }
+
+    /**
+     * Create a path that does not use the FileType since that field is not 
immutable.
+     * @return The key.
+     */
+    private String getKey()
+    {
+        if (key == null)
+        {
+            key = createURI(true, true);
+        }
+        return key;
+    }
+
+    /**
+     * returns a "friendly path", this is a path without a password.
+     * @return The "friendly" URI.
+     */
+    public String getFriendlyURI()
+    {
+        return createURI(false, false);
+    }
+
+    private String createURI(boolean useAbsolutePath, boolean usePassword)
+    {
         final StringBuilder buffer = new StringBuilder();
-        appendRootUri(buffer, true);
-        buffer.append(getPath());
+        appendRootUri(buffer, usePassword);
+        buffer.append(useAbsolutePath ? absPath : getPath());
         return buffer.toString();
     }
 
+
     /**
      * Converts a file name to a relative name, relative to this file name.
      * @param name The FileName.
@@ -539,16 +544,4 @@ public abstract class AbstractFileName i
 
         return true;
     }
-
-    /**
-     * returns a "friendly path", this is a path without a password.
-     * @return The "friendly" URI.
-     */
-    public String getFriendlyURI()
-    {
-        final StringBuilder buffer = new StringBuilder();
-        appendRootUri(buffer, false);
-        buffer.append(getPath());
-        return buffer.toString();
-    }
 }

Modified: commons/proper/vfs/trunk/src/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/commons/proper/vfs/trunk/src/changes/changes.xml?rev=1039191&r1=1039190&r2=1039191&view=diff
==============================================================================
--- commons/proper/vfs/trunk/src/changes/changes.xml (original)
+++ commons/proper/vfs/trunk/src/changes/changes.xml Thu Nov 25 21:31:30 2010
@@ -23,6 +23,10 @@
 
   <body>
     <release version="2.0" date="in SVN" description="">
+       <action issue="VFS-245" dev="rgoers" type="fix">
+        AbstractFileName is not immutable as it should be. equals(), 
hashcode() and compareTo() have been modified
+        to return the same results regardless of whether the FileType is 
changed. 
+      </action>
       <action issue="VFS-334" dev="sebb" type="fix" due-to="sebb">
         DefaultFileSystemConfigBuilder.getConfigClass() returns 
DefaultFileSystemConfigBuilder.class which is not a FileSystem
       </action>


Reply via email to