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: package org.apache.wicket.util.parse.metapattern;
18:
19: import java.util.regex.Matcher;
20:
21: /**
22: * A Group that captures floating point values (doubles and floats).
23: *
24: * @author Jonathan Locke
25: */
26: public final class FloatingPointGroup extends Group {
27: private static final long serialVersionUID = 1L;
28:
29: /**
30: * Constructs an FloatingPointGroup that parses Strings that match the
31: * FLOATING_POINT_NUMBER pattern.
32: *
33: * @see MetaPattern#FLOATING_POINT_NUMBER
34: */
35: public FloatingPointGroup() {
36: super (FLOATING_POINT_NUMBER);
37: }
38:
39: /**
40: * @param matcher
41: * The matcher
42: * @return The value
43: */
44: public float getFloat(final Matcher matcher) {
45: return getFloat(matcher, -1);
46: }
47:
48: /**
49: * Gets float by parsing the String matched by this capturing group.
50: *
51: * @param matcher
52: * The matcher
53: * @param defaultValue
54: * The default value to use if this group is omitted because it
55: * is optional
56: * @return The parsed value
57: */
58: public float getFloat(final Matcher matcher,
59: final float defaultValue) {
60: final String value = get(matcher);
61: return value == null ? defaultValue : Float.parseFloat(value);
62: }
63:
64: /**
65: * @param matcher
66: * The matcher
67: * @return The value
68: */
69: public double getDouble(final Matcher matcher) {
70: return getDouble(matcher, -1);
71: }
72:
73: /**
74: * Gets double by parsing the String matched by this capturing group.
75: *
76: * @param matcher
77: * The matcher
78: * @param defaultValue
79: * The default value to use if this group is omitted because it
80: * is optional
81: * @return The parsed value
82: */
83: public double getDouble(final Matcher matcher,
84: final double defaultValue) {
85: final String value = get(matcher);
86: return value == null ? defaultValue : Double.parseDouble(value);
87: }
88: }
|