728x90
PatternMatchUtils
PatternMatchUtils 는 추상 클래스로 선언된 유틸 클래스이다.
/**
* Utility methods for simple pattern matching, in particular for
* Spring's typical "xxx*", "*xxx" and "*xxx*" pattern styles.
*
* @author Juergen Hoeller
* @since 2.0
*/
public abstract class PatternMatchUtils {
/**
* Match a String against the given pattern, supporting the following simple
* pattern styles: "xxx*", "*xxx", "*xxx*" and "xxx*yyy" matches (with an
* arbitrary number of pattern parts), as well as direct equality.
* @param pattern the pattern to match against
* @param str the String to match
* @return whether the String matches the given pattern
*/
public static boolean simpleMatch(@Nullable String pattern, @Nullable String str) {
if (pattern == null || str == null) {
return false;
}
int firstIndex = pattern.indexOf('*');
if (firstIndex == -1) {
return pattern.equals(str);
}
if (firstIndex == 0) {
if (pattern.length() == 1) {
return true;
}
int nextIndex = pattern.indexOf('*', 1);
if (nextIndex == -1) {
return str.endsWith(pattern.substring(1));
}
String part = pattern.substring(1, nextIndex);
if (part.isEmpty()) {
return simpleMatch(pattern.substring(nextIndex), str);
}
int partIndex = str.indexOf(part);
while (partIndex != -1) {
if (simpleMatch(pattern.substring(nextIndex), str.substring(partIndex + part.length()))) {
return true;
}
partIndex = str.indexOf(part, partIndex + 1);
}
return false;
}
return (str.length() >= firstIndex &&
pattern.startsWith(str.substring(0, firstIndex)) &&
simpleMatch(pattern.substring(firstIndex), str.substring(firstIndex)));
}
/**
* Match a String against the given patterns, supporting the following simple
* pattern styles: "xxx*", "*xxx", "*xxx*" and "xxx*yyy" matches (with an
* arbitrary number of pattern parts), as well as direct equality.
* @param patterns the patterns to match against
* @param str the String to match
* @return whether the String matches any of the given patterns
*/
public static boolean simpleMatch(@Nullable String[] patterns, String str) {
if (patterns != null) {
for (String pattern : patterns) {
if (simpleMatch(pattern, str)) {
return true;
}
}
}
return false;
}
}
사용
사용법은 간단하다.
private static final String[] PATTERNS = {"request*", "order*", "save*"};
String methodName = method.getName();
if (!PatternMatchUtils.simpleMatch(patterns, methodName)) {
// 구현
}
728x90
'Programming Languages > Java' 카테고리의 다른 글
자바 버전별 역사 및 특징 (0) | 2022.01.12 |
---|---|
다양한 자바 플랫폼(SE/EE/ME) (0) | 2022.01.11 |
JVM Archtiecture (0) | 2021.12.24 |
자바 프로그램 실행 과정 (0) | 2021.12.24 |
Checked, Unchecked Exception (0) | 2021.12.09 |