001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.util.parse.metapattern;
018
019import java.util.regex.Matcher;
020
021/**
022 * A Group that captures floating point values (doubles and floats).
023 * 
024 * @author Jonathan Locke
025 */
026public final class FloatingPointGroup extends Group
027{
028        private static final long serialVersionUID = 1L;
029
030        /**
031         * Constructs an FloatingPointGroup that parses Strings that match the FLOATING_POINT_NUMBER
032         * pattern.
033         * 
034         * @see MetaPattern#FLOATING_POINT_NUMBER
035         */
036        public FloatingPointGroup()
037        {
038                super(FLOATING_POINT_NUMBER);
039        }
040
041        /**
042         * @param matcher
043         *            The matcher
044         * @return The value
045         */
046        public float getFloat(final Matcher matcher)
047        {
048                return getFloat(matcher, -1);
049        }
050
051        /**
052         * Gets float by parsing the String matched by this capturing group.
053         * 
054         * @param matcher
055         *            The matcher
056         * @param defaultValue
057         *            The default value to use if this group is omitted because it is optional
058         * @return The parsed value
059         */
060        public float getFloat(final Matcher matcher, final float defaultValue)
061        {
062                final String value = get(matcher);
063                return value == null ? defaultValue : Float.parseFloat(value);
064        }
065
066        /**
067         * @param matcher
068         *            The matcher
069         * @return The value
070         */
071        public double getDouble(final Matcher matcher)
072        {
073                return getDouble(matcher, -1);
074        }
075
076        /**
077         * Gets double by parsing the String matched by this capturing group.
078         * 
079         * @param matcher
080         *            The matcher
081         * @param defaultValue
082         *            The default value to use if this group is omitted because it is optional
083         * @return The parsed value
084         */
085        public double getDouble(final Matcher matcher, final double defaultValue)
086        {
087                final String value = get(matcher);
088                return value == null ? defaultValue : Double.parseDouble(value);
089        }
090}