1adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project/** 2adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project******************************************************************************* 3adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project* Copyright (C) 1996-2005, International Business Machines Corporation and * 4adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project* others. All Rights Reserved. * 5adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project******************************************************************************* 6adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project* 7adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project******************************************************************************* 8adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project*/ 9adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 10cfa6ffebf3a63db7a125c4d9abe7e661fac5cc1fElliott Hughespackage libcore.icu; 11adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 12adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.text.CharacterIterator; 13adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 14adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project/** 15adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project* Collation element iterator JNI wrapper. 16adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project* Iterates over the collation elements of a data string. 17f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes* The iterator supports both forward and backwards full iteration, ie if 18f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes* backwards iteration is performed in the midst of a forward iteration, the 19f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes* result is undefined. 20f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes* To perform a backwards iteration in the midst of a forward iteration, 21f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes* reset() has to be called. 22f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes* This will shift the position to either the start or the last character in the 23adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project* data string depending on whether next() is called or previous(). 24adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project* <pre> 25adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project* RuleBasedCollator coll = Collator.getInstance(); 26adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project* CollationElementIterator iterator = coll.getCollationElementIterator("abc"); 27adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project* int ce = 0; 28adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project* while (ce != CollationElementIterator.NULLORDER) { 29adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project* ce = iterator.next(); 30adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project* } 31adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project* iterator.reset(); 32adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project* while (ce != CollationElementIterator.NULLORDER) { 33adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project* ce = iterator.previous(); 34adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project* } 35adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project* </pre> 36adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project* @author syn wee quek 37adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project* @stable ICU 2.4 38adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project*/ 39cfa6ffebf3a63db7a125c4d9abe7e661fac5cc1fElliott Hughespublic final class CollationElementIteratorICU { 40e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom // public data member ------------------------------------------- 41e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom 42e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom /** 43e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * @stable ICU 2.4 44e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom */ 45e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom public static final int NULLORDER = 0xFFFFFFFF; 46e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom 47e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom // public methods ----------------------------------------------- 48e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom 49e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom /** 50e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * Reset the collation elements to their initial state. 51e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * This will move the 'cursor' to the beginning of the text. 52e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * @stable ICU 2.4 53e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom */ 54e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom public void reset() { 55866e7ae17a3da81a02b0b144e0c9c2b3196d293aElliott Hughes NativeCollation.reset(address); 56e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom } 57e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom 58e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom /** 59e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * Get the ordering priority of the next collation element in the text. 60e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * A single character may contain more than one collation element. 61e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * @return next collation elements ordering, or NULLORDER if the end of the 62e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * text is reached. 63e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * @stable ICU 2.4 64e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom */ 65e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom public int next() { 66866e7ae17a3da81a02b0b144e0c9c2b3196d293aElliott Hughes return NativeCollation.next(address); 67e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom } 68e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom 69e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom /** 70e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * Get the ordering priority of the previous collation element in the text. 71e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * A single character may contain more than one collation element. 72e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * @return previous collation element ordering, or NULLORDER if the end of 73e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * the text is reached. 74e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * @stable ICU 2.4 75e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom */ 76e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom public int previous() { 77866e7ae17a3da81a02b0b144e0c9c2b3196d293aElliott Hughes return NativeCollation.previous(address); 78e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom } 79e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom 80e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom /** 81e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * Get the maximum length of any expansion sequences that end with the 82e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * specified comparison order. 83e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * @param order collation order returned by previous or next. 84e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * @return maximum size of the expansion sequences ending with the collation 85e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * element or 1 if collation element does not occur at the end of 86e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * any expansion sequence 87e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * @stable ICU 2.4 88e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom */ 89e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom public int getMaxExpansion(int order) { 90866e7ae17a3da81a02b0b144e0c9c2b3196d293aElliott Hughes return NativeCollation.getMaxExpansion(address, order); 91e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom } 92e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom 93e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom /** 94e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * Set the text containing the collation elements. 95e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * @param source text containing the collation elements. 96e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * @stable ICU 2.4 97e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom */ 98e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom public void setText(String source) { 99866e7ae17a3da81a02b0b144e0c9c2b3196d293aElliott Hughes NativeCollation.setText(address, source); 100e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom } 101e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom 102e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom public void setText(CharacterIterator source) { 103866e7ae17a3da81a02b0b144e0c9c2b3196d293aElliott Hughes NativeCollation.setText(address, source.toString()); 104e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom } 105e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom 106e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom /** 107e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * Get the offset of the current source character. 108e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * This is an offset into the text of the character containing the current 109e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * collation elements. 110e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * @return offset of the current source character. 111e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * @stable ICU 2.4 112e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom */ 113e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom public int getOffset() { 114866e7ae17a3da81a02b0b144e0c9c2b3196d293aElliott Hughes return NativeCollation.getOffset(address); 115e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom } 116e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom 117e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom /** 118e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * Set the offset of the current source character. 119e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * This is an offset into the text of the character to be processed. 120e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * @param offset The desired character offset. 121e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * @stable ICU 2.4 122e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom */ 123e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom public void setOffset(int offset) { 124866e7ae17a3da81a02b0b144e0c9c2b3196d293aElliott Hughes NativeCollation.setOffset(address, offset); 125e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom } 126e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom 127e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom /** 128e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * Gets the primary order of a collation order. 129e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * @param order the collation order 130e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * @return the primary order of a collation order. 131e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * @stable ICU 2.4 132e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom */ 133e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom public static int primaryOrder(int order) { 134e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom return ((order & PRIMARY_ORDER_MASK_) >> PRIMARY_ORDER_SHIFT_) & 135e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom UNSIGNED_16_BIT_MASK_; 136e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom } 137e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom 138e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom /** 139e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * Gets the secondary order of a collation order. 140e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * @param order the collation order 141e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * @return the secondary order of a collation order. 142e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * @stable ICU 2.4 143e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom */ 144e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom public static int secondaryOrder(int order) { 145e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom return (order & SECONDARY_ORDER_MASK_) >> SECONDARY_ORDER_SHIFT_; 146e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom } 147e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom 148e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom /** 149e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * Gets the tertiary order of a collation order. 150e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * @param order the collation order 151e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * @return the tertiary order of a collation order. 152e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * @stable ICU 2.4 153e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom */ 154e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom public static int tertiaryOrder(int order) { 155e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom return order & TERTIARY_ORDER_MASK_; 156e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom } 157e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom 1581e5d730e58d94c3bfa14b7dde5ab3981fe5a170bElliott Hughes public static CollationElementIteratorICU getInstance(long collatorAddress, String source) { 1591e5d730e58d94c3bfa14b7dde5ab3981fe5a170bElliott Hughes long iteratorAddress = NativeCollation.getCollationElementIterator(collatorAddress, source); 160cfa6ffebf3a63db7a125c4d9abe7e661fac5cc1fElliott Hughes return new CollationElementIteratorICU(iteratorAddress); 161cfa6ffebf3a63db7a125c4d9abe7e661fac5cc1fElliott Hughes } 162e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom 1631e5d730e58d94c3bfa14b7dde5ab3981fe5a170bElliott Hughes private CollationElementIteratorICU(long address) { 164866e7ae17a3da81a02b0b144e0c9c2b3196d293aElliott Hughes this.address = address; 165e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom } 166e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom 167e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom // protected methods -------------------------------------------- 168e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom 169e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom /** 170e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * Garbage collection. 171e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * Close C collator and reclaim memory. 172e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * @stable ICU 2.4 173e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom */ 174e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom @Override protected void finalize() throws Throwable { 175e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom try { 176866e7ae17a3da81a02b0b144e0c9c2b3196d293aElliott Hughes NativeCollation.closeElements(address); 177e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom } finally { 178e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom super.finalize(); 179e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom } 180e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom } 181e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom 182e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom // private data members ----------------------------------------- 183e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom 184e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom /** 185e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * C collator 186e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom */ 1871e5d730e58d94c3bfa14b7dde5ab3981fe5a170bElliott Hughes private final long address; 188e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom 189e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom /** 190e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * ICU constant primary order mask for collation elements 191e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom */ 192e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom private static final int PRIMARY_ORDER_MASK_ = 0xffff0000; 193e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom /** 194e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * ICU constant secondary order mask for collation elements 195e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom */ 196e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom private static final int SECONDARY_ORDER_MASK_ = 0x0000ff00; 197e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom /** 198e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * ICU constant tertiary order mask for collation elements 199e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom */ 200e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom private static final int TERTIARY_ORDER_MASK_ = 0x000000ff; 201e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom /** 202e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * ICU constant primary order shift for collation elements 203e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom */ 204e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom private static final int PRIMARY_ORDER_SHIFT_ = 16; 205e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom /** 206e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * ICU constant secondary order shift for collation elements 207e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom */ 208e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom private static final int SECONDARY_ORDER_SHIFT_ = 8; 209e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom /** 210e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom * Unsigned 16 bit mask 211e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom */ 212e2f58c9501eac730d048199906dc41fe8e4cd6e9Brian Carlstrom private static final int UNSIGNED_16_BIT_MASK_ = 0x0000FFFF; 213adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project} 214