Skip to content

Commit 6d11122

Browse files
committed
Replace anonymous classes with lambdas
1 parent 0397aa7 commit 6d11122

9 files changed

Lines changed: 68 additions & 152 deletions

File tree

commonmark-ext-gfm-alerts/src/main/java/org/commonmark/ext/gfm/alerts/AlertsExtension.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,7 @@ public void extend(Parser.Builder parserBuilder) {
5555

5656
@Override
5757
public void extend(HtmlRenderer.Builder rendererBuilder) {
58-
rendererBuilder.nodeRendererFactory(new HtmlNodeRendererFactory() {
59-
@Override
60-
public NodeRenderer create(HtmlNodeRendererContext context) {
61-
return new AlertHtmlNodeRenderer(context, customTypes);
62-
}
63-
});
58+
rendererBuilder.nodeRendererFactory(context -> new AlertHtmlNodeRenderer(context, customTypes));
6459
}
6560

6661
@Override

commonmark-ext-gfm-tables/src/test/java/org/commonmark/ext/gfm/tables/TablesTest.java

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -718,25 +718,17 @@ public void interruptsParagraph() {
718718

719719
@Test
720720
public void attributeProviderIsApplied() {
721-
AttributeProviderFactory factory = new AttributeProviderFactory() {
722-
@Override
723-
public AttributeProvider create(AttributeProviderContext context) {
724-
return new AttributeProvider() {
725-
@Override
726-
public void setAttributes(Node node, String tagName, Map<String, String> attributes) {
727-
if (node instanceof TableBlock) {
728-
attributes.put("test", "block");
729-
} else if (node instanceof TableHead) {
730-
attributes.put("test", "head");
731-
} else if (node instanceof TableBody) {
732-
attributes.put("test", "body");
733-
} else if (node instanceof TableRow) {
734-
attributes.put("test", "row");
735-
} else if (node instanceof TableCell) {
736-
attributes.put("test", "cell");
737-
}
738-
}
739-
};
721+
AttributeProviderFactory factory = context -> (node, tagName, attributes) -> {
722+
if (node instanceof TableBlock) {
723+
attributes.put("test", "block");
724+
} else if (node instanceof TableHead) {
725+
attributes.put("test", "head");
726+
} else if (node instanceof TableBody) {
727+
attributes.put("test", "body");
728+
} else if (node instanceof TableRow) {
729+
attributes.put("test", "row");
730+
} else if (node instanceof TableCell) {
731+
attributes.put("test", "cell");
740732
}
741733
};
742734
HtmlRenderer renderer = HtmlRenderer.builder()
@@ -762,17 +754,9 @@ public void setAttributes(Node node, String tagName, Map<String, String> attribu
762754

763755
@Test
764756
public void columnWidthIsRecorded() {
765-
AttributeProviderFactory factory = new AttributeProviderFactory() {
766-
@Override
767-
public AttributeProvider create(AttributeProviderContext context) {
768-
return new AttributeProvider() {
769-
@Override
770-
public void setAttributes(Node node, String tagName, Map<String, String> attributes) {
771-
if (node instanceof TableCell && "th".equals(tagName)) {
772-
attributes.put("width", ((TableCell) node).getWidth() + "em");
773-
}
774-
}
775-
};
757+
AttributeProviderFactory factory = context -> (node, tagName, attributes) -> {
758+
if (node instanceof TableCell && "th".equals(tagName)) {
759+
attributes.put("width", ((TableCell) node).getWidth() + "em");
776760
}
777761
};
778762
HtmlRenderer renderer = HtmlRenderer.builder()

commonmark-ext-heading-anchor/src/main/java/org/commonmark/ext/heading/anchor/HeadingAnchorExtension.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,7 @@ public static Builder builder() {
5555

5656
@Override
5757
public void extend(HtmlRenderer.Builder rendererBuilder) {
58-
rendererBuilder.attributeProviderFactory(new AttributeProviderFactory() {
59-
@Override
60-
public AttributeProvider create(AttributeProviderContext context) {
61-
return HeadingIdAttributeProvider.create(defaultId, idPrefix, idSuffix);
62-
}
63-
});
58+
rendererBuilder.attributeProviderFactory(context -> HeadingIdAttributeProvider.create(defaultId, idPrefix, idSuffix));
6459
}
6560

6661
public static class Builder {

commonmark-ext-image-attributes/src/main/java/org/commonmark/ext/image/attributes/ImageAttributesExtension.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ public void extend(Parser.Builder parserBuilder) {
3535

3636
@Override
3737
public void extend(HtmlRenderer.Builder rendererBuilder) {
38-
rendererBuilder.attributeProviderFactory(new AttributeProviderFactory() {
39-
@Override
40-
public AttributeProvider create(AttributeProviderContext context) {
41-
return ImageAttributesAttributeProvider.create();
42-
}
43-
});
38+
rendererBuilder.attributeProviderFactory(context -> ImageAttributesAttributeProvider.create());
4439
}
4540
}

commonmark-integration-test/src/test/java/org/commonmark/ui/DingusApp.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,7 @@ public void changedUpdate(DocumentEvent e) {
7979
tabbedPane.addTab("HTML source", htmlSourceRendererOutput);
8080
tabbedPane.addTab("Plain text", textRendererOutput);
8181

82-
tabbedPane.addChangeListener(new ChangeListener() {
83-
@Override
84-
public void stateChanged(ChangeEvent e) {
85-
updateOutput(input.getText());
86-
}
87-
});
82+
tabbedPane.addChangeListener(e -> updateOutput(input.getText()));
8883

8984
input.setText("# Example\n" +
9085
"Enter text *here* and see how it renders on the right.\n\n" +

commonmark/src/main/java/org/commonmark/internal/util/Escaping.java

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,36 +25,30 @@ public class Escaping {
2525

2626
private static final Pattern WHITESPACE = Pattern.compile("[ \t\r\n]+");
2727

28-
private static final Replacer UNESCAPE_REPLACER = new Replacer() {
29-
@Override
30-
public void replace(String input, StringBuilder sb) {
31-
if (input.charAt(0) == '\\') {
32-
sb.append(input, 1, input.length());
33-
} else {
34-
sb.append(Html5Entities.entityToString(input));
35-
}
28+
private static final Replacer UNESCAPE_REPLACER = (input, sb) -> {
29+
if (input.charAt(0) == '\\') {
30+
sb.append(input, 1, input.length());
31+
} else {
32+
sb.append(Html5Entities.entityToString(input));
3633
}
3734
};
3835

39-
private static final Replacer URI_REPLACER = new Replacer() {
40-
@Override
41-
public void replace(String input, StringBuilder sb) {
42-
if (input.startsWith("%")) {
43-
if (input.length() == 3) {
44-
// Already percent-encoded, preserve
45-
sb.append(input);
46-
} else {
47-
// %25 is the percent-encoding for %
48-
sb.append("%25");
49-
sb.append(input, 1, input.length());
50-
}
36+
private static final Replacer URI_REPLACER = (input, sb) -> {
37+
if (input.startsWith("%")) {
38+
if (input.length() == 3) {
39+
// Already percent-encoded, preserve
40+
sb.append(input);
5141
} else {
52-
byte[] bytes = input.getBytes(StandardCharsets.UTF_8);
53-
for (byte b : bytes) {
54-
sb.append('%');
55-
sb.append(HEX_DIGITS[(b >> 4) & 0xF]);
56-
sb.append(HEX_DIGITS[b & 0xF]);
57-
}
42+
// %25 is the percent-encoding for %
43+
sb.append("%25");
44+
sb.append(input, 1, input.length());
45+
}
46+
} else {
47+
byte[] bytes = input.getBytes(StandardCharsets.UTF_8);
48+
for (byte b : bytes) {
49+
sb.append('%');
50+
sb.append(HEX_DIGITS[(b >> 4) & 0xF]);
51+
sb.append(HEX_DIGITS[b & 0xF]);
5852
}
5953
}
6054
};

commonmark/src/test/java/org/commonmark/test/HtmlRendererTest.java

Lines changed: 27 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -162,23 +162,15 @@ public void percentEncodeUrl() {
162162

163163
@Test
164164
public void attributeProviderForCodeBlock() {
165-
AttributeProviderFactory custom = new AttributeProviderFactory() {
166-
@Override
167-
public AttributeProvider create(AttributeProviderContext context) {
168-
return new AttributeProvider() {
169-
@Override
170-
public void setAttributes(Node node, String tagName, Map<String, String> attributes) {
171-
if (node instanceof FencedCodeBlock && tagName.equals("code")) {
172-
FencedCodeBlock fencedCodeBlock = (FencedCodeBlock) node;
173-
// Remove the default attribute for info
174-
attributes.remove("class");
175-
// Put info in custom attribute instead
176-
attributes.put("data-custom", fencedCodeBlock.getInfo());
177-
} else if (node instanceof FencedCodeBlock && tagName.equals("pre")) {
178-
attributes.put("data-code-block", "fenced");
179-
}
180-
}
181-
};
165+
AttributeProviderFactory custom = context -> (node, tagName, attributes) -> {
166+
if (node instanceof FencedCodeBlock && tagName.equals("code")) {
167+
FencedCodeBlock fencedCodeBlock = (FencedCodeBlock) node;
168+
// Remove the default attribute for info
169+
attributes.remove("class");
170+
// Put info in custom attribute instead
171+
attributes.put("data-custom", fencedCodeBlock.getInfo());
172+
} else if (node instanceof FencedCodeBlock && tagName.equals("pre")) {
173+
attributes.put("data-code-block", "fenced");
182174
}
183175
};
184176

@@ -192,18 +184,10 @@ public void setAttributes(Node node, String tagName, Map<String, String> attribu
192184

193185
@Test
194186
public void attributeProviderForImage() {
195-
AttributeProviderFactory custom = new AttributeProviderFactory() {
196-
@Override
197-
public AttributeProvider create(AttributeProviderContext context) {
198-
return new AttributeProvider() {
199-
@Override
200-
public void setAttributes(Node node, String tagName, Map<String, String> attributes) {
201-
if (node instanceof Image) {
202-
attributes.remove("alt");
203-
attributes.put("test", "hey");
204-
}
205-
}
206-
};
187+
AttributeProviderFactory custom = context -> (node, tagName, attributes) -> {
188+
if (node instanceof Image) {
189+
attributes.remove("alt");
190+
attributes.put("test", "hey");
207191
}
208192
};
209193

@@ -214,18 +198,13 @@ public void setAttributes(Node node, String tagName, Map<String, String> attribu
214198

215199
@Test
216200
public void attributeProviderFactoryNewInstanceForEachRender() {
217-
AttributeProviderFactory factory = new AttributeProviderFactory() {
201+
AttributeProviderFactory factory = context -> new AttributeProvider() {
202+
int i = 0;
203+
218204
@Override
219-
public AttributeProvider create(AttributeProviderContext context) {
220-
return new AttributeProvider() {
221-
int i = 0;
222-
223-
@Override
224-
public void setAttributes(Node node, String tagName, Map<String, String> attributes) {
225-
attributes.put("key", "" + i);
226-
i++;
227-
}
228-
};
205+
public void setAttributes(Node node, String tagName, Map<String, String> attributes) {
206+
attributes.put("key", "" + i);
207+
i++;
229208
}
230209
};
231210

@@ -237,20 +216,15 @@ public void setAttributes(Node node, String tagName, Map<String, String> attribu
237216

238217
@Test
239218
public void overrideNodeRender() {
240-
HtmlNodeRendererFactory nodeRendererFactory = new HtmlNodeRendererFactory() {
219+
HtmlNodeRendererFactory nodeRendererFactory = context -> new NodeRenderer() {
220+
@Override
221+
public Set<Class<? extends Node>> getNodeTypes() {
222+
return Set.of(Link.class);
223+
}
224+
241225
@Override
242-
public NodeRenderer create(final HtmlNodeRendererContext context) {
243-
return new NodeRenderer() {
244-
@Override
245-
public Set<Class<? extends Node>> getNodeTypes() {
246-
return Set.of(Link.class);
247-
}
248-
249-
@Override
250-
public void render(Node node) {
251-
context.getWriter().text("test");
252-
}
253-
};
226+
public void render(Node node) {
227+
context.getWriter().text("test");
254228
}
255229
};
256230

commonmark/src/test/java/org/commonmark/test/ParserTest.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,9 @@ public void indentation() {
9090

9191
@Test
9292
public void inlineParser() {
93-
final InlineParser fakeInlineParser = new InlineParser() {
94-
@Override
95-
public void parse(SourceLines lines, Node node) {
96-
node.appendChild(new ThematicBreak());
97-
}
98-
};
99-
100-
InlineParserFactory fakeInlineParserFactory = new InlineParserFactory() {
93+
final InlineParser fakeInlineParser = (lines, node) -> node.appendChild(new ThematicBreak());
10194

102-
@Override
103-
public InlineParser create(InlineParserContext inlineParserContext) {
104-
return fakeInlineParser;
105-
}
106-
};
95+
InlineParserFactory fakeInlineParserFactory = inlineParserContext -> fakeInlineParser;
10796

10897
Parser parser = Parser.builder().inlineParserFactory(fakeInlineParserFactory).build();
10998
String input = "**bold** **bold** ~~strikethrough~~";

commonmark/src/test/java/org/commonmark/test/UsageExampleTest.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,7 @@ public void sourcePositions() {
7878
public void addAttributes() {
7979
Parser parser = Parser.builder().build();
8080
HtmlRenderer renderer = HtmlRenderer.builder()
81-
.attributeProviderFactory(new AttributeProviderFactory() {
82-
@Override
83-
public AttributeProvider create(AttributeProviderContext context) {
84-
return new ImageAttributeProvider();
85-
}
86-
})
81+
.attributeProviderFactory(context -> new ImageAttributeProvider())
8782
.build();
8883

8984
Node document = parser.parse("![text](/url.png)");

0 commit comments

Comments
 (0)