FileDocCategorySizeDatePackage
CollationKey.javaAPI DocAndroid 1.5 API4856Wed May 06 22:41:04 BST 2009com.ibm.icu4jni.text

CollationKey

public final class CollationKey extends Object implements Comparable
Collation key wrapper, containing the byte array sort key.
author
syn wee quek
stable
ICU 2.4

Fields Summary
private byte[]
m_bytes_
private static final int
UNSIGNED_BYTE_MASK_
Mask value to retrieve a single unsigned byte
private int
m_hash_
Cached hash value
Constructors Summary
CollationKey()
Default constructor, for use by the Collator and its subclasses.

    m_hash_ = 0;
  
CollationKey(byte[] bytes)
Constructor, for use only by the Collator and its subclasses.

    m_bytes_ = bytes;
    m_hash_ = 0;
  
Methods Summary
public intcompareTo(com.ibm.icu4jni.text.CollationKey target)
Bitwise comparison for the collation keys

param
target CollationKey to be compared
return
comparison result from Collator, RESULT_LESS, RESULT_EQUAL, RESULT_GREATER
stable
ICU 2.4

    byte tgtbytes[] = target.m_bytes_;
    
    if (m_bytes_ == null || m_bytes_.length == 0) {
      if (tgtbytes == null || tgtbytes.length == 0) {
        return Collator.RESULT_EQUAL;
      }
      return Collator.RESULT_LESS;
    }
    else {
      if (tgtbytes == null || tgtbytes.length == 0) {
        return Collator.RESULT_GREATER;
      }
    }
        
    int count = m_bytes_.length;
    if (tgtbytes.length < count) {
      count = tgtbytes.length;
    }

    int s,
        t;
    for (int i = 0; i < count; i ++)
    {
      // unable to use Arrays.equals
      s = m_bytes_[i] & UNSIGNED_BYTE_MASK_;
      t = tgtbytes[i] & UNSIGNED_BYTE_MASK_;
      if (s < t) {
        return Collator.RESULT_LESS;
      }
      if (s > t) {
        return Collator.RESULT_GREATER;
      }
    }

    if (m_bytes_.length < target.m_bytes_.length) {
      return Collator.RESULT_LESS;
    }
    
    if (m_bytes_.length > target.m_bytes_.length) {
      return Collator.RESULT_GREATER;
    }
    
    return Collator.RESULT_EQUAL;
  
public intcompareTo(java.lang.Object target)
Bitwise comparison for the collation keys. Argument is casted to CollationKey

param
target CollationKey to be compared
return
comparison result from Collator, RESULT_LESS, RESULT_EQUAL, RESULT_GREATER
stable
ICU 2.4

    return compareTo((CollationKey)target);
  
public booleanequals(java.lang.Object target)
Checks if target object is equal to this object. Target is first casted to CollationKey and bitwise compared.

param
target comparison object
return
true if both objects are equal, false otherwise
stable
ICU 2.4

    if (this == target) {
      return true;
    }
      
    // checks getClass here since CollationKey is final not subclassable
    if (target == null || target.getClass() != getClass()) {
      return false;
    }
    
    return compareTo((CollationKey)target) == Collator.RESULT_EQUAL;
  
public inthashCode()
Creates a hash code for this CollationKey. Compute the hash by iterating sparsely over about 32 (up to 63) bytes spaced evenly through the string. For each byte, multiply the previous hash value by a prime number and add the new byte in, like a linear congruential random number generator, producing a pseudorandom deterministic value well distributed over the output range.

return
hash value of collation key. Hash value is never 0.
stable
ICU 2.4

    if (m_hash_ == 0)
    {
      if (m_bytes_ != null || m_bytes_.length != 0) 
      {                        
        int len = m_bytes_.length;
        int inc = ((len - 32) / 32) + 1;  
        for (int i = 0; i < len;)
        {
          m_hash_ = (m_hash_ * 37) + m_bytes_[i];
          i += inc;                         
        }                                     
      }             
      if (m_hash_ == 0)
        m_hash_ = 1;
    }
    return m_hash_;
  
public byte[]toByteArray()
Create the value of the Collation key in term of bytes

return
value of Collation key in bytes
stable
ICU 2.4

    if (m_bytes_ == null || m_bytes_.length == 0)
      return null;
      
    return (byte[])m_bytes_.clone();