Uses factory method pattern

This commit is contained in:
Lukasz Lenart
2015-09-04 09:08:32 +02:00
parent e38d4657c6
commit cb06d7d258
2 changed files with 31 additions and 29 deletions
@@ -63,7 +63,7 @@ public class ActionConfig extends Located implements Serializable {
results = new LinkedHashMap<>();
interceptors = new ArrayList<>();
exceptionMappings = new ArrayList<>();
allowedMethods = new AllowedMethods(new HashSet<>(Collections.singletonList(DEFAULT_METHOD)));
allowedMethods = AllowedMethods.build(new HashSet<>(Collections.singletonList(DEFAULT_METHOD)));
}
/**
@@ -80,7 +80,7 @@ public class ActionConfig extends Located implements Serializable {
this.interceptors = new ArrayList<>(orig.interceptors);
this.results = new LinkedHashMap<>(orig.results);
this.exceptionMappings = new ArrayList<>(orig.exceptionMappings);
this.allowedMethods = new AllowedMethods(orig.allowedMethods.list());
this.allowedMethods = AllowedMethods.build(orig.allowedMethods.list());
this.location = orig.location;
}
@@ -332,7 +332,7 @@ public class ActionConfig extends Located implements Serializable {
target.results = Collections.unmodifiableMap(target.results);
target.interceptors = Collections.unmodifiableList(target.interceptors);
target.exceptionMappings = Collections.unmodifiableList(target.exceptionMappings);
target.allowedMethods = new AllowedMethods(allowedMethods);
target.allowedMethods = AllowedMethods.build(allowedMethods);
ActionConfig result = target;
target = new ActionConfig(target);
@@ -9,37 +9,39 @@ public class AllowedMethods {
private Set<AllowedMethod> allowedMethods;
public AllowedMethods(Set<String> methods) {
public static AllowedMethods build(Set<String> methods) {
Set<AllowedMethod> allowedMethods = new HashSet<>();
for (String method : methods) {
allowedMethods.add(build(method));
}
this.allowedMethods = Collections.unmodifiableSet(allowedMethods);
}
private AllowedMethod build(String method) {
boolean isPattern = false;
int len = method.length();
StringBuilder ret = new StringBuilder();
char c;
for (int x = 0; x < len; x++) {
c = method.charAt(x);
if (x < len - 2 && c == '{' && '}' == method.charAt(x + 2)) {
ret.append("(.*)");
isPattern = true;
x += 2;
boolean isPattern = false;
int len = method.length();
StringBuilder ret = new StringBuilder();
char c;
for (int x = 0; x < len; x++) {
c = method.charAt(x);
if (x < len - 2 && c == '{' && '}' == method.charAt(x + 2)) {
ret.append("(.*)");
isPattern = true;
x += 2;
} else {
ret.append(c);
}
}
if (isPattern && !method.startsWith("regex:")) {
allowedMethods.add(new PatternAllowedMethod(ret.toString(), method));
} else if (method.startsWith("regex:")) {
String pattern = method.substring(method.indexOf(":") + 1);
allowedMethods.add(new PatternAllowedMethod(pattern, method));
} else {
ret.append(c);
allowedMethods.add(new LiteralAllowedMethod(ret.toString()));
}
}
if (isPattern && !method.startsWith("regex:")) {
return new PatternAllowedMethod(ret.toString(), method);
} else if (method.startsWith("regex:")) {
String pattern = method.substring(method.indexOf(":") + 1);
return new PatternAllowedMethod(pattern, method);
} else {
return new LiteralAllowedMethod(ret.toString());
}
return new AllowedMethods(allowedMethods);
}
private AllowedMethods(Set<AllowedMethod> methods) {
this.allowedMethods = Collections.unmodifiableSet(methods);
}
public boolean isAllowed(String method) {