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