📄 rulebasedcollator.java
字号:
secondary_seq, tertiary_seq, equality_seq, elt.expansionOrdering, elt.ignore)); } this.inverseAccentComparison = inverseComparisons; ce_table = v.toArray(); last_primary_value = primary_seq+1; last_tertiary_value = last_tertiary_seq+1; } /** * Build a tree where all keys are the texts of collation elements and data is * the collation element itself. The tree is used when extracting all prefix * for a given text. */ private void buildPrefixAccess() { prefix_tree = new HashMap(); for (int i = 0; i < ce_table.length; i++) { CollationElement e = (CollationElement) ce_table[i]; prefix_tree.put(e.key, e); } } /** * 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 source The first <code>String</code> to compare. * @param target A second <code>String</code> to compare to the first. * * @return A negative integer if source < target, a positive integer * if source > target, or 0 if source == target. */ public int compare(String source, String target) { CollationElementIterator cs, ct; CollationElement ord1block = null; CollationElement ord2block = null; boolean advance_block_1 = true; boolean advance_block_2 = true; cs = getCollationElementIterator(source); ct = getCollationElementIterator(target); for(;;) { int ord1; int ord2; /* * We have to check whether the characters are ignorable. * If it is the case then forget them. */ if (advance_block_1) { ord1block = cs.nextBlock(); if (ord1block != null && ord1block.ignore) continue; } if (advance_block_2) { ord2block = ct.nextBlock(); if (ord2block != null && ord2block.ignore) { advance_block_1 = false; continue; } } else advance_block_2 = true; if (!advance_block_1) advance_block_1 = true; if (ord1block != null) ord1 = ord1block.getValue(); else { if (ord2block == null) return 0; return -1; } if (ord2block == null) return 1; ord2 = ord2block.getValue(); // We know chars are totally equal, so skip if (ord1 == ord2) { if (getStrength() == IDENTICAL) if (!ord1block.key.equals(ord2block.key)) return ord1block.key.compareTo(ord2block.key); continue; } // Check for primary strength differences int prim1 = CollationElementIterator.primaryOrder(ord1); int prim2 = CollationElementIterator.primaryOrder(ord2); if (prim1 == 0 && getStrength() < TERTIARY) { advance_block_2 = false; continue; } else if (prim2 == 0 && getStrength() < TERTIARY) { advance_block_1 = false; continue; } 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(ord2); if (tert1 < tert2) return -1; else if (tert1 > tert2) return 1; else if (getStrength() == TERTIARY) continue; // Apparently JDK does this (at least for my test case). return ord1block.key.compareTo(ord2block.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 builds a default collation element without invoking * the database created from the rules passed to the constructor. * * @param c Character which needs a collation element. * @return A valid brand new CollationElement instance. */ CollationElement getDefaultElement(char c) { int v; // Preliminary support for generic accent sorting inversion (I don't know if all // characters in the range should be sorted backward). This is the place // to fix this if needed. if (inverseAccentComparison && (c >= 0x02B9 && c <= 0x0361)) v = 0x0361 - ((int) c - 0x02B9); else v = (short) c; return new CollationElement("" + c, last_primary_value + v, (short) 0, (short) 0, (short) 0, null, false); } /** * This method builds a default collation element for an accented character * without invoking the database created from the rules passed to the constructor. * * @param c Character which needs a collation element. * @return A valid brand new CollationElement instance. */ CollationElement getDefaultAccentedElement(char c) { int v; // Preliminary support for generic accent sorting inversion (I don't know if all // characters in the range should be sorted backward). This is the place // to fix this if needed. if (inverseAccentComparison && (c >= 0x02B9 && c <= 0x0361)) v = 0x0361 - ((int) c - 0x02B9); else v = (short) c; return new CollationElement("" + c, (short) 0, (short) 0, (short) (last_tertiary_value + v), (short) 0, null, false); } /** * This method returns an instance for <code>CollationElementIterator</code> * for the specified <code>String</code> under the collation rules for this * object. * * @param source 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 source) { return new CollationElementIterator(this, source); } /** * This method returns an instance of <code>CollationElementIterator</code> * for the <code>String</code> represented by the specified * <code>CharacterIterator</code>. * * @param source 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 source) { StringBuffer expand = new StringBuffer(""); // Right now we assume that we will read from the beginning of the string. for (char c = source.first(); c != CharacterIterator.DONE; c = source.next()) decomposeCharacter(c, expand); return getCollationElementIterator(expand.toString()); } /** * 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 source 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 source) { CollationElementIterator cei = getCollationElementIterator(source); ArrayList vect = new ArrayList(); int ord = cei.next(); cei.reset(); //set to start of string while (ord != CollationElementIterator.NULLORDER) { // If the primary order is null, it means this is an ignorable // character. if (CollationElementIterator.primaryOrder(ord) == 0) { ord = cei.next(); continue; } switch (getStrength()) { case PRIMARY: ord = CollationElementIterator.primaryOrder(ord); break; case SECONDARY: ord = CollationElementIterator.primaryOrder(ord) << 8; ord |= CollationElementIterator.secondaryOrder(ord); default: break; } vect.add(new Integer(ord)); ord = cei.next(); //increment to next key } Object[] objarr = vect.toArray(); byte[] key = new byte[objarr.length * 4]; for (int i = 0; i < objarr.length; i++) { int j = ((Integer) objarr[i]).intValue(); key [i * 4] = (byte) ((j & 0xFF000000) >> 24); key [i * 4 + 1] = (byte) ((j & 0x00FF0000) >> 16); key [i * 4 + 2] = (byte) ((j & 0x0000FF00) >> 8); key [i * 4 + 3] = (byte) (j & 0x000000FF); } return new CollationKey(this, source, key); } /** * 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 returns a hash value for this object. * * @return A hash value for this object. */ public int hashCode() { return System.identityHashCode(this); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -