Skip to content

Commit 72e1eb2

Browse files
committed
0.1.0 - Initial commit of existing code moved from JTextUtil library this this new library.
0 parents  commit 72e1eb2

File tree

10 files changed

+385
-0
lines changed

10 files changed

+385
-0
lines changed

.classpath

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="src" path="test"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
6+
<classpathentry kind="lib" path="C:/Users/TeamworkGuy2/Documents/Java/Libraries/jsimple-types/jar/jsimple_types.jar" sourcepath="/JSimpleTypes/src"/>
7+
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
8+
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/TestUtil"/>
9+
<classpathentry kind="output" path="bin"/>
10+
</classpath>

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 TeamworkGuy2
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
JTextTemplate
2+
==============
3+
version: 0.1.0
4+
5+
Create and compile templates containing string literals and variables.

package-lib.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"version" : "0.1.0",
3+
"name" : "jtext-template",
4+
"description" : "Create and compile templates containing string literals and variables",
5+
"homepage" : "https://github.com/TeamworkGuy2/JTextTemplate",
6+
"license" : "MIT",
7+
"main" : "./jar/jtext-template.jar",
8+
"dependencies" : {
9+
"jsimple-types" : "*"
10+
}
11+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package twg2.text.stringTemplate;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* @author TeamworkGuy2
7+
* @since 2015-7-28
8+
*/
9+
public interface SingleIntTemplate extends StringTemplate {
10+
11+
12+
public default String compile(int value) {
13+
return this.compile(Arrays.asList(value));
14+
}
15+
16+
17+
public default void compile(int value, Appendable dst) {
18+
this.compile(Arrays.asList(value), dst);
19+
}
20+
21+
22+
public static SingleIntTemplate of(StringTemplateBuilder strTmpl) {
23+
if(StringTemplateBuilder.isSingleIntTemplate(strTmpl)) {
24+
return strTmpl;
25+
}
26+
else {
27+
throw new IllegalArgumentException("string template does not match single int argument");
28+
}
29+
}
30+
31+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package twg2.text.stringTemplate;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* @author TeamworkGuy2
7+
* @since 2015-7-28
8+
*/
9+
public interface SingleVarTemplate<T> extends StringTemplate {
10+
11+
12+
public default String compile(T value) {
13+
return this.compile(Arrays.asList(value));
14+
}
15+
16+
17+
public default void compile(T value, Appendable dst) {
18+
this.compile(Arrays.asList(value), dst);
19+
}
20+
21+
22+
public static <R> SingleVarTemplate<R> of(StringTemplateBuilder strTmpl, Class<R> type) {
23+
if(StringTemplateBuilder.isSingleTypeTemplate(strTmpl, type)) {
24+
@SuppressWarnings("unchecked")
25+
SingleVarTemplate<R> resTmpl = (SingleVarTemplate<R>) strTmpl;
26+
return resTmpl;
27+
}
28+
else {
29+
throw new IllegalArgumentException("string template does not match single " + type + " argument");
30+
}
31+
}
32+
33+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package twg2.text.stringTemplate;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
/**
7+
* @author TeamworkGuy2
8+
* @since 2015-7-28
9+
*/
10+
public interface StringTemplate {
11+
12+
public int getArgCount();
13+
14+
15+
public boolean isArgLiteral(int idx);
16+
17+
18+
public Object getArgLiteralValue(int idx);
19+
20+
21+
public default String getArgLiteralString(int idx) {
22+
if(isArgLiteral(idx)) {
23+
Class<?> argType = getArgType(idx);
24+
if(argType == String.class) {
25+
return (String)getArgLiteralValue(idx);
26+
}
27+
else {
28+
throw new IllegalArgumentException("arg " + idx + " expected type 'String', found '" + argType + "'");
29+
}
30+
}
31+
else {
32+
throw new IllegalArgumentException("arg " + idx + " is not a literal value");
33+
}
34+
}
35+
36+
37+
public Class<?> getArgType(int idx);
38+
39+
40+
public void compileTo(List<Object> args, Appendable dst);
41+
42+
43+
public default void compileTo(Object[] args, Appendable dst) {
44+
compileTo(Arrays.asList(args), dst);
45+
}
46+
47+
48+
public default String compile(Object... args) {
49+
StringBuilder strB = new StringBuilder();
50+
compileTo(args, strB);
51+
return strB.toString();
52+
}
53+
54+
55+
public default String compile(List<Object> args) {
56+
StringBuilder strB = new StringBuilder();
57+
compileTo(args, strB);
58+
return strB.toString();
59+
}
60+
61+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package twg2.text.stringTemplate;
2+
3+
import java.io.IOException;
4+
import java.io.UncheckedIOException;
5+
import java.util.AbstractMap;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.function.Function;
10+
11+
import twg2.primitiveIoTypes.JPrimitiveType;
12+
13+
public class StringTemplateBuilder implements StringTemplate, SingleIntTemplate, SingleVarTemplate<Object> {
14+
private List<Object> templateParts = new ArrayList<>();
15+
private List<Class<?>> partType = new ArrayList<>();
16+
private List<Boolean> partIsLiteral = new ArrayList<>();
17+
private List<Function<Object, String>> partToString = new ArrayList<Function<Object,String>>();
18+
private List<Map.Entry<Integer, Class<?>>> nonLiteralTypesByIndex = new ArrayList<>();
19+
20+
21+
public StringTemplateBuilder() {
22+
}
23+
24+
25+
@Override
26+
public int getArgCount() {
27+
return templateParts.size();
28+
}
29+
30+
31+
@Override
32+
public boolean isArgLiteral(int idx) {
33+
return partIsLiteral.get(idx);
34+
}
35+
36+
37+
@Override
38+
public Object getArgLiteralValue(int idx) {
39+
return templateParts.get(idx);
40+
}
41+
42+
43+
@Override
44+
public Class<?> getArgType(int idx) {
45+
return partType.get(idx);
46+
}
47+
48+
49+
public StringTemplateBuilder and(String str) {
50+
templateParts.add(str);
51+
partType.add(String.class);
52+
partIsLiteral.add(true);
53+
partToString.add(null);
54+
return this;
55+
}
56+
57+
58+
public <T> StringTemplateBuilder and(Class<T> type, Function<T, String> toString) {
59+
templateParts.add(null);
60+
partType.add(type);
61+
partIsLiteral.add(false);
62+
@SuppressWarnings("unchecked")
63+
Function<Object, String> toStringFunc = (Function<Object, String>)toString;
64+
partToString.add(toStringFunc);
65+
nonLiteralTypesByIndex.add(new AbstractMap.SimpleImmutableEntry<>(templateParts.size() - 1, type));
66+
return this;
67+
}
68+
69+
70+
public StringTemplateBuilder and(Class<?> type) {
71+
templateParts.add(null);
72+
partType.add(type);
73+
partIsLiteral.add(false);
74+
partToString.add(null);
75+
nonLiteralTypesByIndex.add(new AbstractMap.SimpleImmutableEntry<>(templateParts.size() - 1, type));
76+
return this;
77+
}
78+
79+
80+
public StringTemplateBuilder andInt() {
81+
return this.and(Integer.TYPE);
82+
}
83+
84+
85+
public StringTemplateBuilder andString() {
86+
return this.and(String.class);
87+
}
88+
89+
90+
@Override
91+
public void compileTo(List<Object> args, Appendable dst) {
92+
try {
93+
int varIdx = 0;
94+
for(int i = 0, size = templateParts.size(); i < size; i++) {
95+
if(partIsLiteral.get(i)) {
96+
dst.append(templateParts.get(i).toString());
97+
}
98+
else {
99+
Object arg = args.get(varIdx);
100+
if(arg == null) {
101+
Function<Object, String> toString = partToString.get(i);
102+
dst.append(toString != null ? toString.apply(arg) : "null");
103+
}
104+
else {
105+
Class<?> argType = arg.getClass();
106+
JPrimitiveType primitiveType = null;
107+
108+
if(!partType.get(i).isAssignableFrom(argType) &&
109+
((primitiveType = JPrimitiveType.tryFromType(argType)) == null) || (primitiveType != null && !partType.get(i).isAssignableFrom(primitiveType.getType()))) {
110+
throw new IllegalArgumentException("arg " + varIdx + " expected type '" + partType.get(i) + "' but arg type was '" + arg + "'");
111+
}
112+
Function<Object, String> toString = partToString.get(i);
113+
dst.append(toString != null ? toString.apply(arg) : arg.toString());
114+
}
115+
varIdx++;
116+
}
117+
}
118+
} catch(IOException ioe) {
119+
throw new UncheckedIOException(ioe);
120+
}
121+
}
122+
123+
124+
public static boolean isSingleIntTemplate(StringTemplateBuilder strTmpl) {
125+
List<Map.Entry<Integer, Class<?>>> types = strTmpl.nonLiteralTypesByIndex;
126+
Class<?> type0 = null;
127+
return types.size() == 1 && ((type0 = types.get(0).getValue()) == Integer.class || type0 == Integer.TYPE);
128+
}
129+
130+
131+
public static <T> boolean isSingleTypeTemplate(StringTemplateBuilder strTmpl, Class<T> type) {
132+
List<Map.Entry<Integer, Class<?>>> types = strTmpl.nonLiteralTypesByIndex;
133+
return types.size() == 1 && (types.get(0).getValue() == type);
134+
}
135+
136+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package twg2.text.stringTemplate.test;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
import twg2.text.stringTemplate.SingleIntTemplate;
10+
import twg2.text.stringTemplate.SingleVarTemplate;
11+
import twg2.text.stringTemplate.StringTemplateBuilder;
12+
import checks.CheckTask;
13+
14+
/**
15+
* @author TeamworkGuy2
16+
* @since 2015-7-28
17+
*/
18+
public class StringTemplateTest {
19+
20+
private static class StrTmplData {
21+
private StringTemplateBuilder strTmplBldr;
22+
private List<List<Object>> testArgs;
23+
private List<String> expectResults;
24+
25+
public StrTmplData(StringTemplateBuilder strTmplBldr, List<List<Object>> testArgs, List<String> expectResults) {
26+
this.strTmplBldr = strTmplBldr;
27+
this.testArgs = testArgs;
28+
this.expectResults = expectResults;
29+
}
30+
31+
}
32+
33+
34+
@Test
35+
public void testStringTemplateCompile() {
36+
for(StrTmplData data : createTemplateBuilders()) {
37+
int i = 0;
38+
for(List<Object> args : data.testArgs) {
39+
Assert.assertEquals(data.expectResults.get(i), data.strTmplBldr.compile(args));
40+
i++;
41+
}
42+
}
43+
}
44+
45+
46+
@Test
47+
public void testStringTemplateVar() {
48+
CheckTask.assertException(() -> SingleVarTemplate.of(new StringTemplateBuilder().and("none"), String.class));
49+
CheckTask.assertException(() -> SingleIntTemplate.of(new StringTemplateBuilder().and("none")));
50+
51+
CheckTask.assertException(() -> SingleVarTemplate.of(new StringTemplateBuilder().and("none"), String.class).compile(new Object()));
52+
CheckTask.assertException(() -> SingleIntTemplate.of(new StringTemplateBuilder().and("none")).compile("25"));
53+
54+
Assert.assertEquals("none", SingleVarTemplate.of(new StringTemplateBuilder().and("none").and(String.class), String.class).compile(new String()));
55+
Assert.assertEquals("none25", SingleIntTemplate.of(new StringTemplateBuilder().and("none").andInt()).compile(25));
56+
}
57+
58+
59+
private static List<StrTmplData> createTemplateBuilders() {
60+
return Arrays.asList(
61+
new StrTmplData(new StringTemplateBuilder().and(":: ").andInt().and("-arc"),
62+
Arrays.asList(Arrays.asList(1), Arrays.asList(3), Arrays.asList(5), Arrays.asList((Object)null)),
63+
Arrays.asList(":: 1-arc", ":: 3-arc", ":: 5-arc", ":: null-arc")),
64+
65+
new StrTmplData(new StringTemplateBuilder().and("most ").andString().and(byte[].class, Arrays::toString).and("!").and(Boolean.class),
66+
Arrays.asList(Arrays.asList("= ", new byte[] {}, true), Arrays.asList("()", new byte[] { 3 }, true), Arrays.asList("", new byte[] { 10, 20 }, false), Arrays.asList(",", null, true)),
67+
Arrays.asList("most = []!true", "most ()[3]!true", "most [10, 20]!false", "most ,null!true"))
68+
);
69+
}
70+
71+
}

versions.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
--------
2+
####0.1.0
3+
date: 2016-2-28
4+
5+
commit: ?
6+
7+
* Initial commit of code moved from [JTextUtil] (https://github.com/TeamworkGuy2/JTextFluff)

0 commit comments

Comments
 (0)