xstringforfsb.java

来自「java jdk 1.4的源码」· Java 代码 · 共 1,081 行 · 第 1/3 页

JAVA
1,081
字号
   * Tell if two objects are functionally equal.   *   * @param obj2 Object to compare this to   *   * @return true if the two objects are equal   *   * @throws javax.xml.transform.TransformerException   */  public boolean equals(XObject obj2)  {    if (this == obj2)    {      return true;    }    if(obj2.getType() == XObject.CLASS_NUMBER)    	return obj2.equals(this);    String str = obj2.str();    int n = m_length;    if (n == str.length())    {      FastStringBuffer fsb = fsb();      int i = m_start;      int j = 0;      while (n-- != 0)      {        if (fsb.charAt(i) != str.charAt(j))        {          return false;        }        i++;        j++;      }      return true;    }    return false;  }  /**   * Tell if two objects are functionally equal.   *   * @param obj2 Object to compare this to   *   * NEEDSDOC @param anotherString   *   * @return true if the two objects are equal   *   * @throws javax.xml.transform.TransformerException   */  public boolean equals(String anotherString)  {    int n = m_length;    if (n == anotherString.length())    {      FastStringBuffer fsb = fsb();      int i = m_start;      int j = 0;      while (n-- != 0)      {        if (fsb.charAt(i) != anotherString.charAt(j))        {          return false;        }        i++;        j++;      }      return true;    }    return false;  }  /**   * Compares this string to the specified object.   * The result is <code>true</code> if and only if the argument is not   * <code>null</code> and is a <code>String</code> object that represents   * the same sequence of characters as this object.   *   * @param   anObject   the object to compare this <code>String</code>   *                     against.   *   * NEEDSDOC @param obj2   * @return  <code>true</code> if the <code>String </code>are equal;   *          <code>false</code> otherwise.   * @see     java.lang.String#compareTo(java.lang.String)   * @see     java.lang.String#equalsIgnoreCase(java.lang.String)   */  public boolean equals(Object obj2)  {    if (null == obj2)      return false;          if(obj2 instanceof XNumber)    	return obj2.equals(this);      // In order to handle the 'all' semantics of       // nodeset comparisons, we always call the       // nodeset function.    else if (obj2 instanceof XNodeSet)      return obj2.equals(this);    else if (obj2 instanceof XStringForFSB)      return equals((XMLString) this);    else      return equals(obj2.toString());  }  /**   * Compares this <code>String</code> to another <code>String</code>,   * ignoring case considerations.  Two strings are considered equal   * ignoring case if they are of the same length, and corresponding   * characters in the two strings are equal ignoring case.   *   * @param   anotherString   the <code>String</code> to compare this   *                          <code>String</code> against.   * @return  <code>true</code> if the argument is not <code>null</code>   *          and the <code>String</code>s are equal,   *          ignoring case; <code>false</code> otherwise.   * @see     #equals(Object)   * @see     java.lang.Character#toLowerCase(char)   * @see java.lang.Character#toUpperCase(char)   */  public boolean equalsIgnoreCase(String anotherString)  {    return (m_length == anotherString.length())           ? str().equalsIgnoreCase(anotherString) : false;  }  /**   * Compares two strings lexicographically.   *   * @param   anotherString   the <code>String</code> to be compared.   *   * NEEDSDOC @param xstr   * @return  the value <code>0</code> if the argument string is equal to   *          this string; a value less than <code>0</code> if this string   *          is lexicographically less than the string argument; and a   *          value greater than <code>0</code> if this string is   *          lexicographically greater than the string argument.   * @exception java.lang.NullPointerException if <code>anotherString</code>   *          is <code>null</code>.   */  public int compareTo(XMLString xstr)  {    int len1 = m_length;    int len2 = xstr.length();    int n = Math.min(len1, len2);    FastStringBuffer fsb = fsb();    int i = m_start;    int j = 0;    while (n-- != 0)    {      char c1 = fsb.charAt(i);      char c2 = xstr.charAt(j);      if (c1 != c2)      {        return c1 - c2;      }      i++;      j++;    }    return len1 - len2;  }  /**   * Compares two strings lexicographically, ignoring case considerations.   * This method returns an integer whose sign is that of   * <code>this.toUpperCase().toLowerCase().compareTo(   * str.toUpperCase().toLowerCase())</code>.   * <p>   * Note that this method does <em>not</em> take locale into account,   * and will result in an unsatisfactory ordering for certain locales.   * The java.text package provides <em>collators</em> to allow   * locale-sensitive ordering.   *   * @param   str   the <code>String</code> to be compared.   *   * NEEDSDOC @param xstr   * @return  a negative integer, zero, or a positive integer as the   *          the specified String is greater than, equal to, or less   *          than this String, ignoring case considerations.   * @see     java.text.Collator#compare(String, String)   * @since   1.2   */  public int compareToIgnoreCase(XMLString xstr)  {    int len1 = m_length;    int len2 = xstr.length();    int n = Math.min(len1, len2);    FastStringBuffer fsb = fsb();    int i = m_start;    int j = 0;    while (n-- != 0)    {      char c1 = Character.toLowerCase(fsb.charAt(i));      char c2 = Character.toLowerCase(xstr.charAt(j));      if (c1 != c2)      {        return c1 - c2;      }      i++;      j++;    }    return len1 - len2;  }  /**   * Returns a hashcode for this string. The hashcode for a   * <code>String</code> object is computed as   * <blockquote><pre>   * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]   * </pre></blockquote>   * using <code>int</code> arithmetic, where <code>s[i]</code> is the   * <i>i</i>th character of the string, <code>n</code> is the length of   * the string, and <code>^</code> indicates exponentiation.   * (The hash value of the empty string is zero.)   *   * @return  a hash code value for this object.   */  public int hashCode()  {    // Commenting this out because in JDK1.1.8 and VJ++    // we don't match XMLStrings. Defaulting to the super    // causes us to create a string, but at this point    // this only seems to get called in key processing.    // Maybe we can live with it?    /*    int h = m_hash;    if (h == 0)    {      int off = m_start;      int len = m_length;      FastStringBuffer fsb = fsb();      for (int i = 0; i < len; i++)      {        h = 31 * h + fsb.charAt(off);        off++;      }      m_hash = h;    }    */    return super.hashCode(); // h;  }  /**   * Tests if this string starts with the specified prefix beginning   * a specified index.   *   * @param   prefix    the prefix.   * @param   toffset   where to begin looking in the string.   * @return  <code>true</code> if the character sequence represented by the   *          argument is a prefix of the substring of this object starting   *          at index <code>toffset</code>; <code>false</code> otherwise.   *          The result is <code>false</code> if <code>toffset</code> is   *          negative or greater than the length of this   *          <code>String</code> object; otherwise the result is the same   *          as the result of the expression   *          <pre>   *          this.subString(toffset).startsWith(prefix)   *          </pre>   * @exception java.lang.NullPointerException if <code>prefix</code> is   *          <code>null</code>.   */  public boolean startsWith(XMLString prefix, int toffset)  {    FastStringBuffer fsb = fsb();    int to = m_start + toffset;    int tlim = m_start + m_length;    int po = 0;    int pc = prefix.length();    // Note: toffset might be near -1>>>1.    if ((toffset < 0) || (toffset > m_length - pc))    {      return false;    }    while (--pc >= 0)    {      if (fsb.charAt(to) != prefix.charAt(po))      {        return false;      }      to++;      po++;    }    return true;  }  /**   * Tests if this string starts with the specified prefix.   *   * @param   prefix   the prefix.   * @return  <code>true</code> if the character sequence represented by the   *          argument is a prefix of the character sequence represented by   *          this string; <code>false</code> otherwise.   *          Note also that <code>true</code> will be returned if the   *          argument is an empty string or is equal to this   *          <code>String</code> object as determined by the   *          {@link #equals(Object)} method.   * @exception java.lang.NullPointerException if <code>prefix</code> is   *          <code>null</code>.   * @since   JDK1. 0   */  public boolean startsWith(XMLString prefix)  {    return startsWith(prefix, 0);  }  /**   * Returns the index within this string of the first occurrence of the   * specified character. If a character with value <code>ch</code> occurs   * in the character sequence represented by this <code>String</code>   * object, then the index of the first such occurrence is returned --   * that is, the smallest value <i>k</i> such that:   * <blockquote><pre>   * this.charAt(<i>k</i>) == ch   * </pre></blockquote>   * is <code>true</code>. If no such character occurs in this string,   * then <code>-1</code> is returned.   *   * @param   ch   a character.   * @return  the index of the first occurrence of the character in the   *          character sequence represented by this object, or   *          <code>-1</code> if the character does not occur.   */  public int indexOf(int ch)  {    return indexOf(ch, 0);  }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?