Skip to content

Commit 9a7d4f4

Browse files
committed
Optimized invoker meta data and default memory.
- created class `Meta`. - added `Meta.Memory`. - added `Meta.OUTPUT`. - added `Meta.PROJECT`. - added `Address.OUTPUT`. - updated `Jamplate.execute(...)` - tests
1 parent fe2ea62 commit 9a7d4f4

4 files changed

Lines changed: 88 additions & 4 deletions

File tree

src/main/java/org/jamplate/impl/Address.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ public final class Address {
7474
@NotNull
7575
public static final String LINE = "__LINE__";
7676

77+
/**
78+
* An address used to store the output directory.
79+
*
80+
* @since 0.2.0 ~2021.06.03
81+
*/
82+
@NotNull
83+
public static final String OUTPUT = "__OUTPUT__";
84+
7785
/**
7886
* An address used to allocate the current path.
7987
*

src/main/java/org/jamplate/impl/Jamplate.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.io.IOException;
3838
import java.text.SimpleDateFormat;
3939
import java.util.Date;
40+
import java.util.Map;
4041
import java.util.Objects;
4142

4243
/**
@@ -446,10 +447,23 @@ public static boolean execute(@NotNull Environment environment, @Nullable Compil
446447
if (instruction != null) {
447448
Memory memory = new Memory();
448449

450+
Object memoryDefaults = environment.getMeta().get(Meta.MEMORY);
451+
452+
if (memoryDefaults instanceof Map)
453+
((Map<?, ?>) memoryDefaults).forEach((key, value) -> {
454+
String address = String.valueOf(key);
455+
String text = String.valueOf(value);
456+
memory.set(address, v -> text);
457+
});
458+
449459
//set project dir
450460
memory.set(
451461
Address.PROJECT,
452-
m -> String.valueOf(environment.getMeta().getOrDefault(Address.PROJECT, ""))
462+
m -> String.valueOf(environment.getMeta().getOrDefault(Meta.PROJECT, ""))
463+
);
464+
memory.set(
465+
Address.OUTPUT,
466+
m -> String.valueOf(environment.getMeta().getOrDefault(Meta.OUTPUT, ""))
453467
);
454468
memory.set(
455469
Address.JAMPLATE,
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2021 Cufy
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.jamplate.impl;
17+
18+
import org.jetbrains.annotations.NotNull;
19+
20+
/**
21+
* A class containing common meta keys.
22+
*
23+
* @author LSafer
24+
* @version 0.2.0
25+
* @since 0.2.0 ~2021.06.03
26+
*/
27+
public final class Meta {
28+
/**
29+
* The key for the memory default allocations. The stored value must be of type {@link
30+
* java.util.Map}.
31+
*
32+
* @since 0.2.0 ~2021.06.03
33+
*/
34+
@NotNull
35+
public static final String MEMORY = "memory";
36+
37+
/**
38+
* The key for the output directory.
39+
*
40+
* @since 0.2.0 ~2021.06.03
41+
*/
42+
@NotNull
43+
public static final String OUTPUT = "output";
44+
45+
/**
46+
* The key for the project directory.
47+
*
48+
* @since 0.2.0 ~2021.06.03
49+
*/
50+
@NotNull
51+
public static final String PROJECT = "project";
52+
53+
/**
54+
* Utility classes must not be initialized.
55+
*
56+
* @throws AssertionError when called.
57+
* @since 0.2.0 ~2021.06.03
58+
*/
59+
private Meta() {
60+
throw new AssertionError("No instance for you");
61+
}
62+
}

src/test/java/org/jamplate/FileTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//Use this class to manually test the directory test_input
22
package org.jamplate;
33

4-
import org.jamplate.impl.Address;
54
import org.jamplate.impl.Jamplate;
5+
import org.jamplate.impl.Meta;
66
import org.jamplate.impl.model.EnvironmentImpl;
77
import org.jamplate.impl.model.FileDocument;
88
import org.jamplate.impl.model.PseudoDocument;
@@ -26,7 +26,7 @@ public void pseudo() {
2626

2727
Environment environment = new EnvironmentImpl();
2828

29-
environment.getMeta().put(Address.PROJECT, new File("test_input"));
29+
environment.getMeta().put(Meta.PROJECT, new File("test_input"));
3030

3131
boolean compiled = Jamplate.compile(environment, documents);
3232

@@ -68,7 +68,7 @@ public void run() {
6868

6969
Environment environment = new EnvironmentImpl();
7070

71-
environment.getMeta().put(Address.PROJECT, new File("test_input"));
71+
environment.getMeta().put(Meta.PROJECT, new File("test_input"));
7272

7373
boolean compiled = Jamplate.compile(environment, documents);
7474

0 commit comments

Comments
 (0)