001 /*
002 * $Id: TypePattern.java,v 1.1 2014/01/12 18:57:55 oboehm Exp $
003 *
004 * Copyright (c) 2013 by Oliver Boehm
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 *
018 * (c)reated 12.01.2014 by oliver (ob@oasd.de)
019 */
020
021 package patterntesting.runtime.util.regex;
022
023 import java.util.regex.Pattern;
024
025 /**
026 * A type pattern which matches patterns like "java..*" (all tpyes of the JDK).
027 * These are the same patterns as AspectJ allows.
028 *
029 * @author oliver
030 * @since 1.4.1 (12.01.2014)
031 */
032 public class TypePattern {
033
034 private final Pattern pattern;
035
036 /**
037 * Instantiates a new type pattern.
038 *
039 * @param pattern the pattern
040 */
041 public TypePattern(final String pattern) {
042 this.pattern = Pattern.compile(pattern);
043 }
044
045 /**
046 * Matches.
047 *
048 * @param object the object
049 * @return true, if successful
050 */
051 public boolean matches(final Object object) {
052 if (object == null) {
053 return false;
054 }
055 return matches(object.getClass());
056 }
057
058 /**
059 * Matches.
060 *
061 * @param clazz the clazz
062 * @return true, if successful
063 */
064 public boolean matches(final Class<?> clazz) {
065 if (clazz == null) {
066 return false;
067 }
068 return matches(clazz.getName());
069 }
070
071 /**
072 * Matches.
073 *
074 * @param classname the classname
075 * @return true, if successful
076 */
077 public boolean matches(final String classname) {
078 if (classname == null) {
079 return false;
080 }
081 return this.pattern.matcher(classname).matches();
082 }
083
084 /**
085 * To string.
086 *
087 * @return the string
088 * @see java.lang.Object#toString()
089 */
090 @Override
091 public String toString() {
092 return this.getClass().getSimpleName() + "(" + this.pattern + ")";
093 }
094
095 }
096