1 package org.apache.commons.jcs.engine.match; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import org.apache.commons.jcs.engine.match.behavior.IKeyMatcher; 23 24 import java.util.HashSet; 25 import java.util.Set; 26 import java.util.regex.Matcher; 27 import java.util.regex.Pattern; 28 29 /** This implementation of the KeyMatcher uses standard Java Pattern matching. */ 30 public class KeyMatcherPatternImpl<K> 31 implements IKeyMatcher<K> 32 { 33 /** Serial version */ 34 private static final long serialVersionUID = 6667352064144381264L; 35 36 /** 37 * Creates a pattern and find matches on the array. 38 * <p> 39 * @param pattern 40 * @param keyArray 41 * @return Set of the matching keys 42 */ 43 @Override 44 public Set<K> getMatchingKeysFromArray( String pattern, Set<K> keyArray ) 45 { 46 Pattern compiledPattern = Pattern.compile( pattern ); 47 48 Set<K> matchingKeys = new HashSet<K>(); 49 50 // Look for matches 51 for (K key : keyArray) 52 { 53 // TODO we might want to match on the toString. 54 if ( key instanceof String ) 55 { 56 Matcher matcher = compiledPattern.matcher( (String) key ); 57 if ( matcher.matches() ) 58 { 59 matchingKeys.add( key ); 60 } 61 } 62 } 63 64 return matchingKeys; 65 } 66 }