1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html#License
3/*
4*******************************************************************************
5* Copyright (C) 2012-2014, International Business Machines
6* Corporation and others.  All Rights Reserved.
7*******************************************************************************
8* IterCollationIterator.java, ported from uitercollationiterator.h/.cpp
9*
10* C++ version created on: 2012sep23 (from utf16collationiterator.h)
11* created by: Markus W. Scherer
12*/
13
14package com.ibm.icu.impl.coll;
15
16import com.ibm.icu.text.UCharacterIterator;
17
18/**
19 * UCharIterator-based collation element and character iterator.
20 * Handles normalized text, with length or NUL-terminated.
21 * Unnormalized text is handled by a subclass.
22 */
23public class IterCollationIterator extends CollationIterator {
24    public IterCollationIterator(CollationData d, boolean numeric, UCharacterIterator ui) {
25        super(d, numeric);
26        iter = ui;
27    }
28
29    @Override
30    public void resetToOffset(int newOffset) {
31        reset();
32        iter.setIndex(newOffset);
33    }
34
35    @Override
36    public int getOffset() {
37        return iter.getIndex();
38    }
39
40    @Override
41    public int nextCodePoint() {
42        return iter.nextCodePoint();
43    }
44
45    @Override
46    public int previousCodePoint() {
47        return iter.previousCodePoint();
48    }
49
50    @Override
51    protected long handleNextCE32() {
52        int c = iter.next();
53        if(c < 0) {
54            return NO_CP_AND_CE32;
55        }
56        return makeCodePointAndCE32Pair(c, trie.getFromU16SingleLead((char)c));
57    }
58
59    @Override
60    protected char handleGetTrailSurrogate() {
61        int trail = iter.next();
62        if(!isTrailSurrogate(trail) && trail >= 0) { iter.previous(); }
63        return (char)trail;
64    }
65
66    @Override
67    protected void forwardNumCodePoints(int num) {
68        iter.moveCodePointIndex(num);
69    }
70
71    @Override
72    protected void backwardNumCodePoints(int num) {
73        iter.moveCodePointIndex(-num);
74    }
75
76    protected UCharacterIterator iter;
77}
78