jericho     2003/01/29 08:03:33

  Modified:    httpclient/src/java/org/apache/commons/httpclient URI.java
  Log:
  - Add the hash code functionality.
  
  Suggested by Joseph Artsimovich <[EMAIL PROTECTED]>
  Thanks to you!
  
  Revision  Changes    Path
  1.27      +41 -6     
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/URI.java
  
  Index: URI.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/URI.java,v
  retrieving revision 1.26
  retrieving revision 1.27
  diff -u -r1.26 -r1.27
  --- URI.java  28 Jan 2003 22:25:22 -0000      1.26
  +++ URI.java  29 Jan 2003 16:03:33 -0000      1.27
  @@ -526,6 +526,12 @@
   
   
       /**
  +     * Cache the hash code for this URI.
  +     */
  +    protected int hash = 0;
  +
  +
  +    /**
        * This Uniform Resource Identifier (URI).
        * The URI is always in an "escaped" form, since escaping or unescaping
        * a completed URI might change its semantics.  
  @@ -2216,6 +2222,7 @@
           }
           // ignore the fragment identifier
           _uri = buf.toString().toCharArray();
  +        hash = 0;
       }
   
       // ----------------------------------------------------------- Test methods
  @@ -3165,13 +3172,14 @@
       public void setRawFragment(char[] escapedFragment) throws URIException {
           if (escapedFragment == null || escapedFragment.length == 0) {
               _fragment = escapedFragment;
  +            hash = 0;
               return;
           }
           if (!validate(escapedFragment, fragment))
               throw new URIException(URIException.ESCAPING,
                       "escaped fragment not valid");
           _fragment = escapedFragment;
  -        setURI();
  +        hash = 0;
       }
   
   
  @@ -3184,6 +3192,7 @@
       public void setEscapedFragment(String escapedFragment) throws URIException {
           if (escapedFragment == null) {
               _fragment = null;
  +            hash = 0;
               return;
           }
           setRawFragment(escapedFragment.toCharArray());
  @@ -3199,10 +3208,11 @@
       public void setFragment(String fragment) throws URIException {
           if (fragment == null || fragment.length() == 0) {
               _fragment = (fragment == null) ? null : fragment.toCharArray();
  +            hash = 0;
               return;
           }
           _fragment = encode(fragment, allowed_fragment);
  -        setURI();
  +        hash = 0;
       }
   
   
  @@ -3436,6 +3446,31 @@
           throws ClassNotFoundException, IOException {
   
           ois.defaultReadObject();
  +    }
  +
  +    // -------------------------------------------------------------- Hash code
  +
  +    /**
  +     * Return a hash code for this URI.
  +     *
  +     * @return a has code value for this URI
  +     */
  +    public int hashCode() {
  +        if (hash == 0) {
  +            char[] c = _uri;
  +            if (c != null) {
  +                for (int i = 0, len = c.length; i < len; i++) {
  +                    hash = 31 * hash + c[i];
  +                }
  +            }
  +            c = _fragment;
  +            if (c != null) {
  +                for (int i = 0, len = c.length; i < len; i++) {
  +                    hash = 31 * hash + c[i];
  +                }
  +            }
  +        }
  +        return hash;
       }
   
       // ------------------------------------------------------------- Comparison 
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to