01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: /**
19: * @author Nikolay A. Kuznetsov
20: * @version $Revision: 1.10.2.3 $
21: */package java.util.regex;
22:
23: /**
24: * Composite (i.e. {n,m}) quantifier node for groups ("(X){n,m}")
25: *
26: * @author Nikolay A. Kuznetsov
27: * @version $Revision: 1.10.2.3 $
28: */
29: class CompositeGroupQuantifierSet extends GroupQuantifierSet {
30:
31: protected Quantifier quantifier = null;
32:
33: int setCounter;
34:
35: /**
36: * Constructs CompositeGroupQuantifierSet
37: * @param quant - given composite quantifier
38: * @param innerSet - given group
39: * @param next - next set after the quantifier
40: */
41: public CompositeGroupQuantifierSet(Quantifier quant,
42: AbstractSet innerSet, AbstractSet next, int type,
43: int setCounter) {
44: super (innerSet, next, type);
45: this .quantifier = quant;
46: this .setCounter = setCounter;
47: }
48:
49: public int matches(int stringIndex, CharSequence testString,
50: MatchResultImpl matchResult) {
51: int enterCounter = matchResult.getEnterCounter(setCounter);
52:
53: if (!innerSet.hasConsumed(matchResult))
54: return next.matches(stringIndex, testString, matchResult);
55:
56: // can't go inner set;
57: if (enterCounter >= quantifier.max()) {
58: return next.matches(stringIndex, testString, matchResult);
59: }
60:
61: // go inner set;
62: matchResult.setEnterCounter(setCounter, ++enterCounter);
63: int nextIndex = innerSet.matches(stringIndex, testString,
64: matchResult);
65:
66: if (nextIndex < 0) {
67: matchResult.setEnterCounter(setCounter, --enterCounter);
68: if (enterCounter >= quantifier.min()) {
69: return next.matches(stringIndex, testString,
70: matchResult);
71: } else {
72: matchResult.setEnterCounter(setCounter, 0);
73: return -1;
74: }
75: } else {
76: matchResult.setEnterCounter(setCounter, 0);
77: return nextIndex;
78: }
79: }
80:
81: public void reset() {
82: quantifier.resetCounter();
83: }
84:
85: protected String getName() {
86: return quantifier.toString();
87: }
88:
89: void setQuantifier(Quantifier quant) {
90: this.quantifier = quant;
91: }
92: }
|