UTF16CollationIterator.java revision f86f25d102340da66b9c7cb6b2d5ecdc0de43ecf
1/* GENERATED SOURCE. DO NOT MODIFY. */
2// © 2016 and later: Unicode, Inc. and others.
3// License & terms of use: http://www.unicode.org/copyright.html#License
4/*
5*******************************************************************************
6* Copyright (C) 2010-2014, International Business Machines
7* Corporation and others.  All Rights Reserved.
8*******************************************************************************
9* UTF16CollationIterator.java, ported from utf16collationiterator.h/.cpp
10*
11* C++ version created on: 2010oct27
12* created by: Markus W. Scherer
13*/
14
15package android.icu.impl.coll;
16
17/**
18 * UTF-16 collation element and character iterator.
19 * Handles normalized UTF-16 text, with length or NUL-terminated.
20 * Unnormalized text is handled by a subclass.
21 * @hide Only a subset of ICU is exposed in Android
22 */
23public class UTF16CollationIterator extends CollationIterator {
24    /**
25     * Partial constructor, see {@link CollationIterator#CollationIterator(CollationData)}.
26     */
27    public UTF16CollationIterator(CollationData d) {
28        super(d);
29    }
30
31    public UTF16CollationIterator(CollationData d, boolean numeric, CharSequence s, int p) {
32        super(d, numeric);
33        seq = s;
34        start = 0;
35        pos = p;
36        limit = s.length();
37    }
38
39    @Override
40    public boolean equals(Object other) {
41        if(!super.equals(other)) { return false; }
42        UTF16CollationIterator o = (UTF16CollationIterator)other;
43        // Compare the iterator state but not the text: Assume that the caller does that.
44        return (pos - start) == (o.pos - o.start);
45    }
46
47    @Override
48    public int hashCode() {
49        assert false : "hashCode not designed";
50        return 42; // any arbitrary constant will do
51    }
52
53    @Override
54    public void resetToOffset(int newOffset) {
55        reset();
56        pos = start + newOffset;
57    }
58
59    @Override
60    public int getOffset() {
61        return pos - start;
62    }
63
64    public void setText(boolean numeric, CharSequence s, int p) {
65        reset(numeric);
66        seq = s;
67        start = 0;
68        pos = p;
69        limit = s.length();
70    }
71
72    @Override
73    public int nextCodePoint() {
74        if(pos == limit) {
75            return Collation.SENTINEL_CP;
76        }
77        char c = seq.charAt(pos++);
78        char trail;
79        if(Character.isHighSurrogate(c) && pos != limit &&
80                Character.isLowSurrogate(trail = seq.charAt(pos))) {
81            ++pos;
82            return Character.toCodePoint(c, trail);
83        } else {
84            return c;
85        }
86    }
87
88    @Override
89    public int previousCodePoint() {
90        if(pos == start) {
91            return Collation.SENTINEL_CP;
92        }
93        char c = seq.charAt(--pos);
94        char lead;
95        if(Character.isLowSurrogate(c) && pos != start &&
96                Character.isHighSurrogate(lead = seq.charAt(pos - 1))) {
97            --pos;
98            return Character.toCodePoint(lead, c);
99        } else {
100            return c;
101        }
102    }
103
104    @Override
105    protected long handleNextCE32() {
106        if(pos == limit) {
107            return NO_CP_AND_CE32;
108        }
109        char c = seq.charAt(pos++);
110        return makeCodePointAndCE32Pair(c, trie.getFromU16SingleLead(c));
111    }
112
113    @Override
114    protected char handleGetTrailSurrogate() {
115        if(pos == limit) { return 0; }
116        char trail;
117        if(Character.isLowSurrogate(trail = seq.charAt(pos))) { ++pos; }
118        return trail;
119    }
120
121    /* boolean foundNULTerminator(); */
122
123    @Override
124    protected void forwardNumCodePoints(int num) {
125        while(num > 0 && pos != limit) {
126            char c = seq.charAt(pos++);
127            --num;
128            if(Character.isHighSurrogate(c) && pos != limit &&
129                    Character.isLowSurrogate(seq.charAt(pos))) {
130                ++pos;
131            }
132        }
133    }
134
135    @Override
136    protected void backwardNumCodePoints(int num) {
137        while(num > 0 && pos != start) {
138            char c = seq.charAt(--pos);
139            --num;
140            if(Character.isLowSurrogate(c) && pos != start &&
141                    Character.isHighSurrogate(seq.charAt(pos-1))) {
142                --pos;
143            }
144        }
145    }
146
147    protected CharSequence seq;
148    protected int start;
149    protected int pos;
150    protected int limit;
151}
152