com.fastchar.core.FastInterceptors Maven / Gradle / Ivy
package com.fastchar.core;
import com.fastchar.interfaces.IFastInterceptor;
import com.fastchar.interfaces.IFastRootInterceptor;
import com.fastchar.utils.FastClassUtils;
import com.fastchar.utils.FastStringUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public final class FastInterceptors {
private List> rootInterceptors = new ArrayList<>();
private List> beforeInterceptors = new ArrayList<>();
private List> afterInterceptors = new ArrayList<>();
public FastInterceptors addRoot(Class interceptor,
String... urlPattern) {
return addRoot(interceptor, 0, urlPattern);
}
public FastInterceptors addRoot(Class interceptor,
int priority,
String... urlPattern) {
if (!FastClassUtils.checkNewInstance(interceptor)) {
return this;
}
for (String url : urlPattern) {
InterceptorInfo info = new InterceptorInfo<>();
info.interceptor = interceptor;
info.url = url;
info.priority = priority;
rootInterceptors.add(info);
}
return this;
}
public FastInterceptors addBefore(Class interceptor,
String... urlPattern) {
return addBefore(interceptor, 0,urlPattern);
}
public FastInterceptors addBefore(Class interceptor,
int priority,
String... urlPattern) {
if (!FastClassUtils.checkNewInstance(interceptor)) {
return this;
}
for (String url : urlPattern) {
InterceptorInfo info = new InterceptorInfo<>();
info.interceptor = interceptor;
info.url = url;
info.priority = priority;
beforeInterceptors.add(info);
}
return this;
}
public FastInterceptors addAfter(Class interceptor,
String... urlPattern) {
return addAfter(interceptor,0, urlPattern);
}
public FastInterceptors addAfter(Class interceptor,
int priority,
String... urlPattern) {
if (!FastClassUtils.checkNewInstance(interceptor)) {
return this;
}
for (String url : urlPattern) {
InterceptorInfo info = new InterceptorInfo<>();
info.interceptor = interceptor;
info.url = url;
info.priority = priority;
afterInterceptors.add(info);
}
return this;
}
void sortRootInterceptor() {
Comparator comparator = new Comparator() {
@Override
public int compare(InterceptorInfo o1, InterceptorInfo o2) {
if (o1.priority > o2.priority) {
return -1;
}
if (o1.priority < o2.priority) {
return 1;
}
return 0;
}
};
Collections.sort(rootInterceptors, comparator);
}
List> getRootInterceptors(String contentPath) {
ArrayList> interceptors = new ArrayList<>();
for (InterceptorInfo rootInterceptor : rootInterceptors) {
if (FastStringUtils.matches(rootInterceptor.getUrl(), contentPath)) {
interceptors.add(rootInterceptor.getInterceptor());
}
}
return interceptors;
}
List> getBeforeInterceptors(String url) {
List> interceptorInfos = new ArrayList<>();
for (InterceptorInfo beforeInterceptor : beforeInterceptors) {
if (FastStringUtils.matches(beforeInterceptor.getUrl(), url)) {
interceptorInfos.add(beforeInterceptor);
}
}
return interceptorInfos;
}
List> getAfterInterceptors(String url) {
List> interceptorInfos = new ArrayList<>();
for (InterceptorInfo afterInterceptor : afterInterceptors) {
if (FastStringUtils.matches(afterInterceptor.getUrl(), url)) {
interceptorInfos.add(afterInterceptor);
}
}
return interceptorInfos;
}
public static class InterceptorInfo {
private String url;
private Class interceptor;
private int priority;
public String getUrl() {
return url;
}
public InterceptorInfo setUrl(String url) {
this.url = url;
return this;
}
public Class getInterceptor() {
return interceptor;
}
public InterceptorInfo setInterceptor(Class interceptor) {
this.interceptor = interceptor;
return this;
}
public int getPriority() {
return priority;
}
public InterceptorInfo setPriority(int priority) {
this.priority = priority;
return this;
}
}
}