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.commons.configuration2.tree;
018
019import org.apache.commons.lang3.StringUtils;
020
021/**
022 * <p>
023 * An enumeration class with several pre-defined {@link NodeMatcher} implementations based on node names.
024 * </p>
025 * <p>
026 * Filtering nodes by their name is a typical use case. Therefore, some default implementations for typical filter
027 * algorithms are already provided. They are available as constants of this class. Because the algorithms are state-less
028 * these instances can be shared and accessed concurrently.
029 * </p>
030 *
031 * @since 2.0
032 */
033public enum NodeNameMatchers implements NodeMatcher<String> {
034    /**
035     * A matcher for exact node name matches. This matcher returns <b>true</b> if and only if the name of the passed in node
036     * equals exactly the given criterion string.
037     */
038    EQUALS {
039        @Override
040        public <T> boolean matches(final T node, final NodeHandler<T> handler, final String criterion) {
041            return StringUtils.equals(criterion, handler.nodeName(node));
042        }
043    },
044
045    /**
046     * A matcher for matches on node names which ignores case. For this matcher the names {@code node}, {@code NODE}, or
047     * {@code NodE} are all the same.
048     */
049    EQUALS_IGNORE_CASE {
050        @Override
051        public <T> boolean matches(final T node, final NodeHandler<T> handler, final String criterion) {
052            return StringUtils.equalsIgnoreCase(criterion, handler.nodeName(node));
053        }
054    }
055}