]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/text/UnicodeFilter.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / text / UnicodeFilter.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 1996-2006, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 package com.ibm.icu.text;\r
8 \r
9 /**\r
10  * <code>UnicodeFilter</code> defines a protocol for selecting a\r
11  * subset of the full range (U+0000 to U+FFFF) of Unicode characters.\r
12  * Currently, filters are used in conjunction with classes like {@link\r
13  * Transliterator} to only process selected characters through a\r
14  * transformation.\r
15  * @stable ICU 2.0\r
16  */\r
17 public abstract class UnicodeFilter implements UnicodeMatcher {\r
18 \r
19     /**\r
20      * Returns <tt>true</tt> for characters that are in the selected\r
21      * subset.  In other words, if a character is <b>to be\r
22      * filtered</b>, then <tt>contains()</tt> returns\r
23      * <b><tt>false</tt></b>.\r
24      * @stable ICU 2.0\r
25      */\r
26     public abstract boolean contains(int c);\r
27 \r
28     /**\r
29      * Default implementation of UnicodeMatcher::matches() for Unicode\r
30      * filters.  Matches a single 16-bit code unit at offset.\r
31      * @stable ICU 2.0\r
32      */\r
33     public int matches(Replaceable text,\r
34                        int[] offset,\r
35                        int limit,\r
36                        boolean incremental) {\r
37         int c;\r
38         if (offset[0] < limit &&\r
39             contains(c = text.char32At(offset[0]))) {\r
40             offset[0] += UTF16.getCharCount(c);\r
41             return U_MATCH;\r
42         }\r
43         if (offset[0] > limit &&\r
44             contains(c = text.char32At(offset[0]))) {\r
45             // Backup offset by 1, unless the preceding character is a\r
46             // surrogate pair -- then backup by 2 (keep offset pointing at\r
47             // the lead surrogate).\r
48             --offset[0];\r
49             if (offset[0] >= 0) {\r
50                 offset[0] -= UTF16.getCharCount(text.char32At(offset[0])) - 1;\r
51             }\r
52             return U_MATCH;\r
53         }\r
54         if (incremental && offset[0] == limit) {\r
55             return U_PARTIAL_MATCH;\r
56         }\r
57         return U_MISMATCH;\r
58     }\r
59 \r
60     /**\r
61      * (This should not be here; it is declared to make CheckTags\r
62      * happy.  Java inserts a synthetic constructor and CheckTags\r
63      * can't tell that it's synthetic.)\r
64      *\r
65      * TODO Remove this when the JDK property implements MemberDoc.isSynthetic\r
66      * @internal\r
67      * @deprecated This API is ICU internal only.\r
68      */\r
69     protected UnicodeFilter() {}\r
70 }\r