测试用例:
package com.itheima.sh.test;
public abstract class PatternMatchUtils {
public static boolean simpleMatch(String pattern, 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('*', firstIndex + 1);
if (nextIndex == -1) {
return str.endsWith(pattern.substring(1));
}
String part = pattern.substring(1, nextIndex);
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.substring(0, firstIndex).equals(str.substring(0, firstIndex)) &&
simpleMatch(pattern.substring(firstIndex), str.substring(firstIndex)));
}
public static boolean simpleMatch(String[] patterns, String str) {
if (patterns != null) {
for (int i = 0; i < patterns.length; i++) {
if (simpleMatch(patterns[i], str)) {
return true;
}
}
}
return false;
}
}
package com.itheima.sh.test;看到这里是否想起了Spring中的AOP:的切入点匹配表达式了,对的用的就是这个工具类;;
import org.junit.Test;
public class UtilsTest {
@Test
public void test(){
String[] patterns={"save*","update*","delete*"};
boolean simpleMatch = PatternMatchUtils.simpleMatch(patterns, "updateUser");
System.out.println(simpleMatch);
}
}
里面还有很多其他的工具类,这里截个图:
<script type="text/javascript" id="wumiiRelatedItems"> </script>