Skip to content

Commit d294115

Browse files
xuwei-kjknack
authored andcommitted
use Java 8 lambda (#632)
1 parent 2831f12 commit d294115

File tree

8 files changed

+27
-81
lines changed

8 files changed

+27
-81
lines changed

handlebars-guava-cache/src/main/java/com/github/jknack/handlebars/cache/GuavaTemplateCache.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import static org.apache.commons.lang3.Validate.notNull;
2121

2222
import java.io.IOException;
23-
import java.util.concurrent.Callable;
2423
import java.util.concurrent.ExecutionException;
2524

2625
import com.github.jknack.handlebars.HandlebarsException;
@@ -72,12 +71,7 @@ public Template get(final TemplateSource source, final Parser parser) throws IOE
7271
notNull(source, "The source is required.");
7372
notNull(parser, "The parser is required.");
7473
try {
75-
return cache.get(key(source), new Callable<Template>() {
76-
@Override
77-
public Template call() throws IOException {
78-
return parser.parse(source);
79-
}
80-
});
74+
return cache.get(key(source), () -> parser.parse(source));
8175
} catch (ExecutionException ex) {
8276
throw launderThrowable(source, ex.getCause());
8377
}

handlebars-guava-cache/src/main/java/com/github/jknack/handlebars/io/GuavaCachedTemplateLoader.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import java.io.IOException;
2121
import java.nio.charset.Charset;
22-
import java.util.concurrent.Callable;
2322
import java.util.concurrent.ExecutionException;
2423
import java.util.concurrent.TimeUnit;
2524

@@ -79,12 +78,7 @@ public static GuavaCachedTemplateLoader cacheWithExpiration(
7978
*/
8079
public TemplateSource sourceAt(final String location) throws IOException {
8180
try {
82-
return cache.get(location, new Callable<TemplateSource>() {
83-
@Override
84-
public TemplateSource call() throws Exception {
85-
return delegate.sourceAt(location);
86-
}
87-
});
81+
return cache.get(location, () -> delegate.sourceAt(location));
8882
} catch (ExecutionException e) {
8983
Throwables.propagateIfPossible(e.getCause(), IOException.class);
9084
throw Throwables.propagate(e.getCause());

handlebars-maven-plugin/src/main/java/com/github/jknack/handlebars/maven/PrecompilePlugin.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,7 @@ protected void doExecute() throws Exception {
267267
* @param handlebars The handlebars object.
268268
*/
269269
private void silentHelpers(final Handlebars handlebars) {
270-
handlebars.registerHelper(HelperRegistry.HELPER_MISSING, new Helper<Object>() {
271-
@Override
272-
public Object apply(final Object context, final Options options) throws IOException {
273-
return null;
274-
}
275-
});
270+
handlebars.registerHelper(HelperRegistry.HELPER_MISSING, (context, options) -> null);
276271
}
277272

278273
/**

handlebars-proto/src/main/java/com/github/jknack/handlebars/server/HbsServer.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,10 @@ public static void run(final Options args) throws Exception {
164164
* Helper wont work in the stand-alone version, so we add a default helper
165165
* that render the plain text.
166166
*/
167-
handlebars.registerHelper(HelperRegistry.HELPER_MISSING, new Helper<Object>() {
168-
@Override
169-
public Object apply(final Object context,
170-
final com.github.jknack.handlebars.Options options)
171-
throws IOException {
172-
return new Handlebars.SafeString(options.fn.text());
173-
}
174-
});
167+
handlebars.registerHelper(
168+
HelperRegistry.HELPER_MISSING,
169+
(context, options) -> new Handlebars.SafeString(options.fn.text())
170+
);
175171
handlebars.registerHelper("json", Jackson2Helper.INSTANCE);
176172
handlebars.registerHelper("md", new MarkdownHelper());
177173
// String helpers
@@ -180,15 +176,12 @@ public Object apply(final Object context,
180176
HumanizeHelper.register(handlebars);
181177

182178
final Server server = new Server(args.port);
183-
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
184-
@Override
185-
public void run() {
186-
logger.info("Hope you enjoy it! bye!");
187-
try {
188-
server.stop();
189-
} catch (Exception ex) {
190-
logger.info("Can't stop the server", ex);
191-
}
179+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
180+
logger.info("Hope you enjoy it! bye!");
181+
try {
182+
server.stop();
183+
} catch (Exception ex) {
184+
logger.info("Can't stop the server", ex);
192185
}
193186
}));
194187

handlebars/src/main/java/com/github/jknack/handlebars/EscapingStrategy.java

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -105,36 +105,19 @@ public CharSequence escape(final CharSequence value) {
105105
EscapingStrategy HBS4 = HTML_ENTITY;
106106

107107
/** Escape variable for CSV. */
108-
EscapingStrategy CSV = new EscapingStrategy() {
109-
@Override
110-
public CharSequence escape(final CharSequence value) {
111-
return value == null ? null : StringEscapeUtils.escapeCsv(value.toString());
112-
}
113-
};
108+
EscapingStrategy CSV = value ->
109+
value == null ? null : StringEscapeUtils.escapeCsv(value.toString());
114110

115111
/** Escape variable for XML. */
116-
EscapingStrategy XML = new EscapingStrategy() {
117-
@Override
118-
public CharSequence escape(final CharSequence value) {
119-
return value == null ? null : StringEscapeUtils.escapeXml(value.toString());
120-
}
121-
};
112+
EscapingStrategy XML = value ->
113+
value == null ? null : StringEscapeUtils.escapeXml(value.toString());
122114

123115
/** Escape variable for JavaScript. */
124-
EscapingStrategy JS = new EscapingStrategy() {
125-
@Override
126-
public CharSequence escape(final CharSequence value) {
127-
return value == null ? null : StringEscapeUtils.escapeEcmaScript(value.toString());
128-
}
129-
};
116+
EscapingStrategy JS = value ->
117+
value == null ? null : StringEscapeUtils.escapeEcmaScript(value.toString());
130118

131119
/** NOOP escaping. */
132-
EscapingStrategy NOOP = new EscapingStrategy() {
133-
@Override
134-
public CharSequence escape(final CharSequence value) {
135-
return value;
136-
}
137-
};
120+
EscapingStrategy NOOP = value -> value;
138121

139122
/** Default escaping strategy. */
140123
EscapingStrategy DEF = HBS4;

handlebars/src/main/java/com/github/jknack/handlebars/Formatter.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,7 @@ interface Chain {
6969
*
7070
* @author edgar
7171
*/
72-
Formatter.Chain NOOP = new Formatter.Chain() {
73-
@Override
74-
public Object format(final Object value) {
75-
return value;
76-
}
77-
};
72+
Formatter.Chain NOOP = value -> value;
7873

7974
/**
8075
* Format a value if possible or call next formatter in the chain.

handlebars/src/main/java/com/github/jknack/handlebars/Handlebars.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,14 +1437,12 @@ public Charset getCharset() {
14371437
* @return Composite escape strategy.
14381438
*/
14391439
private static EscapingStrategy newEscapeChain(final EscapingStrategy[] chain) {
1440-
return new EscapingStrategy() {
1441-
@Override public CharSequence escape(final CharSequence value) {
1442-
CharSequence result = value;
1443-
for (EscapingStrategy escape : chain) {
1444-
result = escape.escape(result);
1445-
}
1446-
return result;
1440+
return value -> {
1441+
CharSequence result = value;
1442+
for (EscapingStrategy escape : chain) {
1443+
result = escape.escape(result);
14471444
}
1445+
return result;
14481446
};
14491447
}
14501448

handlebars/src/main/java/com/github/jknack/handlebars/cache/HighConcurrencyTemplateCache.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import static org.apache.commons.lang3.Validate.notNull;
2121

2222
import java.io.IOException;
23-
import java.util.concurrent.Callable;
2423
import java.util.concurrent.CancellationException;
2524
import java.util.concurrent.ConcurrentHashMap;
2625
import java.util.concurrent.ConcurrentMap;
@@ -162,12 +161,7 @@ private Template cacheGet(final TemplateSource source, final Parser parser) thro
162161
private FutureTask<Pair<TemplateSource, Template>> newTask(final TemplateSource source,
163162
final Parser parser) {
164163
return new FutureTask<>(
165-
new Callable<Pair<TemplateSource, Template>>() {
166-
@Override
167-
public Pair<TemplateSource, Template> call() throws IOException {
168-
return Pair.of(source, parser.parse(source));
169-
}
170-
});
164+
() -> Pair.of(source, parser.parse(source)));
171165
}
172166

173167
/**

0 commit comments

Comments
 (0)