rulebasedcollator.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 560 行 · 第 1/2 页

JAVA
560
字号
          throw new ParseException("Reset not implemented yet", 0);
        }

      // See if we are still reading characters to skip
      if (ignore_chars == true)
        {
          CollationElement e = new CollationElement(c + "", 0, (short)0, 
                                                    (short)0);
          v.add(e);
          continue;
        }

      sb.append(c);
    }

  ce_table = v.toArray();
}

/*************************************************************************/

/*
 * Instance Methods
 */

/**
  * This method returns a <code>String</code> containing the collation rules
  * for this object.
  *
  * @return The collation rules for this object.
  */
public String
getRules()
{
  return(rules);
}

/*************************************************************************/

/**
  * This method calculates the collation element value for the specified
  * character(s).
  */
int
getCollationElementValue(String str)
{
  CollationElement e = null;

  // The table is sorted.  Change to a binary search later.
  for (int i = 0; i < ce_table.length; i++) 
    if (((CollationElement)ce_table[i]).char_seq.equals(str))
      {
        e = (CollationElement)ce_table[i];
        break;
      }

  if (e == null)
    e = new CollationElement(str, 0xFFFF, (short)0xFF, (short)0xFF);

  int retval = (e.primary << 16) + (e.secondary << 8) + e.tertiary;

  return(retval);
}

/*************************************************************************/

/**
  * This method returns an instance for <code>CollationElementIterator</code>
  * for the specified <code>String</code> under the collation rules for this
  * object.
  *
  * @param str The <code>String</code> to return the <code>CollationElementIterator</code> instance for.
  *
  * @return A <code>CollationElementIterator</code> for the specified <code>String</code>.
  */
public CollationElementIterator
getCollationElementIterator(String str)
{
  return(new CollationElementIterator(this, str));
}  

/*************************************************************************/

/**
  * This method returns an instance of <code>CollationElementIterator</code>
  * for the <code>String</code> represented by the specified
  * <code>CharacterIterator</code>.
  *
  * @param ci The <code>CharacterIterator</code> with the desired <code>String</code>.
  *
  * @return A <code>CollationElementIterator</code> for the specified <code>String</code>.
  */
public CollationElementIterator
getCollationElementIterator(CharacterIterator ci)
{
  StringBuffer sb = new StringBuffer("");

  // Right now we assume that we will read from the beginning of the string.
  char c = ci.first();
  while (c != CharacterIterator.DONE) 
    {
      sb.append(c);
      c = ci.next();
    }

  return(getCollationElementIterator(sb.toString()));
}

/*************************************************************************/

/**
  * This method returns an integer which indicates whether the first
  * specified <code>String</code> is less than, greater than, or equal to
  * the second.  The value depends not only on the collation rules in
  * effect, but also the strength and decomposition settings of this object.
  *
  * @param s1 The first <code>String</code> to compare.
  * @param s2 A second <code>String</code> to compare to the first.
  *
  * @return A negative integer if s1 &lt; s2, a positive integer
  * if s1 &gt; s2, or 0 if s1 == s2.
  */
public int
compare(String s1, String s2)
{
  CollationElementIterator cei1 = getCollationElementIterator(s1);
  CollationElementIterator cei2 = getCollationElementIterator(s2);

  for(;;)
    {
      int ord1 = cei1.next(); 
      int ord2 = cei2.next(); 

      // Check for end of string
      if (ord1 == CollationElementIterator.NULLORDER)
        if (ord2 == CollationElementIterator.NULLORDER)
          return(0);
        else
          return(-1);
      else if (ord2 == CollationElementIterator.NULLORDER)
        return(1);

      // We know chars are totally equal, so skip
      if (ord1 == ord2)
        continue;

      // Check for primary strength differences
      int prim1 = CollationElementIterator.primaryOrder(ord1); 
      int prim2 = CollationElementIterator.primaryOrder(ord2); 

      if (prim1 < prim2)
        return(-1);
      else if (prim1 > prim2)
        return(1);
      else if (getStrength() == PRIMARY)
        continue;

      // Check for secondary strength differences
      int sec1 = CollationElementIterator.secondaryOrder(ord1);
      int sec2 = CollationElementIterator.secondaryOrder(ord2);

      if (sec1 < sec2)
        return(-1);
      else if (sec1 > sec2)
        return(1);
      else if (getStrength() == SECONDARY)
        continue;

      // Check for tertiary differences
      int tert1 = CollationElementIterator.tertiaryOrder(ord1);
      int tert2 = CollationElementIterator.tertiaryOrder(ord1);

      if (tert1 < tert2)
        return(-1);
      else if (tert1 > tert2)
        return(1);
    }
}

/*************************************************************************/

/**
  * This method returns an instance of <code>CollationKey</code> for the
  * specified <code>String</code>.  The object returned will have a
  * more efficient mechanism for its comparison function that could
  * provide speed benefits if multiple comparisons are performed, such
  * as during a sort.
  *
  * @param str The <code>String</code> to create a <code>CollationKey</code> for.
  *
  * @return A <code>CollationKey</code> for the specified <code>String</code>.
  */
public CollationKey
getCollationKey(String str)
{
  CollationElementIterator cei = getCollationElementIterator(str);
  Vector vect = new Vector(25);

  int ord = cei.next();
  while (ord != CollationElementIterator.NULLORDER)
    {
      switch (getStrength())
        {
          case PRIMARY:
             ord = CollationElementIterator.primaryOrder(ord);
             break;

          case SECONDARY:
             ord = CollationElementIterator.secondaryOrder(ord);

          default:
             break;
        }

      vect.add(new Integer(ord)); 
    }

  Object[] objarr = vect.toArray();
  byte[] key = new byte[objarr.length * 4];

  for (int i = 0; i < key.length; i++)
    {
      int j = ((Integer)objarr[i]).intValue();
      key[i++] = (byte)((j & 0xFF000000) >> 24);
      key[i++] = (byte)((j & 0x00FF0000) >> 16);
      key[i++] = (byte)((j & 0x0000FF00) >> 8);
      key[i++] = (byte)(j & 0x000000FF);
    }

  return(new CollationKey(this, str, key));
}

/*************************************************************************/

/**
  * This method tests this object for equality against the specified 
  * object.  This will be true if and only if the specified object is
  * another reference to this object.
  *
  * @param obj The <code>Object</code> to compare against this object.
  *
  * @return <code>true</code> if the specified object is equal to this object, <code>false</code> otherwise.
  */
public boolean
equals(Object obj)
{
  if (obj == this)
    return(true);
  else
    return(false);
}

/*************************************************************************/

/**
  * This method returns a hash value for this object.
  *
  * @return A hash value for this object.
  */
public int
hashCode()
{
  return(System.identityHashCode(this));
}

/*************************************************************************/

/**
  * This method creates a copy of this object.
  *
  * @return A copy of this object.
  */
public Object
clone()
{
  return super.clone();
}

} // class RuleBasedCollator

⌨️ 快捷键说明

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