New collation files

X-SVN-Rev: 3984
This commit is contained in:
Syn Wee Quek 2001-03-09 00:34:42 +00:00
parent 0b17b3ca71
commit 554d5b5ade
7 changed files with 1402 additions and 0 deletions

View File

@ -0,0 +1,152 @@
/**
*******************************************************************************
* Copyright (C) 1996-2000, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/text/Attic/CollationAttribute.java,v $
* $Date: 2001/03/09 00:34:42 $
* $Revision: 1.1 $
*
*******************************************************************************
*/
package com.ibm.icu4jni.text;
/**
* Interface for storing ICU collation equivalent enum values.
* Constants with the prefix VALUE corresponds to ICU's UColAttributeValues,
* the rest corresponds to UColAttribute.
* @author syn wee quek
* @since Jan 18 01
*/
public final class CollationAttribute
{
// Collation strength constants ----------------------------------
/**
* Primary collation strength
*/
public static final int VALUE_PRIMARY = 0;
/**
* Secondary collation strength
*/
public static final int VALUE_SECONDARY = 1;
/**
* Tertiary collation strength
*/
public static final int VALUE_TERTIARY = 2;
/**
* Default collation strength
*/
public static final int VALUE_DEFAULT_STRENGTH = VALUE_TERTIARY;
/**
* Quaternary collation strength
*/
public static final int VALUE_QUATERNARY = 3;
/**
* Identical collation strength
*/
public static final int VALUE_IDENTICAL = 15;
// French collation mode constants ---------------------------------
// FRENCH_COLLATION; CASE_LEVEL & DECOMPOSITION_MODE
public static final int VALUE_OFF = 16;
public static final int VALUE_ON = 17;
// ALTERNATE_HANDLING mode constants --------------------------
public static final int VALUE_SHIFTED = 20;
public static final int VALUE_NON_IGNORABLE = 21;
// CASE_FIRST mode constants ----------------------------------
public static final int VALUE_LOWER_FIRST = 24;
public static final int VALUE_UPPER_FIRST = 25;
// NORMALIZATION_MODE mode constants --------------------------
public static final int VALUE_ON_WITHOUT_HANGUL = 28;
// Number of attribute value constants -----------------------------
public static final int VALUE_ATTRIBUTE_VALUE_COUNT = 29;
// Collation attribute constants -----------------------------------
// attribute for direction of secondary weights
public static final int FRENCH_COLLATION = 0;
// attribute for handling variable elements
public static final int ALTERNATE_HANDLING = 1;
// who goes first, lower case or uppercase
public static final int CASE_FIRST = 2;
// do we have an extra case level
public static final int CASE_LEVEL = 3;
// attribute for normalization
public static final int NORMALIZATION_MODE = 4;
// attribute for strength
public static final int STRENGTH = 5;
// attribute count
public static final int ATTRIBUTE_COUNT = 6;
// public methods --------------------------------------------------
/**
* Checks if argument is a valid collation strength
* @param strength potential collation strength
* @return true if strength is a valid collation strength, false otherwise
*/
protected static boolean checkStrength(int strength)
{
if (strength < VALUE_PRIMARY ||
(strength > VALUE_QUATERNARY && strength != VALUE_IDENTICAL))
return false;
return true;
}
/**
* Checks if argument is a valid collation type
* @param type collation type to be checked
* @return true if type is a valid collation type, false otherwise
*/
protected static boolean checkType(int type)
{
if (type < FRENCH_COLLATION || type > STRENGTH)
return false;
return true;
}
/**
* Checks if attribute type and corresponding attribute value is valid
* @param type attribute type
* @param value attribute value
* @return true if the pair is valid, false otherwise
*/
protected static boolean checkAttribute(int type, int value)
{
switch (type)
{
case FRENCH_COLLATION :
if (value >= VALUE_OFF && value <= VALUE_ON)
return true;
break;
case ALTERNATE_HANDLING :
if (value >= VALUE_SHIFTED &&
value <= VALUE_NON_IGNORABLE)
return true;
break;
case CASE_FIRST :
if (value >= VALUE_LOWER_FIRST &&
value <= VALUE_UPPER_FIRST)
return true;
break;
case CASE_LEVEL :
if (value >= VALUE_LOWER_FIRST &&
value <= VALUE_UPPER_FIRST)
return true;
break;
case NORMALIZATION_MODE :
return NormalizationMode.check(value);
case STRENGTH :
checkStrength(value);
}
return false;
}
}

View File

@ -0,0 +1,185 @@
/**
*******************************************************************************
* Copyright (C) 1996-2000, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/text/CollationElementIterator.java,v $
* $Date: 2001/03/09 00:34:42 $
* $Revision: 1.1 $
*
*******************************************************************************
*/
package com.ibm.icu4jni.text;
import com.ibm.icu4jni.common.ErrorCode;
/**
* Collation element iterator JNI wrapper
* @author syn wee quek
* @since Jan 22 01
*/
public final class CollationElementIterator
{
// public data member -------------------------------------------
public static final int NULLORDER = 0xFFFFFFFF;
// public methods -----------------------------------------------
/**
* Reset the collation elements to their initial state.
* This will move the 'cursor' to the beginning of the text.
*/
public void reset()
{
NativeCollation.reset(m_collelemiterator_);
}
/**
* Get the ordering priority of the next collation element in the text.
* A single character may contain more than one collation element.
* @return next collation elements ordering, or NULLORDER if the end of the
* text is reached.
*/
public int next()
{
return NativeCollation.next(m_collelemiterator_);
}
/**
* Get the ordering priority of the previous collation element in the text.
* A single character may contain more than one collation element.
* @return previous collation element ordering, or NULLORDER if the end of
* the text is reached.
*/
public int previous()
{
return NativeCollation.previous(m_collelemiterator_);
}
/**
* Get the maximum length of any expansion sequences that end with the
* specified comparison order.
* @param order collation order returned by previous or next.
* @return maximum size of the expansion sequences ending with the collation
* element or 1 if collation element does not occur at the end of any
* expansion sequence
*/
public int getMaxExpansion(int order)
{
return NativeCollation.getMaxExpansion(m_collelemiterator_, order);
}
/**
* Set the text containing the collation elements.
* @param source text containing the collation elements.
*/
public void setText(String source)
{
NativeCollation.setText(m_collelemiterator_, source);
}
/**
* Get the offset of the current source character.
* This is an offset into the text of the character containing the current
* collation elements.
* @return offset of the current source character.
*/
public int getOffset()
{
return NativeCollation.getOffset(m_collelemiterator_);
}
/**
* Set the offset of the current source character.
* This is an offset into the text of the character to be processed.
* @param offset The desired character offset.
*/
public void setOffset(int offset)
{
NativeCollation.setOffset(m_collelemiterator_, offset);
}
/**
* Gets the primary order of a collation order.
* @param order the collation order
* @return the primary order of a collation order.
*/
public static int primaryOrder(int order)
{
return NativeCollation.primaryOrder(order);
}
/**
* Gets the secondary order of a collation order.
* @param order the collation order
* @return the secondary order of a collation order.
*/
public static int secondaryOrder(int order)
{
return NativeCollation.secondaryOrder(order);
}
/**
* Gets the tertiary order of a collation order.
* @param order the collation order
* @return the tertiary order of a collation order.
*/
public static int tertiaryOrder(int order)
{
return NativeCollation.tertiaryOrder(order);
}
// protected constructor ----------------------------------------
/**
* CollationElementIteratorJNI constructor.
* The only caller of this class should be
* RuleBasedCollator.getCollationElementIterator().
* @param collelemiteratoraddress address of C collationelementiterator
*/
CollationElementIterator(long collelemiteratoraddress)
{
m_collelemiterator_ = collelemiteratoraddress;
}
// protected methods --------------------------------------------
/**
* Set ownership for C collator element iterator
* @param ownership true or false
*/
/*
void setOwnCollationElementIterator(boolean ownership)
{
m_owncollelemiterator_ = ownership;
}
*/
/**
* Garbage collection.
* Close C collator and reclaim memory.
*/
protected void finalize()
{
// if (m_owncollelemiterator_)
NativeCollation.closeElements(m_collelemiterator_);
}
// private data members -----------------------------------------
/**
* C collator
*/
private long m_collelemiterator_;
/**
* Flag indicating if the C data associated with m_collelemiterator_ is
* created by this object. This flag is used to determine if the C data is
* to be destroyed when this object is garbage-collected.
*/
// private boolean m_owncollelemiterator_ = false;
}

View File

@ -0,0 +1,185 @@
/**
*******************************************************************************
* Copyright (C) 1996-2000, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/text/CollationKey.java,v $
* $Date: 2001/03/09 00:34:42 $
* $Revision: 1.1 $
*
*******************************************************************************
*/
package com.ibm.icu4jni.text;
/**
* Collation key wrapper.
* @author syn wee quek
* @since Jan 23 01
*/
public final class CollationKey implements Comparable
{
// public methods -----------------------------------------------
/**
* Bitwise comparison for the collation keys
* @param target CollationKey to be compared
* @return comparison result from Collator, RESULT_LESS, RESULT_EQUAL,
* RESULT_GREATER
*/
public int compareTo(CollationKey target)
{
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 ++)
{
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;
}
/**
* 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
*/
public int compareTo(Object target)
{
return compareTo((CollationKey)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
*/
public boolean equals(Object target)
{
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;
}
/**
* 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.
*/
public int hashCode()
{
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_;
}
/**
* Create the value of the Collation key in term of bytes
* @return value of Collation key in bytes
*/
public byte[] toByteArray()
{
if (m_bytes_ == null || m_bytes_.length == 0)
return null;
byte[] result = new byte[m_bytes_.length];
return (byte[])m_bytes_.clone();
}
// package constructors ----------------------------------------------
/**
* Default constructor, for use by the Collator and its subclasses.
*/
CollationKey()
{
m_hash_ = 0;
}
/**
* Constructor, for use only by the Collator and its subclasses.
*/
CollationKey(byte[] bytes)
{
m_bytes_ = bytes;
m_hash_ = 0;
}
// private data members -----------------------------------------------
private byte m_bytes_[];
/**
* Mask value to retrieve a single unsigned byte
*/
private static final int UNSIGNED_BYTE_MASK_ = 0x00FF;
/**
* Cached hash value
*/
private int m_hash_;
}

View File

@ -0,0 +1,203 @@
/**
*******************************************************************************
* Copyright (C) 1996-2000, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/text/Collator.java,v $
* $Date: 2001/03/09 00:34:42 $
* $Revision: 1.1 $
*
*******************************************************************************
*/
package com.ibm.icu4jni.text;
import java.util.Locale;
import com.ibm.icu4jni.text.RuleBasedCollator;
/**
* Abstract class for C Collation.
* Considerations :
* 1) ErrorCode not returned to user throw exceptions instead
* 2) Similar API to java.text.Collator
* @author syn wee quek
* @since Jan 17 01
*/
public abstract class Collator implements Cloneable
{
// public data member -------------------------------------------
// Collation result constants -----------------------------------
// corresponds to ICU's UCollationResult enum balues
/**
* string a == string b
*/
public static final int RESULT_EQUAL = 0;
/**
* string a > string b
*/
public static final int RESULT_GREATER = 1;
/**
* string a < string b
*/
public static final int RESULT_LESS = -1;
/**
* accepted by most attributes
*/
public static final int RESULT_DEFAULT = -1;
// public methods -----------------------------------------------
/**
* Factory method to create an appropriate Collator which uses the default
* locale collation rules.
* Current implementation createInstance() returns a RuleBasedCollator(Locale)
* instance. The RuleBasedCollator will be created in the following order,
* <ul>
* <li> Data from argument locale resource bundle if found, otherwise
* <li> Data from parent locale resource bundle of arguemtn locale if found,
* otherwise
* <li> Data from built-in default collation rules if found, other
* <li> null is returned
* </ul>
* @return an instance of Collator
*/
public static Collator getInstance()
{
return getInstance(Locale.getDefault());
}
/**
* Factory method to create an appropriate Collator which uses the argument
* locale collation rules.<br>
* Current implementation createInstance() returns a RuleBasedCollator(Locale)
* instance. The RuleBasedCollator will be created in the following order,
* <ul>
* <li> Data from argument locale resource bundle if found, otherwise
* <li> Data from parent locale resource bundle of arguemtn locale if found,
* otherwise
* <li> Data from built-in default collation rules if found, other
* <li> null is returned
* </ul>
* @param locale to be used for collation
* @return an instance of Collator
*/
public static Collator getInstance(Locale locale)
{
RuleBasedCollator result = new RuleBasedCollator(locale);
return result;
}
/**
* Equality check for the argument strings.
* @param source string
* @param target string
* @return true if source is equivalent to target, false otherwise
*/
public boolean equals(String source, String target)
{
return (compare(source, target) == RESULT_EQUAL);
}
/**
* Checks if argument object is equals to this object.
* @param target object
* @return true if source is equivalent to target, false otherwise
*/
public abstract boolean equals(Object target);
/**
* Makes a copy of the current object.
* @return a copy of this object
*/
public abstract Object clone() throws CloneNotSupportedException;
/**
* The comparison function compares the character data stored in two
* different strings. Returns information about whether a string is less
* than, greater than or equal to another string.
* <p>Example of use:
* <pre>
* . Collator myCollation = Collator.createInstance(Locale::US);
* . myCollation.setStrength(Collation.PRIMARY);
* . // result would be Collation.EQUAL ("abc" == "ABC")
* . // (no primary difference between "abc" and "ABC")
* . int result = myCollation.compare("abc", "ABC",3);
* . myCollation.setStrength(Collation.TERTIARY);
* . // result would be Collation.LESS (abc" &lt;&lt;&lt; "ABC")
* . // (with tertiary difference between "abc" and "ABC")
* . int result = myCollation.compare("abc", "ABC",3);
* </pre>
* @param source source string.
* @param target target string.
* @return result of the comparison, Collation.EQUAL, Collation.GREATER
* or Collation.LESS
*/
public abstract int compare(String source, String target);
/**
* Get the decomposition mode of this Collator
* Return values from com.ibm.icu4jni.text.Normalization.
* @return the decomposition mode
*/
public abstract int getDecomposition();
/**
* Set the decomposition mode of the Collator object.
* Argument values from com.ibm.icu4jni.text.Normalization.
* @param decompositionmode the new decomposition mode
*/
public abstract void setDecomposition(int mode);
/**
* Determines the minimum strength that will be use in comparison or
* transformation.
* <p>
* E.g. with strength == Collation.SECONDARY, the tertiary difference
* is ignored
* </p>
* <p>
* E.g. with strength == PRIMARY, the secondary and tertiary difference are
* ignored.
* </p>
* @return the current comparison level.
*/
public abstract int getStrength();
/**
* Sets the minimum strength to be used in comparison or transformation.
* <p>Example of use:
* <pre>
* . Collator myCollation = Collator.createInstance(Locale::US);
* . myCollation.setStrength(Collation.PRIMARY);
* . // result will be "abc" == "ABC"
* . // tertiary differences will be ignored
* . int result = myCollation->compare("abc", "ABC");
* </pre>
* @param strength the new comparison level.
*/
public abstract void setStrength(int strength);
/**
* Get the sort key as an CollationKey object from the argument string.
* To retrieve sort key in terms of byte arrays, use the method as below<br>
* <code>
* Collator collator = Collator.getInstance();
* CollationKey collationkey = collator.getCollationKey("string");
* byte[] array = collationkey.toByteArray();
* </code><br>
* Byte array result are zero-terminated and can be compared using
* java.util.Arrays.equals();
* @param source string to be processed.
* @return the sort key
*/
public abstract CollationKey getCollationKey(String source);
/**
* Returns a hash of this collation object
* @return hash of this collation object
*/
public abstract int hashCode();
}

View File

@ -0,0 +1,247 @@
/**
*******************************************************************************
* Copyright (C) 1996-2000, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/text/Attic/NativeCollation.java,v $
* $Date: 2001/03/09 00:34:42 $
* $Revision: 1.1 $
*
*******************************************************************************
*/
package com.ibm.icu4jni.text;
import com.ibm.icu4jni.common.ErrorCode;
/**
* Package static class for declaring all native methods for collation use.
* @author syn wee quek
* @since Mar 05 2001
*/
final class NativeCollation
{
// library loading ----------------------------------------------
static {
if (ErrorCode.LIBRARY_LOADED)
System.out.println("test");
}
// collator methods ---------------------------------------------
/**
* Method to create a new C Collator using the argument locale rules.
* @param locale locale name
* @return new c collator
*/
static native long openCollator(String locale);
/**
* Method to create a new C Collator using the argument rules.
* @param rules, set of collation rules
* @param normalizationmode default normalization mode
* @param collationstrength default collation strength
* @return new c collator
*/
static native long openCollatorFromRules(String rules,
int normalizationmode,
int collationstrength);
/**
* Close a C collator
* Once closed, a UCollatorOld should not be used.
* @param coll The UCollatorOld to close
*/
static native void closeCollator(long collatoraddress);
/**
* Compare two strings.
* The strings will be compared using the normalization mode and options
* specified in openCollator or openCollatorFromRules
* @param collatoraddress address of the c collator
* @param source The source string.
* @param target The target string.
* @return result of the comparison, Collation.EQUAL,
* Collation.GREATER or Collation.LESS
*/
static native int compare(long collatoraddress, String source,
String target);
/**
* Get the normalization mode for this object.
* The normalization mode influences how strings are compared.
* @param collatoraddress
* @return normalization mode; one of the values from Normalization
*/
static native int getNormalization(long collatoraddress);
/**
* Set the normalization mode used int this object
* The normalization mode influences how strings are compared.
* @param collatoraddress the address of the C collator
* @param normalizationmode desired normalization mode; one of the values
* from Normalization
*/
static native void setNormalization(long collatoraddress,
int normalizationmode);
/**
* Get the collation rules from a UCollator.
* The rules will follow the rule syntax.
* @param collatoraddress the address of the C collator
* @return collation rules.
*/
static native String getRules(long collatoraddress);
/**
* Get a sort key for the argument string
* Sort keys may be compared using java.util.Arrays.equals
* @param collatoraddress address of the C collator
* @param source string for key to be generated
* @return sort key
*/
static native byte[] getSortKey(long collatoraddress, String source);
/**
* Gets the version information for collation.
* @param collatoraddress address of the C collator
* @return version information
*/
// private native String getVersion(int collatoraddress);
/**
* Universal attribute setter.
* @param collatoraddress address of the C collator
* @param type type of attribute to be set
* @param value attribute value
* @exception thrown when error occurs while setting attribute value
*/
static native void setAttribute(long collatoraddress, int type, int value);
/**
* Universal attribute getter
* @param collatoraddress address of the C collator
* @param type type of attribute to be set
* @return attribute value
* @exception thrown when error occurs while getting attribute value
*/
static native int getAttribute(long collatoraddress, int type);
/**
* Thread safe cloning operation
* @param collatoraddress address of C collator to be cloned
* @return address of the new clone
* @exception thrown when error occurs while cloning
*/
static native long safeClone(long collatoraddress);
/**
* Create a CollationElementIterator object that will iterator over the
* elements in a string, using the collation rules defined in this
* RuleBasedCollator
* @param collatoraddress address of C collator
* @param source string to iterate over
* @return address of C collationelementiterator
*/
static native long getCollationElementIterator(long collatoraddress,
String source);
/**
* Returns a hash of this collation object
* @param collatoraddress address of C collator
* @return hash of this collation object
*/
static native int hashCode(long collatoraddress);
// collationelementiterator methods -------------------------------------
/**
* Close a C collation element iterator.
* @param address of C collation element iterator to close.
*/
static native void closeElements(long address);
/**
* Reset the collation elements to their initial state.
* This will move the 'cursor' to the beginning of the text.
* @param address of C collation element iterator to reset.
*/
static native void reset(long address);
/**
* Get the ordering priority of the next collation element in the text.
* A single character may contain more than one collation element.
* @param address if C collation elements containing the text.
* @return next collation elements ordering, or NULLORDER if the end of the
* text is reached.
*/
static native int next(long address);
/**
* Get the ordering priority of the previous collation element in the text.
* A single character may contain more than one collation element.
* @param address of the C collation element iterator containing the text.
* @return previous collation element ordering, or NULLORDER if the end of
* the text is reached.
*/
static native int previous(long address);
/**
* Get the maximum length of any expansion sequences that end with the
* specified comparison order.
* @param address of the C collation element iterator containing the text.
* @param order collation order returned by previous or next.
* @return maximum length of any expansion sequences ending with the
* specified order.
*/
static native int getMaxExpansion(long address, int order);
/**
* Set the text containing the collation elements.
* @param address of the C collation element iterator to be set
* @param source text containing the collation elements.
*/
static native void setText(long address, String source);
/**
* Get the offset of the current source character.
* This is an offset into the text of the character containing the current
* collation elements.
* @param addresss of the C collation elements iterator to query.
* @return offset of the current source character.
*/
static native int getOffset(long address);
/**
* Set the offset of the current source character.
* This is an offset into the text of the character to be processed.
* @param address of the C collation element iterator to set.
* @param offset The desired character offset.
*/
static native void setOffset(long address, int offset);
/**
* Gets the primary order of a collation order.
* @param order the collation order
* @return the primary order of a collation order.
*/
static native int primaryOrder(int order);
/**
* Gets the secondary order of a collation order.
* @param order the collation order
* @return the secondary order of a collation order.
*/
static native int secondaryOrder(int order);
/**
* Gets the tertiary order of a collation order.
* @param order the collation order
* @return the tertiary order of a collation order.
*/
static native int tertiaryOrder(int order);
}

View File

@ -0,0 +1,69 @@
/**
*******************************************************************************
* Copyright (C) 1996-2000, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/text/Attic/NormalizationMode.java,v $
* $Date: 2001/03/09 00:34:42 $
* $Revision: 1.1 $
*
*******************************************************************************
*/
package com.ibm.icu4jni.text;
/**
* Internal interface for storing ICU normalization equivalent enum values.
* Used by RuleBaseCollator.
* @author syn wee quek
* @since Jan 18 01
*/
public final class NormalizationMode
{
// public static data members -----------------------------------
public static final int NO_NORMALIZATION = 1;
/**
* Canonical decomposition
*/
public static final int DECOMP_CAN = 2;
/**
* Compatibility decomposition
*/
public static final int DECOMP_COMPAT = 3;
/**
* Default normalization
*/
public static final int DEFAULT_NORMALIZATION = DECOMP_COMPAT;
/**
* Canonical decomposition followed by canonical composition
*/
public static final int DECOMP_CAN_COMP_COMPAT = 4;
/**
* Compatibility decomposition followed by canonical composition
*/
public static final int DECOMP_COMPAT_COMP_CAN = 5;
/**
* Do not normalize Hangul
*/
public static final int IGNORE_HANGUL = 16;
// public methods ------------------------------------------------------
/**
* Checks if argument is a valid collation strength
* @param strength potential collation strength
* @return true if strength is a valid collation strength, false otherwise
*/
static boolean check(int normalizationmode)
{
if (normalizationmode < NO_NORMALIZATION ||
(normalizationmode > DECOMP_COMPAT_COMP_CAN &&
normalizationmode != IGNORE_HANGUL))
return false;
return true;
}
}

View File

@ -0,0 +1,361 @@
/**
*******************************************************************************
* Copyright (C) 1996-2000, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/text/RuleBasedCollator.java,v $
* $Date: 2001/03/09 00:34:42 $
* $Revision: 1.1 $
*
*******************************************************************************
*/
package com.ibm.icu4jni.text;
import java.util.Locale;
import java.text.ParseException;
import com.ibm.icu4jni.common.ErrorCode;
/**
* Concrete implementation class for Collation.
* @author syn wee quek
* @since Jan 17 01
*/
public final class RuleBasedCollator extends Collator
{
// public constructors ------------------------------------------
/**
* RuleBasedCollator constructor. This takes the table rules and builds a
* collation table out of them. Please see RuleBasedCollator class
* description for more details on the collation rule syntax.
* @param rules the collation rules to build the collation table from.
* @exception ParseException thrown if rules are empty or a Runtime error
* if collator can not be created.
*/
public RuleBasedCollator(String rules) throws ParseException
{
if (rules.length() == 0)
throw new ParseException("Build rules empty.", 0);
m_collator_ = NativeCollation.openCollatorFromRules(rules,
NormalizationMode.DEFAULT_NORMALIZATION,
CollationAttribute.VALUE_DEFAULT_STRENGTH);
}
/**
* RuleBasedCollator constructor. This takes the table rules and builds a
* collation table out of them. Please see RuleBasedCollator class
* description for more details on the collation rule syntax.
* @param rules the collation rules to build the collation table from.
* @param strength collation strength
* @exception ParseException thrown if rules are empty or a Runtime error
* if collator can not be created.
*/
public RuleBasedCollator(String rules, int strength) throws ParseException
{
if (rules.length() == 0)
throw new ParseException("Build rules empty.", 0);
if (!CollationAttribute.checkStrength(strength))
throw ErrorCode.getException(ErrorCode.U_ILLEGAL_ARGUMENT_ERROR);
m_collator_ = NativeCollation.openCollatorFromRules(rules,
NormalizationMode.DEFAULT_NORMALIZATION,
strength);
}
/**
* RuleBasedCollator constructor. This takes the table rules and builds a
* collation table out of them. Please see RuleBasedCollator class
* description for more details on the collation rule syntax.
* @param rules the collation rules to build the collation table from.
* @param strength collation strength
* @param normalizationmode normalization mode
* @exception thrown when constructor error occurs
*/
public RuleBasedCollator(String rules, int normalizationmode, int strength)
{
if (!CollationAttribute.checkStrength(strength) ||
!NormalizationMode.check(normalizationmode)) {
throw ErrorCode.getException(ErrorCode.U_ILLEGAL_ARGUMENT_ERROR);
}
m_collator_ = NativeCollation.openCollatorFromRules(rules,
normalizationmode, strength);
}
// public methods -----------------------------------------------
/**
* Makes a complete copy of the current object.
* @return a copy of this object if data clone is a success, otherwise null
*/
public Object clone()
{
RuleBasedCollator result = null;
long collatoraddress = NativeCollation.safeClone(m_collator_);
result = new RuleBasedCollator(collatoraddress);
return (Collator)result;
}
/**
* The comparison function compares the character data stored in two
* different strings. Returns information about whether a string is less
* than, greater than or equal to another string.
* <p>Example of use:
* <pre>
* . Collator myCollation = Collator.createInstance(Locale::US);
* . myCollation.setStrength(CollationAttribute.VALUE_PRIMARY);
* . // result would be Collator.RESULT_EQUAL ("abc" == "ABC")
* . // (no primary difference between "abc" and "ABC")
* . int result = myCollation.compare("abc", "ABC",3);
* . myCollation.setStrength(CollationAttribute.VALUE_TERTIARY);
* . // result would be Collation::LESS (abc" &lt;&lt;&lt; "ABC")
* . // (with tertiary difference between "abc" and "ABC")
* . int result = myCollation.compare("abc", "ABC",3);
* </pre>
* @param source The source string.
* @param target The target string.
* @return result of the comparison, Collator.RESULT_EQUAL,
* Collator.RESULT_GREATER or Collator.RESULT_LESS
*/
public int compare(String source, String target)
{
return NativeCollation.compare(m_collator_, source, target);
}
/**
* Get the normalization mode for this object.
* The normalization mode influences how strings are compared.
* @return normalization mode; one of the values from NormalizationMode
*/
public int getDecomposition()
{
return NativeCollation.getNormalization(m_collator_);
}
/**
* Set the normalization mode used int this object
* The normalization mode influences how strings are compared.
* @param normalizationmode desired normalization mode; one of the values
* from NormalizationMode
* @exception thrown when argument does not belong to any normalization mode
*/
public void setDecomposition(int decompositionmode)
{
if (!NormalizationMode.check(decompositionmode))
throw ErrorCode.getException(ErrorCode.U_ILLEGAL_ARGUMENT_ERROR);
NativeCollation.setNormalization(m_collator_, decompositionmode);
}
/**
* Determines the minimum strength that will be use in comparison or
* transformation.
* <p>
* E.g. with strength == CollationAttribute.VALUE_SECONDARY, the tertiary difference
* is ignored
* </p>
* <p>
* E.g. with strength == PRIMARY, the secondary and tertiary difference are
* ignored.
* </p>
* @return the current comparison level.
*/
public int getStrength()
{
return NativeCollation.getAttribute(m_collator_,
CollationAttribute.STRENGTH);
}
/**
* Sets the minimum strength to be used in comparison or transformation.
* <p>Example of use:
* <pre>
* . Collator myCollation = Collator.createInstance(Locale::US);
* . myCollation.setStrength(CollationAttribute.VALUE_PRIMARY);
* . // result will be "abc" == "ABC"
* . // tertiary differences will be ignored
* . int result = myCollation->compare("abc", "ABC");
* </pre>
* @param strength the new comparison level.
* @exception thrown when argument does not belong to any collation strength
* mode or error occurs while setting data.
*/
public void setStrength(int strength)
{
if (!CollationAttribute.checkStrength(strength))
throw ErrorCode.getException(ErrorCode.U_ILLEGAL_ARGUMENT_ERROR);
NativeCollation.setAttribute(m_collator_, CollationAttribute.STRENGTH,
strength);
}
/**
* Get the sort key as an CollationKey object from the argument string.
* To retrieve sort key in terms of byte arrays, use the method as below<br>
* <code>
* Collator collator = Collator.getInstance();
* byte[] array = collator.getSortKey(source);
* </code><br>
* Byte array result are zero-terminated and can be compared using
* java.util.Arrays.equals();
* @param source string to be processed.
* @return the sort key
*/
public CollationKey getCollationKey(String source)
{
return new CollationKey(NativeCollation.getSortKey(m_collator_, source));
}
/**
* Get a sort key for the argument string
* Sort keys may be compared using java.util.Arrays.equals
* @param collatoraddress address of the C collator
* @param source string for key to be generated
* @return sort key
*/
public byte[] getSortKey(String source)
{
return NativeCollation.getSortKey(m_collator_, source);
}
/**
* Get the collation rules of this Collation object
* The rules will follow the rule syntax.
* @return collation rules.
*/
public String getRules()
{
return NativeCollation.getRules(m_collator_);
}
/**
* Create a CollationElementIterator object that will iterator over the
* elements in a string, using the collation rules defined in this
* RuleBasedCollator
* @param collatoraddress address of C collator
* @param source string to iterate over
* @return address of C collationelement
* @exception thrown when error occurs
*/
public CollationElementIterator getCollationElementIterator(String source)
{
CollationElementIterator result = new CollationElementIterator(
NativeCollation.getCollationElementIterator(m_collator_, source));
// result.setOwnCollationElementIterator(true);
return result;
}
/**
* Returns a hash of this collation object
* @return hash of this collation object
*/
public int hashCode()
{
// since rules do not change once it is created, we can cache the hash
if (m_hashcode_ == 0) {
m_hashcode_ = NativeCollation.hashCode(m_collator_);
if (m_hashcode_ == 0)
m_hashcode_ = 1;
}
return m_hashcode_;
}
/**
* Checks if argument object is equals to this object.
* @param target object
* @return true if source is equivalent to target, false otherwise
*/
public boolean equals(Object target)
{
if (this == target)
return true;
if (target == null)
return false;
if (getClass() != target.getClass())
return false;
long tgtcollatoraddress = ((RuleBasedCollator)target).m_collator_;
return m_collator_ == tgtcollatoraddress;
}
// package constructor ----------------------------------------
/**
* RuleBasedCollator default constructor. This constructor takes the default
* locale. The only caller of this class should be Collator.getInstance().
* Current implementation createInstance() returns a RuleBasedCollator(Locale)
* instance. The RuleBasedCollator will be created in the following order,
* <ul>
* <li> Data from argument locale resource bundle if found, otherwise
* <li> Data from parent locale resource bundle of arguemtn locale if found,
* otherwise
* <li> Data from built-in default collation rules if found, other
* <li> null is returned
* </ul>
* @param desiredLocale locale used
* @param status error code status
*/
RuleBasedCollator()
{
Locale locale = Locale.getDefault();
m_collator_ = NativeCollation.openCollator(locale.toString());
}
/**
* RuleBasedCollator constructor. This constructor takes a locale. The
* only caller of this class should be Collator.createInstance().
* Current implementation createInstance() returns a RuleBasedCollator(Locale)
* instance. The RuleBasedCollator will be created in the following order,
* <ul>
* <li> Data from argument locale resource bundle if found, otherwise
* <li> Data from parent locale resource bundle of arguemtn locale if found,
* otherwise
* <li> Data from built-in default collation rules if found, other
* <li> null is returned
* </ul>
* @param desiredLocale locale used
* @param status error code status
*/
RuleBasedCollator(Locale locale)
{
m_collator_ = NativeCollation.openCollator(locale.toString());
}
// protected methods --------------------------------------------
/**
* Garbage collection.
* Close C collator and reclaim memory.
*/
protected void finalize()
{
NativeCollation.closeCollator(m_collator_);
}
// private data members -----------------------------------------
/**
* C collator
*/
private long m_collator_;
/**
* Hash code for rules
*/
private int m_hashcode_ = 0;
// private constructor -----------------------------------------
/**
* Private use constructor.
* Does not create any instance of the C collator. Accepts argument as the
* C collator for new instance.
* @param collatoraddress address of C collator
*/
private RuleBasedCollator(long collatoraddress)
{
m_collator_ = collatoraddress;
}
}