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 case-sensitive boolean values "true" or "false".
023 * 
024 * @author Jonathan Locke
025 */
026public final class BooleanGroup extends Group
027{
028        private static final long serialVersionUID = 1L;
029
030        /**
031         * Constructs an IntegerGroup that parses Strings that match the INTEGER pattern in base 10.
032         * 
033         * @see MetaPattern#INTEGER
034         */
035        public BooleanGroup()
036        {
037                super(new MetaPattern("true|false"));
038        }
039
040        /**
041         * @param matcher
042         *            The matcher
043         * @return The value
044         * @see BooleanGroup#getBoolean(java.util.regex.Matcher, boolean)
045         */
046        public boolean getBoolean(final Matcher matcher)
047        {
048                return getBoolean(matcher, false);
049        }
050
051        /**
052         * Gets a boolean 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 int value
059         */
060        public boolean getBoolean(final Matcher matcher, final boolean defaultValue)
061        {
062                final String value = get(matcher);
063                return value == null ? defaultValue : value.equals("true");
064        }
065}