Skip to content

Commit

Permalink
improve *ExtensionStorage.read methods, remove comments and whitespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
decebals committed Jan 4, 2016
1 parent 6a666aa commit 342825b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

/**
* @author Decebal Suiu
Expand All @@ -34,6 +35,9 @@ public class LegacyExtensionStorage extends ExtensionStorage {

public static final String EXTENSIONS_RESOURCE = "META-INF/extensions.idx";

private static final Pattern COMMENT = Pattern.compile("#.*");
private static final Pattern WHITESPACE = Pattern.compile("\\s+");

public LegacyExtensionStorage(ExtensionAnnotationProcessor processor) {
super(processor);
}
Expand All @@ -43,11 +47,16 @@ public static void read(Reader reader, Set<String> entries) throws IOException {

String line;
while ((line = bufferedReader.readLine()) != null) {
entries.add(line);
line = COMMENT.matcher(line).replaceFirst("");
line = WHITESPACE.matcher(line).replaceAll("");
if (line.length() > 0) {
entries.add(line);
}
}

bufferedReader.close();
}

@Override
public Map<String, Set<String>> read() {
Map<String, Set<String>> extensions = new HashMap<>();
Expand All @@ -73,6 +82,7 @@ public void write(Map<String, Set<String>> extensions) {
FileObject file = getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", EXTENSIONS_RESOURCE);
BufferedWriter writer = new BufferedWriter(file.openWriter());
writer.write("# Generated by PF4J"); // write header
writer.newLine();
for (Map.Entry<String, Set<String>> entry : extensions.entrySet()) {
for (String extension : entry.getValue()) {
writer.write(extension);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

/**
* @author Decebal Suiu
Expand All @@ -34,6 +35,9 @@ public class ServiceProviderExtensionStorage extends ExtensionStorage {

public static final String EXTENSIONS_RESOURCE = "META-INF/services";

private static final Pattern COMMENT = Pattern.compile("#.*");
private static final Pattern WHITESPACE = Pattern.compile("\\s+");

public ServiceProviderExtensionStorage(ExtensionAnnotationProcessor processor) {
super(processor);
}
Expand All @@ -43,7 +47,11 @@ public static void read(Reader reader, Set<String> entries) throws IOException {

String line;
while ((line = bufferedReader.readLine()) != null) {
entries.add(line);
line = COMMENT.matcher(line).replaceFirst("");
line = WHITESPACE.matcher(line).replaceAll("");
if (line.length() > 0) {
entries.add(line);
}
}

bufferedReader.close();
Expand Down Expand Up @@ -78,10 +86,15 @@ public void write(Map<String, Set<String>> extensions) {
FileObject file = getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", EXTENSIONS_RESOURCE
+ "/" + extensionPoint);
BufferedWriter writer = new BufferedWriter(file.openWriter());
// write header
writer.write("# Generated by PF4J"); // write header
writer.newLine();
// write extensions
for (String extension : entry.getValue()) {
if (processor.getOldExtensions().containsKey(extensionPoint))
writer.write(extension);
if (!isExtensionOld(extensionPoint, extension)) {
writer.write(" # pf4j extension");
}
writer.newLine();
}
writer.close();
Expand All @@ -93,4 +106,9 @@ public void write(Map<String, Set<String>> extensions) {
}
}

private boolean isExtensionOld(String extensionPoint, String extension) {
return processor.getOldExtensions().containsKey(extensionPoint)
&& processor.getOldExtensions().get(extensionPoint).contains(extension);
}

}

0 comments on commit 342825b

Please sign in to comment.