]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/classes/core/src/com/ibm/icu/text/Quantifier.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / classes / core / src / com / ibm / icu / text / Quantifier.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 2001-2010, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 package com.ibm.icu.text;\r
8 import com.ibm.icu.impl.Utility;\r
9 \r
10 class Quantifier implements UnicodeMatcher {\r
11 \r
12     private UnicodeMatcher matcher;\r
13 \r
14     private int minCount;\r
15 \r
16     private int maxCount;\r
17 \r
18     /**\r
19      * Maximum count a quantifier can have.\r
20      */\r
21     public static final int MAX = Integer.MAX_VALUE;\r
22 \r
23     public Quantifier(UnicodeMatcher theMatcher,\r
24                       int theMinCount, int theMaxCount) {\r
25         if (theMatcher == null || minCount < 0 || maxCount < 0 || minCount > maxCount) {\r
26             throw new IllegalArgumentException();\r
27         }\r
28         matcher = theMatcher;\r
29         minCount = theMinCount;\r
30         maxCount = theMaxCount;\r
31     }\r
32 \r
33     /**\r
34      * Implement UnicodeMatcher API.\r
35      */\r
36     public int matches(Replaceable text,\r
37                        int[] offset,\r
38                        int limit,\r
39                        boolean incremental) {\r
40         int start = offset[0];\r
41         int count = 0;\r
42         while (count < maxCount) {\r
43             int pos = offset[0];\r
44             int m = matcher.matches(text, offset, limit, incremental);\r
45             if (m == U_MATCH) {\r
46                 ++count;\r
47                 if (pos == offset[0]) {\r
48                     // If offset has not moved we have a zero-width match.\r
49                     // Don't keep matching it infinitely.\r
50                     break;\r
51                 }\r
52             } else if (incremental && m == U_PARTIAL_MATCH) {\r
53                 return U_PARTIAL_MATCH;\r
54             } else {\r
55                 break;\r
56             }\r
57         }\r
58         if (incremental && offset[0] == limit) {\r
59             return U_PARTIAL_MATCH;\r
60         }\r
61         if (count >= minCount) {\r
62             return U_MATCH;\r
63         }\r
64         offset[0] = start;\r
65         return U_MISMATCH;\r
66     }\r
67 \r
68     /**\r
69      * Implement UnicodeMatcher API\r
70      */\r
71     public String toPattern(boolean escapeUnprintable) {\r
72         StringBuilder result = new StringBuilder();\r
73         result.append(matcher.toPattern(escapeUnprintable));\r
74         if (minCount == 0) {\r
75             if (maxCount == 1) {\r
76                 return result.append('?').toString();\r
77             } else if (maxCount == MAX) {\r
78                 return result.append('*').toString();\r
79             }\r
80             // else fall through\r
81         } else if (minCount == 1 && maxCount == MAX) {\r
82             return result.append('+').toString();\r
83         }\r
84         result.append('{');\r
85         result.append(Utility.hex(minCount,1));\r
86         result.append(',');\r
87         if (maxCount != MAX) {\r
88             result.append(Utility.hex(maxCount,1));\r
89         }\r
90         result.append('}');\r
91         return result.toString();\r
92     }\r
93 \r
94     /**\r
95      * Implement UnicodeMatcher API\r
96      */\r
97     public boolean matchesIndexValue(int v) {\r
98         return (minCount == 0) || matcher.matchesIndexValue(v);\r
99     }\r
100 \r
101     /**\r
102      * Implementation of UnicodeMatcher API.  Union the set of all\r
103      * characters that may be matched by this object into the given\r
104      * set.\r
105      * @param toUnionTo the set into which to union the source characters\r
106      * @returns a reference to toUnionTo\r
107      */\r
108     public void addMatchSetTo(UnicodeSet toUnionTo) {\r
109         if (maxCount > 0) {\r
110             matcher.addMatchSetTo(toUnionTo);\r
111         }\r
112     }\r
113 }\r
114 \r
115 //eof\r