KickJava   Java API By Example, From Geeks To Geeks.

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


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

7 package com.ibm.icu.text;
8 import com.ibm.icu.impl.Utility;
9
10 class Quantifier implements UnicodeMatcher {
11
12     private UnicodeMatcher matcher;
13
14     private int minCount;
15
16     private int maxCount;
17
18     /**
19      * Maximum count a quantifier can have.
20      */

21     public static final int MAX = Integer.MAX_VALUE;
22
23     public Quantifier(UnicodeMatcher theMatcher,
24                       int theMinCount, int theMaxCount) {
25         if (theMatcher == null || minCount < 0 || maxCount < 0 || minCount > maxCount) {
26             throw new IllegalArgumentException JavaDoc();
27         }
28         matcher = theMatcher;
29         minCount = theMinCount;
30         maxCount = theMaxCount;
31     }
32
33     /**
34      * Implement UnicodeMatcher API.
35      */

36     public int matches(Replaceable text,
37                        int[] offset,
38                        int limit,
39                        boolean incremental) {
40         int start = offset[0];
41         int count = 0;
42         while (count < maxCount) {
43             int pos = offset[0];
44             int m = matcher.matches(text, offset, limit, incremental);
45             if (m == U_MATCH) {
46                 ++count;
47                 if (pos == offset[0]) {
48                     // If offset has not moved we have a zero-width match.
49
// Don't keep matching it infinitely.
50
break;
51                 }
52             } else if (incremental && m == U_PARTIAL_MATCH) {
53                 return U_PARTIAL_MATCH;
54             } else {
55                 break;
56             }
57         }
58         if (incremental && offset[0] == limit) {
59             return U_PARTIAL_MATCH;
60         }
61         if (count >= minCount) {
62             return U_MATCH;
63         }
64         offset[0] = start;
65         return U_MISMATCH;
66     }
67
68     /**
69      * Implement UnicodeMatcher API
70      */

71     public String JavaDoc toPattern(boolean escapeUnprintable) {
72         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
73         result.append(matcher.toPattern(escapeUnprintable));
74         if (minCount == 0) {
75             if (maxCount == 1) {
76                 return result.append('?').toString();
77             } else if (maxCount == MAX) {
78                 return result.append('*').toString();
79             }
80             // else fall through
81
} else if (minCount == 1 && maxCount == MAX) {
82             return result.append('+').toString();
83         }
84         result.append('{');
85         Utility.appendNumber(result, minCount);
86         result.append(',');
87         if (maxCount != MAX) {
88             Utility.appendNumber(result, maxCount);
89         }
90         result.append('}');
91         return result.toString();
92     }
93
94     /**
95      * Implement UnicodeMatcher API
96      */

97     public boolean matchesIndexValue(int v) {
98         return (minCount == 0) || matcher.matchesIndexValue(v);
99     }
100
101     /**
102      * Implementation of UnicodeMatcher API. Union the set of all
103      * characters that may be matched by this object into the given
104      * set.
105      * @param toUnionTo the set into which to union the source characters
106      * @returns a reference to toUnionTo
107      */

108     public void addMatchSetTo(UnicodeSet toUnionTo) {
109         if (maxCount > 0) {
110             matcher.addMatchSetTo(toUnionTo);
111         }
112     }
113 }
114
115 //eof
116
Popular Tags