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