Skip to content

Commit

Permalink
refactor: a few usages of newer Collections APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospereira committed May 10, 2024
1 parent 56ee05e commit a367477
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -61,7 +61,7 @@ public void generateJavaFacades() {
// When
Collection<Path> generatedPaths = modelExtension.generate(
config(Paths.get("target/generated-test-sources"), TEST_PACKAGE),
Collections.singleton(templateDescription)
Set.of(templateDescription)
);

// Then
Expand Down Expand Up @@ -125,7 +125,7 @@ public void generatesKotlinFacades() {
// When
Collection<Path> generatedPaths = modelExtension.generate(
config(Paths.get("target/generated-test-sources"), TEST_PACKAGE),
Collections.singleton(templateDescription)
Set.of(templateDescription)
);

// Then
Expand Down Expand Up @@ -172,7 +172,7 @@ public void generatesKotlinFacadesWhenParamHasDefaultValue() {
// When
Collection<Path> generatedPaths = modelExtension.generate(
config(Paths.get("target/generated-test-sources"), TEST_PACKAGE),
Collections.singleton(templateDescription)
Set.of(templateDescription)
);

// Then
Expand Down Expand Up @@ -216,7 +216,7 @@ public void generatesKotlinWithoutDefaultValueForContentParams() {
// When
Collection<Path> generatedPaths = modelExtension.generate(
config(Paths.get("target/generated-test-sources"), TEST_PACKAGE),
Collections.singleton(templateDescription)
Set.of(templateDescription)
);

// Then
Expand Down Expand Up @@ -266,10 +266,10 @@ override fun hello(content: gg.jte.Content): JteModel = StaticJteModel<TemplateO
"DynamicTemplates.kt", withSystemLineEndings("""
@file:Suppress("ktlint")
package test.myktemplates
import gg.jte.TemplateEngine
import gg.jte.models.runtime.*
class DynamicTemplates(private val engine: TemplateEngine) : Templates {
\s
override fun hello(content: gg.jte.Content): JteModel {
Expand All @@ -281,7 +281,7 @@ override fun hello(content: gg.jte.Content): JteModel {
\s
return DynamicJteModel(engine, "hello.kte", paramMap)
}
}""")
);
assertThat(actual).containsExactlyInAnyOrderEntriesOf(expected);
Expand All @@ -299,7 +299,7 @@ public void generatesKotlinFacadesWithNoParameters() {
// When
Collection<Path> generatedPaths = modelExtension.generate(
config(Paths.get("target/generated-test-sources"), TEST_PACKAGE),
Collections.singleton(templateDescription)
Set.of(templateDescription)
);

// Then
Expand All @@ -317,14 +317,14 @@ public void generatesKotlinFacadesWithNoParameters() {
"Templates.kt", withSystemLineEndings("""
@file:Suppress("ktlint")
package test.myktemplates
import gg.jte.models.runtime.*
interface Templates {
\s
@JteView("hello.kte")
fun hello(): JteModel
}"""),
"StaticTemplates.kt", withSystemLineEndings("""
@file:Suppress("ktlint")
Expand All @@ -349,10 +349,10 @@ override fun hello(): JteModel = StaticJteModel<TemplateOutput>(
"DynamicTemplates.kt", withSystemLineEndings("""
@file:Suppress("ktlint")
package test.myktemplates
import gg.jte.TemplateEngine
import gg.jte.models.runtime.*
class DynamicTemplates(private val engine: TemplateEngine) : Templates {
\s
override fun hello(): JteModel {
Expand Down
5 changes: 1 addition & 4 deletions jte-runtime/src/main/java/gg/jte/TemplateEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,7 @@ public TemplateEngine reloadPrecompiled(Path classDirectory) throws TemplateExce
TemplateEngine engine = createPrecompiled(classDirectory, contentType, parentClassLoader, config.packageName);
engine.setHtmlInterceptor(htmlInterceptor);

Set<String> templates = new HashSet<>(templateCache.keySet());
for (String templateName : templates) {
engine.prepareForRendering(templateName);
}
templateCache.keySet().forEach(engine::prepareForRendering);

return engine;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface LocalizationSupport {
@SuppressWarnings("unused") // Called by template code
default Content localize(String key) {
String value = lookup(key);
if (value == null || value.length() == 0) {
if (value == null || value.isEmpty()) {
return null;
}

Expand All @@ -24,7 +24,7 @@ default Content localize(String key) {
@SuppressWarnings("unused") // Called by template code
default Content localize(String key, Object ... params) {
String value = lookup(key);
if (value == null || value.length() == 0) {
if (value == null || value.isEmpty()) {
return null;
}

Expand Down
6 changes: 3 additions & 3 deletions jte/src/main/java/gg/jte/compiler/LineInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import java.util.List;

final class LineInfo {
private static final List<TemplateParser.Mode> ConditionEndModes = Arrays.asList(TemplateParser.Mode.Condition, TemplateParser.Mode.Text);
private static final List<TemplateParser.Mode> ForLoopEndModes = Arrays.asList(TemplateParser.Mode.ForLoop, TemplateParser.Mode.Text);
private static final List<TemplateParser.Mode> RawEndModes = Collections.singletonList(TemplateParser.Mode.Raw);
private static final List<TemplateParser.Mode> ConditionEndModes = List.of(TemplateParser.Mode.Condition, TemplateParser.Mode.Text);
private static final List<TemplateParser.Mode> ForLoopEndModes = List.of(TemplateParser.Mode.ForLoop, TemplateParser.Mode.Text);
private static final List<TemplateParser.Mode> RawEndModes = List.of(TemplateParser.Mode.Raw);

private LineInfo() {
}
Expand Down
12 changes: 6 additions & 6 deletions jte/src/main/java/gg/jte/compiler/TemplateCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ public TemplateCompiler(TemplateConfig config, CodeResolver codeResolver, Path c

@Override
public Template load(String name) {
precompile(Collections.singletonList(name));
precompile(List.of(name));
return super.load(name);
}

@Override
public Template hotReload(String name) {
LinkedHashSet<ClassDefinition> classDefinitions = generate(Collections.singletonList(name), true);
LinkedHashSet<ClassDefinition> classDefinitions = generate(List.of(name), true);
classDefinitions.removeIf(c -> !c.isChanged());

if (!classDefinitions.isEmpty()) {
Expand Down Expand Up @@ -320,11 +320,11 @@ public List<String> getTemplatesUsing(String name) {
TemplateDependency dependency = new TemplateDependency(name, 0);

List<String> result = new ArrayList<>();
for (Map.Entry<String, LinkedHashSet<TemplateDependency>> dependencies : templateDependencies.entrySet()) {
if (dependencies.getValue().contains(dependency)) {
result.add(dependencies.getKey());
templateDependencies.forEach((key, value) -> {
if (value.contains(dependency)) {
result.add(key);
}
}
});

return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public String className() {

@Override
public List<ParamDescription> params() {
return new ArrayList<>(classDefinition.getParams());
return List.copyOf(classDefinition.getParams());
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion jte/src/test/java/gg/jte/DummyCodeResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ public void givenCode(String name, String code) {

@Override
public List<String> resolveAllTemplateNames() {
return new ArrayList<>(codeLookup.keySet());
return List.copyOf(codeLookup.keySet());
}
}
2 changes: 1 addition & 1 deletion jte/src/test/java/gg/jte/PreCompileTemplatesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private List<String> getCompilePath(String ... additionalPaths) {

String classPath = System.getProperty("java.class.path");
if (classPath != null) {
result.addAll(Arrays.asList(classPath.split(File.pathSeparator)));
result.addAll(List.of(classPath.split(File.pathSeparator)));
}

return result;
Expand Down

0 comments on commit a367477

Please sign in to comment.