下面列出了怎么用java.text.RuleBasedCollator的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* We do not anticipate this method on collation sensitive DVD to be
* ever called in Derby 10.3 In future, when Derby will start supporting
* SQL standard COLLATE clause, this method might get called on the
* collation sensitive DVDs.
*
* @see StringDataValue#getValue(RuleBasedCollator)
*/
public StringDataValue getValue(RuleBasedCollator collatorForComparison)
{
if (collatorForComparison != null)
{
//non-null collatorForComparison means use this collator sensitive
//implementation of SQLClob
setCollator(collatorForComparison);
return this;
} else {
//null collatorForComparison means use UCS_BASIC for collation.
//For that, we need to use the base class SQLClob
SQLClob s = new SQLClob();
s.copyState(this);
return s;
}
}
/**
* We do not anticipate this method on collation sensitive DVD to be
* ever called in Derby 10.3 In future, when Derby will start supporting
* SQL standard COLLATE clause, this method might get called on the
* collation sensitive DVDs.
*
* @see StringDataValue#getValue(RuleBasedCollator)
*/
public StringDataValue getValue(RuleBasedCollator collatorForComparison)
{
if (collatorForComparison != null)
{
//non-null collatorForComparison means use this collator sensitive
//implementation of SQLLongvarchar
setCollator(collatorForComparison);
return this;
} else {
//null collatorForComparison means use UCS_BASIC for collation.
//For that, we need to use the base class SQLLongvarchar
SQLLongvarchar s = new SQLLongvarchar();
s.copyState(this);
return s;
}
}
private Collator getCollator() {
RuleBasedCollator base = (RuleBasedCollator)Collator.getInstance();
String rule = base.getRules();
try {
return new RuleBasedCollator(rule
+ "&B < \ud800\udc01 < \ud800\udc00"
+ ", \ud800\udc02, \ud800\udc03"
+ "; \ud800\udc04, \ud800\udc05"
+ "< \ud800\udc06 < \ud800\udc07"
+ "&FE < \ud800\udc08"
+ "&PE, \ud800\udc09"
+ "&Z < \ud800\udc0a < \ud800\udc0b < \ud800\udc0c"
+ "&\ud800\udc0a < x, X"
+ "&A < \ud800\udc04\ud800\udc05");
} catch (Exception e) {
errln("Failed to create new RulebasedCollator object");
return null;
}
}
/**
* We do not anticipate this method on collation sensitive DVD to be
* ever called in Derby 10.3 In future, when Derby will start supporting
* SQL standard COLLATE clause, this method might get called on the
* collation sensitive DVDs.
*
* @see StringDataValue#getValue(RuleBasedCollator)
*/
public StringDataValue getValue(RuleBasedCollator collatorForComparison)
{
if (collatorForComparison != null)
{
//non-null collatorForComparison means use this collator sensitive
//implementation of SQLChar
setCollator(collatorForComparison);
return this;
} else {
//null collatorForComparison means use UCS_BASIC for collation.
//For that, we need to use the base class SQLChar
SQLChar s = new SQLChar();
s.copyState(this);
return s;
}
}
private Collator getCollator() {
RuleBasedCollator base = (RuleBasedCollator)Collator.getInstance();
String rule = base.getRules();
try {
return new RuleBasedCollator(rule
+ "&B < \ud800\udc01 < \ud800\udc00"
+ ", \ud800\udc02, \ud800\udc03"
+ "; \ud800\udc04, \ud800\udc05"
+ "< \ud800\udc06 < \ud800\udc07"
+ "&FE < \ud800\udc08"
+ "&PE, \ud800\udc09"
+ "&Z < \ud800\udc0a < \ud800\udc0b < \ud800\udc0c"
+ "&\ud800\udc0a < x, X"
+ "&A < \ud800\udc04\ud800\udc05");
} catch (Exception e) {
errln("Failed to create new RulebasedCollator object");
return null;
}
}
private static synchronized Collator updateCollatorRules(Collator collator)
{
if (collator instanceof RuleBasedCollator)
{
try
{
// get current collator rules
String collatorRules = ((RuleBasedCollator)collator).getRules();
// we shoudn't ignore space character in character comparison - put it before u0021 character
String newCollatorRules = collatorRules.replaceAll("<'\u0021'", "<'\u0020'<'\u0021'");
// create new collator with overridden rules
return new RuleBasedCollator(newCollatorRules);
}
catch(ParseException e)
{
return collator;
}
}
return collator;
}
/**
* Constructor for most general range lookup table
*
* @param id id
* @param metadata metadata defining this lookup table
* @param parser parser for reading defining records
* @param collator collator for comparing string fields
* @param startInclude indicates whether start points belong to the intervals or not
* @param endInclude indicates whether end points belong to the intervals or not
*/
public RangeLookupTable(String id, DataRecordMetadata metadata, String[] startFields,
String[] endFields, Parser parser, RuleBasedCollator collator, boolean[] startInclude, boolean[] endInclude){
super(id);
this.metadata = metadata;
this.startFields = startFields;
this.endFields = endFields;
this.dataParser = parser;
this.collators = new RuleBasedCollator[metadata.getFields().length];
Arrays.fill(collators, collator);
if (startInclude.length != (metadata.getNumFields() - 1)/2) {
throw new InvalidParameterException("startInclude parameter has wrong number " +
"of elements: " + startInclude.length + " (should be " +
(metadata.getNumFields() - 1)/2 + ")");
}
this.startInclude = startInclude;
if (endInclude.length != (metadata.getNumFields() - 1)/2) {
throw new InvalidParameterException("endInclude parameter has wrong number " +
"of elements: " + endInclude.length + " (should be " +
(metadata.getNumFields() - 1)/2 + ")");
}
this.endInclude = endInclude;
}
/** @see DataValueFactory#getCharacterCollator(int) */
public RuleBasedCollator getCharacterCollator(int collationType)
throws StandardException {
if (collationType == StringDataValue.COLLATION_TYPE_UCS_BASIC)
return (RuleBasedCollator)null;
else if (collatorForCharacterTypes == null) {
//This is the first access to Collator because otherwise
//it will not be null. Verify that JVM has support for
//the Collator for the database locale.
// Calculate the collator strength. COLLATION_TYPE_TERRITORY_BASED use strength -1, i e unspecified.
int strength = collationType - StringDataValue.COLLATION_TYPE_TERRITORY_BASED_PRIMARY;
collatorForCharacterTypes = verifyCollatorSupport(strength);
return collatorForCharacterTypes;
} else
return collatorForCharacterTypes;
}
/**
* Verify that JVM has support for the Collator for the datbase's locale.
*
* @param strength Collator strength or -1 for locale default.
* @return Collator for database's locale
* @throws StandardException if JVM does not have support for Collator
*/
private RuleBasedCollator verifyCollatorSupport(int strength)
throws StandardException {
Locale[] availLocales = Collator.getAvailableLocales();
//Verify that Collator can be instantiated for the given locale.
boolean localeFound = false;
for (int i=0; i<availLocales.length;i++)
{
if (availLocales[i].equals(databaseLocale)) {
localeFound = true;
break;
}
}
if (!localeFound)
throw StandardException.newException(
SQLState.COLLATOR_NOT_FOUND_FOR_LOCALE,
(databaseLocale != null ? databaseLocale.toString() : "null"));
RuleBasedCollator collator = (RuleBasedCollator)Collator.getInstance(databaseLocale);
if (strength != -1)
collator.setStrength(strength);
return collator;
}
/**
* We do not anticipate this method on collation sensitive DVD to be
* ever called in Derby 10.3 In future, when Derby will start supporting
* SQL standard COLLATE clause, this method might get called on the
* collation sensitive DVDs.
*
* @see StringDataValue#getValue(RuleBasedCollator)
*/
public StringDataValue getValue(RuleBasedCollator collatorForComparison)
{
if (collatorForComparison != null)
{
//non-null collatorForComparison means use this collator sensitive
//implementation of SQLVarchar
setCollator(collatorForComparison);
return this;
} else {
//null collatorForComparison means use UCS_BASIC for collation.
//For that, we need to use the base class SQLVarchar
SQLVarchar s = new SQLVarchar();
s.copyState(this);
return s;
}
}
/**
* Verify that JVM has support for the Collator for the datbase's locale.
*
* @param strength Collator strength or -1 for locale default.
* @return Collator for database's locale
* @throws StandardException if JVM does not have support for Collator
*/
private RuleBasedCollator verifyCollatorSupport(int strength)
throws StandardException {
Locale[] availLocales = Collator.getAvailableLocales();
//Verify that Collator can be instantiated for the given locale.
boolean localeFound = false;
for (Locale availLocale : availLocales) {
if (availLocale.equals(databaseLocale)) {
localeFound = true;
break;
}
}
if (!localeFound)
throw StandardException.newException(
SQLState.COLLATOR_NOT_FOUND_FOR_LOCALE,
databaseLocale.toString());
RuleBasedCollator collator = (RuleBasedCollator)Collator.getInstance(databaseLocale);
if (strength != -1)
collator.setStrength(strength);
return collator;
}
/**
* Compares this object with the specified object for order -
* respecting i18n particularities - e.g. "e" versus "??".<br>
* Using this method requires lots of resources and is therefore
* much slower than simple compareTo(Object obj) method.
*
* @param obj Any object implementing CharSequence interface
* @param collator Collator which should be used to compare
* string representations respecting i18n particularities
* @return -1;0;1 based on comparison result
*/
public int compareTo(Object obj,RuleBasedCollator collator) {
CharSequence strObj;
if (isNull) return -1;
if (obj == null) return 1;
if (obj instanceof StringDataField) {
if (((StringDataField) obj).isNull())
return 1;
strObj=((StringDataField) obj).value;
}else if (obj instanceof CharSequence) {
strObj = (CharSequence) obj;
}else {
throw new ClassCastException("Can't compare StringDataField to "
+ obj.getClass().getName());
}
return Compare.compare(value, strObj, collator);
}
/**
*
* @param metadata
*/
public void updateCollators(DataRecordMetadata metadata) {
Locale[] metadataLocale = getLocaleFromMetadata(metadata, keyFields);
if (metadataLocale == null) return;
Integer[] iSensitivity = getSensitivityFromMetadata(metadata, keyFields);
if (collators == null) collators = new RuleBasedCollator[keyFields.length];
for (int i=0; i<keyFields.length; i++) {
//collator was prepared from outside the comparator and this collator has higher priority
if (collators[i] != null || metadataLocale[i] == null) continue;
collators[i] = (RuleBasedCollator)Collator.getInstance(metadataLocale[i]);
if (iSensitivity != null && iSensitivity[i] != null) collators[i].setStrength(iSensitivity[i].intValue());
collators[i].setDecomposition(Collator.CANONICAL_DECOMPOSITION);
useCollator = true;
}
}
/**
* We do not anticipate this method on collation sensitive DVD to be
* ever called in Derby 10.3 In future, when Derby will start supporting
* SQL standard COLLATE clause, this method might get called on the
* collation sensitive DVDs.
*
* @see StringDataValue#getValue(RuleBasedCollator)
*/
public StringDataValue getValue(RuleBasedCollator collatorForComparison)
{
if (collatorForComparison != null)
{
//non-null collatorForComparison means use this collator sensitive
//implementation of SQLVarchar
setCollator(collatorForComparison);
return this;
} else {
//null collatorForComparison means use UCS_BASIC for collation.
//For that, we need to use the base class SQLVarchar
SQLVarchar s = new SQLVarchar();
s.copyState(this);
return s;
}
}
public void TestDemoTest2() {
final Collator myCollation = Collator.getInstance(Locale.US);
final String defRules = ((RuleBasedCollator)myCollation).getRules();
String newRules = defRules + "& C < ch , cH, Ch, CH";
try {
RuleBasedCollator tblColl = new RuleBasedCollator(newRules);
for (int j = 0; j < TOTALTESTSET; j++) {
for (int n = j+1; n < TOTALTESTSET; n++) {
doTest(tblColl, testCases[Test2Results[j]],
testCases[Test2Results[n]], -1);
}
}
} catch (Exception foo) {
errln("Exception: " + foo.getMessage() +
"\nDemo Test 2 Table Collation object creation failed.\n");
}
}
public final static Comparable getComparator( final String text, final Locale locale, final Collator collator, final String caseOrder){
if((caseOrder == null) ||(caseOrder.length() == 0)){// no case-order specified
return ((RuleBasedCollator)collator).getCollationKey(text);
}else{
return new StringComparable(text, locale, collator, caseOrder);
}
}
/** @see StringDataValue#getValue(RuleBasedCollator) */
public StringDataValue getValue(RuleBasedCollator collatorForComparison)
{
if (collatorForComparison == null)
{//null collatorForComparison means use UCS_BASIC for collation
return this;
} else {
//non-null collatorForComparison means use collator sensitive
//implementation of SQLChar
CollatorSQLChar s = new CollatorSQLChar(collatorForComparison);
s.copyState(this);
return s;
}
}
/**
* Constructs a RecordComparator based on particular metadata and settings
*
* @param metaData
* @return
*/
private RecordOrderedKey buildRecordOrderedKey(String joiners[], DataRecordMetadata metaData) {
boolean[] ordering = new boolean[joiners.length];
Arrays.fill(ordering, true);//TODO change the key dialog
if (locale != null) {
RuleBasedCollator col = (RuleBasedCollator)Collator.getInstance(MiscUtils.createLocale(locale));
col.setStrength(caseSensitive ? Collator.TERTIARY : Collator.SECONDARY);
col.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
return new RecordOrderedKey(joiners, ordering, metaData, col);
} else {
return new RecordOrderedKey(joiners, ordering, metaData);
}
}
/**
* RESOLVE - unfinished LIKE test with dataset of all unicode characters
**/
private static final void printRuleBasedCollator()
{
// get en_US Collator rules
RuleBasedCollator en_USCollator =
(RuleBasedCollator)Collator.getInstance(Locale.US);
String en_rules = en_USCollator.getRules();
System.out.println("ENGLISH RULES: " + en_rules);
System.out.println("ENGLISH RULES: " + formatString(en_rules, true));
System.out.println("ENGLISH RULES: " + formatString(en_rules, false));
}
public StringComparable(final String text, final Locale locale, final Collator collator, final String caseOrder){
m_text = text;
m_locale = locale;
m_collator = (RuleBasedCollator)collator;
m_caseOrder = caseOrder;
m_mask = getMask(m_collator.getStrength());
}
public final static Comparable getComparator( final String text, final Locale locale, final Collator collator, final String caseOrder){
if((caseOrder == null) ||(caseOrder.length() == 0)){// no case-order specified
return ((RuleBasedCollator)collator).getCollationKey(text);
}else{
return new StringComparable(text, locale, collator, caseOrder);
}
}
public StringComparable(final String text, final Locale locale, final Collator collator, final String caseOrder){
m_text = text;
m_locale = locale;
m_collator = (RuleBasedCollator)collator;
m_caseOrder = caseOrder;
m_mask = getMask(m_collator.getStrength());
}
public final static Comparable getComparator( final String text, final Locale locale, final Collator collator, final String caseOrder){
if((caseOrder == null) ||(caseOrder.length() == 0)){// no case-order specified
return ((RuleBasedCollator)collator).getCollationKey(text);
}else{
return new StringComparable(text, locale, collator, caseOrder);
}
}
public StringComparable(final String text, final Locale locale, final Collator collator, final String caseOrder){
m_text = text;
m_locale = locale;
m_collator = (RuleBasedCollator)collator;
m_caseOrder = caseOrder;
m_mask = getMask(m_collator.getStrength());
}
public final static Comparable getComparator( final String text, final Locale locale, final Collator collator, final String caseOrder){
if((caseOrder == null) ||(caseOrder.length() == 0)){// no case-order specified
return ((RuleBasedCollator)collator).getCollationKey(text);
}else{
return new StringComparable(text, locale, collator, caseOrder);
}
}
public static Boolean like(char[] value, int valueLength, char[] pattern,
int patternLength, RuleBasedCollator collator)
throws StandardException {
if (value == null || pattern == null) return null;
return like(value, valueLength, pattern, patternLength, null, 0,
collator);
}
public int compare(Object o1, Object o2) {
SelectItem i1 = (SelectItem)o1;
SelectItem i2 = (SelectItem)o2;
try{
RuleBasedCollator r_collator= new RuleBasedCollator(((RuleBasedCollator)Collator.getInstance()).getRules().replaceAll("<'\u005f'", "<' '<'\u005f'"));
return r_collator.compare(i1.getLabel(), i2.getLabel());
}catch(ParseException e){
return Collator.getInstance().compare(i1.getLabel(),i2.getLabel());
}
}
public int compare(Object o1, Object o2) {
try{
RuleBasedCollator r_collator= new RuleBasedCollator(((RuleBasedCollator)Collator.getInstance()).getRules().replaceAll("<'\u005f'", "<' '<'\u005f'"));
return r_collator.compare(((EnrollmentRecord)o1).getUser().getSortName(),((EnrollmentRecord)o2).getUser().getSortName());
}catch(ParseException e){
return Collator.getInstance().compare(((EnrollmentRecord)o1).getUser().getSortName(),((EnrollmentRecord)o2).getUser().getSortName());
}
}
public StringComparable(final String text, final Locale locale, final Collator collator, final String caseOrder){
m_text = text;
m_locale = locale;
m_collator = (RuleBasedCollator)collator;
m_caseOrder = caseOrder;
m_mask = getMask(m_collator.getStrength());
}
public StringComparable(final String text, final Locale locale, final Collator collator, final String caseOrder){
m_text = text;
m_locale = locale;
m_collator = (RuleBasedCollator)collator;
m_caseOrder = caseOrder;
m_mask = getMask(m_collator.getStrength());
}