Skip to content

Commit

Permalink
Make CodeWriters intercept Object instead of String
Browse files Browse the repository at this point in the history
This commit makes CodeWriter section interceptors deal with Object
instead of String. This allows for the toString() method of the
intercepted text to be wrapped by classes that extend from CodeWriter in
order to determine if any side effects from the original content should
take effect or be discarded. The general idea is that if toString() was
called on the original content of an intercepted section, then original
contents are also going to be written to the CodeWriter, meaning any
side effects should also occur (for example, adding imports).
  • Loading branch information
mtdowling committed Oct 10, 2019
1 parent a16e608 commit 2b47ab6
Showing 1 changed file with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@
*
* <pre>{@code
* CodeWriter writer = CodeWriter.createDefault();
* writer.onSection("example", text -> writer.write("Intercepted: " + text"));
* writer.onSection("example", text -> writer.write("Intercepted: " + text));
* writer.pushState("example");
* writer.write("Original contents");
* writer.popState();
Expand All @@ -326,7 +326,7 @@
*
* <pre>{@code
* CodeWriter writer = CodeWriter.createDefault();
* writer.onSection("example", text -> writer.write("Intercepted: " + text"));
* writer.onSection("example", text -> writer.write("Intercepted: " + text));
* writer.write("Leading text...${L@example}...Trailing text...", "foo");
* System.out.println(writer.toString());
* // Outputs: "Leading text...Intercepted: foo...Trailing text...\n"
Expand Down Expand Up @@ -535,8 +535,8 @@ public CodeWriter popState() {
String result = getTrimmedPoppedStateContents(popped);

if (popped.interceptors.containsKey(popped.sectionName)) {
List<Consumer<String>> interceptors = popped.interceptors.get(popped.sectionName);
for (Consumer<String> interceptor : interceptors) {
List<Consumer<Object>> interceptors = popped.interceptors.get(popped.sectionName);
for (Consumer<Object> interceptor : interceptors) {
result = expandSection("__" + popped.sectionName, result, interceptor);
}
}
Expand Down Expand Up @@ -602,7 +602,7 @@ private String getTrimmedPoppedStateContents(State state) {
* @param interceptor The function to intercept with.
* @return Returns the CodeWriter.
*/
public CodeWriter onSection(String sectionName, Consumer<String> interceptor) {
public CodeWriter onSection(String sectionName, Consumer<Object> interceptor) {
currentState.putInterceptor(sectionName, interceptor);
return this;
}
Expand Down Expand Up @@ -1032,7 +1032,7 @@ public Object getContext(String key) {
}

// Used only by CodeFormatter to expand inline argument sections.
String expandSection(String sectionName, String defaultContent, Consumer<String> writerConsumer) {
String expandSection(String sectionName, String defaultContent, Consumer<Object> writerConsumer) {
StringBuilder buffer = new StringBuilder();
pushState(sectionName);
currentState.isInline = true;
Expand Down Expand Up @@ -1065,7 +1065,7 @@ private final class State {
private boolean copiedContext = false;

/** The interceptors map implements a simple copy on write pattern. */
private Map<String, List<Consumer<String>>> interceptors = MapUtils.of();
private Map<String, List<Consumer<Object>>> interceptors = MapUtils.of();
private boolean copiedInterceptors = false;

State() {}
Expand Down Expand Up @@ -1105,7 +1105,7 @@ void removeContext(String key) {
}
}

void putInterceptor(String section, Consumer<String> interceptor) {
void putInterceptor(String section, Consumer<Object> interceptor) {
if (!copiedInterceptors) {
interceptors = new HashMap<>(interceptors);
copiedInterceptors = true;
Expand Down

0 comments on commit 2b47ab6

Please sign in to comment.