Author: joehni
Date: Thu Aug 13 14:30:18 2009
New Revision: 803902

URL: http://svn.apache.org/viewvc?rev=803902&view=rev
Log:
Implement reasonable equals and hashCode methods for StaticUserAuthenticator 
(VFS-274).
Implement Comparable for StaticUserAuthenticator.
Improve javadoc for UserAuthenticator.

Modified:
    
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/UserAuthenticator.java
    
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/auth/StaticUserAuthenticator.java

Modified: 
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/UserAuthenticator.java
URL: 
http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/UserAuthenticator.java?rev=803902&r1=803901&r2=803902&view=diff
==============================================================================
--- 
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/UserAuthenticator.java
 (original)
+++ 
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/UserAuthenticator.java
 Thu Aug 13 14:30:18 2009
@@ -17,13 +17,15 @@
 package org.apache.commons.vfs;
 
 /**
- * The user authenticator is used to query credentials from the user
+ * The user authenticator is used to query credentials from the user. Since a 
UserAuthenticator 
+ * is provided with the {...@link FileSystemOptions} to a {...@link 
FileSystem} it should also implement
+ * reasonable equals and hashCode functions if the FileSystem should be shared.
  * @author <a href="http://commons.apache.org/vfs/team-list.html";>Commons VFS 
team</a>
  */
 public interface UserAuthenticator
 {
     /**
-     * queries the given type from the user
+     * Queries the given type from the user
      * @param types An array containing the user's credentials
      * @return The UserAuthenticationData.
      */

Modified: 
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/auth/StaticUserAuthenticator.java
URL: 
http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/auth/StaticUserAuthenticator.java?rev=803902&r1=803901&r2=803902&view=diff
==============================================================================
--- 
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/auth/StaticUserAuthenticator.java
 (original)
+++ 
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/auth/StaticUserAuthenticator.java
 Thu Aug 13 14:30:18 2009
@@ -21,10 +21,10 @@
 import org.apache.commons.vfs.util.UserAuthenticatorUtils;
 
 /**
- * provides always the same credential data passed in with the constructor.
+ * Provides always the same credentials data passed in with the constructor.
  * @author <a href="http://commons.apache.org/vfs/team-list.html";>Commons VFS 
team</a>
  */
-public class StaticUserAuthenticator implements UserAuthenticator
+public class StaticUserAuthenticator implements UserAuthenticator, Comparable
 {
     /** The user name */
     private final String username;
@@ -50,4 +50,100 @@
         data.setData(UserAuthenticationData.PASSWORD, 
UserAuthenticatorUtils.toChar(password));
         return data;
     }
+
+    /**
+     * {...@inheritdoc}
+     */
+    public int hashCode() {
+        final int prime = 37;
+        int result = 1;
+        result = prime * result + ((domain == null) ? 0 : domain.hashCode());
+        result = prime * result + ((password == null) ? 0 : 
password.hashCode());
+        result = prime * result + ((username == null) ? 0 : 
username.hashCode());
+
+        return result;
+    }
+
+    /**
+     * {...@inheritdoc}
+     */
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+
+        if (obj == null) {
+            return false;
+        }
+
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+
+        StaticUserAuthenticator other = (StaticUserAuthenticator)obj;
+        return equalsNullsafe(domain, other.domain)
+                && equalsNullsafe(username, other.username)
+                && equalsNullsafe(password, other.password);
+    }
+
+    private boolean equalsNullsafe(final String thisString, final String 
otherString) {
+        if (thisString == null) {
+            if (otherString != null) {
+                return false;
+            }
+        } else if (!thisString.equals(otherString)) {
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * {...@inheritdoc}
+     */
+    public int compareTo(final Object o) {
+        StaticUserAuthenticator other = (StaticUserAuthenticator)o;
+        int result = compareStringOrNull(domain, other.domain);
+        result = result == 0 ? compareStringOrNull(username, other.username) : 
result;
+        result = result == 0 ? compareStringOrNull(password, other.password) : 
result;
+
+        return result;
+    }
+
+    private int compareStringOrNull(final String thisString, final String 
otherString) {
+        if (thisString == null) {
+            if (otherString != null) {
+                return -1;
+            }
+        } else {
+            if (otherString == null) {
+                return 1;
+            }
+
+            final int result = thisString.compareTo(otherString);
+            if (result != 0) {
+                return result;
+            }
+        }
+
+        return 0;
+    }
+    
+    /**
+     * {...@inheritdoc}
+     */
+    public String toString() {
+        StringBuffer buffer = new StringBuffer();
+        if (domain != null) {
+            buffer.append(domain).append('\\');
+        }
+        if (username != null) {
+            buffer.append(username);
+        } else {
+            buffer.append("(null)");
+        }
+        if (password != null) {
+            buffer.append(":***");
+        }
+        return buffer.toString();
+    }
 }


Reply via email to