KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > text > UnicodeFilter


1 /*
2  *******************************************************************************
3  * Copyright (C) 1996-2006, International Business Machines Corporation and *
4  * others. All Rights Reserved. *
5  *******************************************************************************
6  */

7 package com.ibm.icu.text;
8
9 /**
10  * <code>UnicodeFilter</code> defines a protocol for selecting a
11  * subset of the full range (U+0000 to U+FFFF) of Unicode characters.
12  * Currently, filters are used in conjunction with classes like {@link
13  * Transliterator} to only process selected characters through a
14  * transformation.
15  * @stable ICU 2.0
16  */

17 public abstract class UnicodeFilter implements UnicodeMatcher {
18
19     /**
20      * Returns <tt>true</tt> for characters that are in the selected
21      * subset. In other words, if a character is <b>to be
22      * filtered</b>, then <tt>contains()</tt> returns
23      * <b><tt>false</tt></b>.
24      * @stable ICU 2.0
25      */

26     public abstract boolean contains(int c);
27
28     /**
29      * Default implementation of UnicodeMatcher::matches() for Unicode
30      * filters. Matches a single 16-bit code unit at offset.
31      * @stable ICU 2.0
32      */

33     public int matches(Replaceable text,
34                        int[] offset,
35                        int limit,
36                        boolean incremental) {
37         int c;
38         if (offset[0] < limit &&
39             contains(c = text.char32At(offset[0]))) {
40             offset[0] += UTF16.getCharCount(c);
41             return U_MATCH;
42         }
43         if (offset[0] > limit &&
44             contains(c = text.char32At(offset[0]))) {
45             // Backup offset by 1, unless the preceding character is a
46
// surrogate pair -- then backup by 2 (keep offset pointing at
47
// the lead surrogate).
48
--offset[0];
49             if (offset[0] >= 0) {
50                 offset[0] -= UTF16.getCharCount(text.char32At(offset[0])) - 1;
51             }
52             return U_MATCH;
53         }
54         if (incremental && offset[0] == limit) {
55             return U_PARTIAL_MATCH;
56         }
57         return U_MISMATCH;
58     }
59
60     /**
61      * (This should not be here; it is declared to make CheckTags
62      * happy. Java inserts a synthetic constructor and CheckTags
63      * can't tell that it's synthetic.)
64      *
65      * TODO Remove this when the JDK property implements MemberDoc.isSynthetic
66      * @internal
67      * @deprecated This API is ICU internal only.
68      */

69     protected UnicodeFilter() {}
70 }
71
Popular Tags