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