Skip to content

Commit

Permalink
action: Improve resource name matching
Browse files Browse the repository at this point in the history
We clean up the abstraction to use a predicate which allows us to
capture extensions and their length without having to recompute.
We can now support multiple extensions for an action type.

Signed-off-by: BJ Hargrave <hargrave@us.ibm.com>
  • Loading branch information
bjhargrave committed Jul 1, 2022
1 parent e9e24bc commit b936980
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@

package org.eclipse.transformer.action;

import java.util.Arrays;
import java.util.function.Predicate;

public enum ActionType {
RENAME("Rename Action", ""),
RENAME("Rename Action"),

CLASS("Class Action", ".class"),
MANIFEST("Manifest Action", "manifest.mf"),
FEATURE("Feature Action", ".mf"), // Sub of MANIFEST
SERVICE_LOADER_CONFIG("Service Config Action", ""),
SERVICE_LOADER_CONFIG("Service Config Action"),
PROPERTIES("Properties Action", ".properties"), // Sub of TEXT

TEXT("Text Action", ""),
TEXT("Text Action"),
JAVA("Java Action", ".java"), // Sub of TEXT
JSP("JSP Action", ".jsp"), // Sub of TEXT
XML("XML Action", ".xml"), // Sub of TEXT
Expand All @@ -31,18 +34,30 @@ public enum ActionType {
RAR("RAR Action", ".rar"),
EAR("EAR Action", ".ear"),

DIRECTORY("Directory Action", "");
DIRECTORY("Directory Action");

private final String name;
private final String extension;
private final Predicate<String> matcher;

ActionType(String name, String extension) {
ActionType(String name, String... extensions) {
this.name = name;
this.extension = extension;
this.matcher = Arrays.stream(extensions)
.map(this::extensionPredicate)
.reduce(Predicate::or)
.orElse(this::matchingUnsupported);
}

private Predicate<String> extensionPredicate(String extension) {
int length = extension.length();
return resourceName -> resourceName.regionMatches(true, resourceName.length() - length, extension, 0, length);
}

private boolean matchingUnsupported(String resourceName) {
throw new UnsupportedOperationException(getName().concat(" does not support resource name matching"));
}

public String getExtension() {
return extension;
public Predicate<String> resourceNameMatcher() {
return matcher;
}

public String getName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,21 +184,8 @@ public boolean acceptResource(String resourceName, File resourceFile) {
throw new UnsupportedOperationException(getClass().getSimpleName() + " does not support this method");
}

protected boolean acceptExtension(String resourceName, File resourceFile) {
String ext = getAcceptExtension();
int extLen = ext.length();
int resLen = resourceName.length();

boolean accept = ((resLen >= extLen) && resourceName.regionMatches(true, resLen - extLen, ext, 0, extLen));
return accept;
}

public String getAcceptExtension() {
String acceptExtension = getActionType().getExtension();
if (acceptExtension.isEmpty()) {
throw new UnsupportedOperationException(getClass().getSimpleName() + " does not support this method");
}
return acceptExtension;
protected boolean matchResourceName(String resourceName) {
return getActionType().resourceNameMatcher().test(resourceName);
}

//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void addReplacements(int additions) {

@Override
public boolean acceptResource(String resourceName, File resourceFile) {
return acceptExtension(resourceName, resourceFile);
return matchResourceName(resourceName);
}

// Entry from the transformer, and from the directory action.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ public ActionType getActionType() {

@Override
public boolean acceptResource(String resourceName, File resourceFile) {
return acceptExtension(resourceName, resourceFile);
return matchResourceName(resourceName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ public ActionType getActionType() {

@Override
public boolean acceptResource(String resourceName, File resourceFile) {
return acceptExtension(resourceName, resourceFile);
return matchResourceName(resourceName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ public ActionType getActionType() {

@Override
public boolean acceptResource(String resourceName, File resourceFile) {
return acceptExtension(resourceName, resourceFile);
return matchResourceName(resourceName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ public ActionType getActionType() {

@Override
public boolean acceptResource(String resourceName, File resourceFile) {
return acceptExtension(resourceName, resourceFile);
return matchResourceName(resourceName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public boolean isArchiveAction() {

@Override
public boolean acceptResource(String resourceName, File resourceFile) {
return acceptExtension(resourceName, resourceFile);
return matchResourceName(resourceName);
}

// Entry from the transformer, or, from the directory action.
Expand Down

0 comments on commit b936980

Please sign in to comment.