-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
0.1.0 - Initial commit of existing code moved from JTextUtil library …
…this this new library.
- Loading branch information
0 parents
commit 72e1eb2
Showing
10 changed files
with
385 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="src" path="test"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> | ||
<classpathentry kind="lib" path="C:/Users/TeamworkGuy2/Documents/Java/Libraries/jsimple-types/jar/jsimple_types.jar" sourcepath="/JSimpleTypes/src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/TestUtil"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 TeamworkGuy2 | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
JTextTemplate | ||
============== | ||
version: 0.1.0 | ||
|
||
Create and compile templates containing string literals and variables. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"version" : "0.1.0", | ||
"name" : "jtext-template", | ||
"description" : "Create and compile templates containing string literals and variables", | ||
"homepage" : "https://github.com/TeamworkGuy2/JTextTemplate", | ||
"license" : "MIT", | ||
"main" : "./jar/jtext-template.jar", | ||
"dependencies" : { | ||
"jsimple-types" : "*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package twg2.text.stringTemplate; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* @author TeamworkGuy2 | ||
* @since 2015-7-28 | ||
*/ | ||
public interface SingleIntTemplate extends StringTemplate { | ||
|
||
|
||
public default String compile(int value) { | ||
return this.compile(Arrays.asList(value)); | ||
} | ||
|
||
|
||
public default void compile(int value, Appendable dst) { | ||
this.compile(Arrays.asList(value), dst); | ||
} | ||
|
||
|
||
public static SingleIntTemplate of(StringTemplateBuilder strTmpl) { | ||
if(StringTemplateBuilder.isSingleIntTemplate(strTmpl)) { | ||
return strTmpl; | ||
} | ||
else { | ||
throw new IllegalArgumentException("string template does not match single int argument"); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package twg2.text.stringTemplate; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* @author TeamworkGuy2 | ||
* @since 2015-7-28 | ||
*/ | ||
public interface SingleVarTemplate<T> extends StringTemplate { | ||
|
||
|
||
public default String compile(T value) { | ||
return this.compile(Arrays.asList(value)); | ||
} | ||
|
||
|
||
public default void compile(T value, Appendable dst) { | ||
this.compile(Arrays.asList(value), dst); | ||
} | ||
|
||
|
||
public static <R> SingleVarTemplate<R> of(StringTemplateBuilder strTmpl, Class<R> type) { | ||
if(StringTemplateBuilder.isSingleTypeTemplate(strTmpl, type)) { | ||
@SuppressWarnings("unchecked") | ||
SingleVarTemplate<R> resTmpl = (SingleVarTemplate<R>) strTmpl; | ||
return resTmpl; | ||
} | ||
else { | ||
throw new IllegalArgumentException("string template does not match single " + type + " argument"); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package twg2.text.stringTemplate; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* @author TeamworkGuy2 | ||
* @since 2015-7-28 | ||
*/ | ||
public interface StringTemplate { | ||
|
||
public int getArgCount(); | ||
|
||
|
||
public boolean isArgLiteral(int idx); | ||
|
||
|
||
public Object getArgLiteralValue(int idx); | ||
|
||
|
||
public default String getArgLiteralString(int idx) { | ||
if(isArgLiteral(idx)) { | ||
Class<?> argType = getArgType(idx); | ||
if(argType == String.class) { | ||
return (String)getArgLiteralValue(idx); | ||
} | ||
else { | ||
throw new IllegalArgumentException("arg " + idx + " expected type 'String', found '" + argType + "'"); | ||
} | ||
} | ||
else { | ||
throw new IllegalArgumentException("arg " + idx + " is not a literal value"); | ||
} | ||
} | ||
|
||
|
||
public Class<?> getArgType(int idx); | ||
|
||
|
||
public void compileTo(List<Object> args, Appendable dst); | ||
|
||
|
||
public default void compileTo(Object[] args, Appendable dst) { | ||
compileTo(Arrays.asList(args), dst); | ||
} | ||
|
||
|
||
public default String compile(Object... args) { | ||
StringBuilder strB = new StringBuilder(); | ||
compileTo(args, strB); | ||
return strB.toString(); | ||
} | ||
|
||
|
||
public default String compile(List<Object> args) { | ||
StringBuilder strB = new StringBuilder(); | ||
compileTo(args, strB); | ||
return strB.toString(); | ||
} | ||
|
||
} |
136 changes: 136 additions & 0 deletions
136
src/twg2/text/stringTemplate/StringTemplateBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package twg2.text.stringTemplate; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.util.AbstractMap; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
import twg2.primitiveIoTypes.JPrimitiveType; | ||
|
||
public class StringTemplateBuilder implements StringTemplate, SingleIntTemplate, SingleVarTemplate<Object> { | ||
private List<Object> templateParts = new ArrayList<>(); | ||
private List<Class<?>> partType = new ArrayList<>(); | ||
private List<Boolean> partIsLiteral = new ArrayList<>(); | ||
private List<Function<Object, String>> partToString = new ArrayList<Function<Object,String>>(); | ||
private List<Map.Entry<Integer, Class<?>>> nonLiteralTypesByIndex = new ArrayList<>(); | ||
|
||
|
||
public StringTemplateBuilder() { | ||
} | ||
|
||
|
||
@Override | ||
public int getArgCount() { | ||
return templateParts.size(); | ||
} | ||
|
||
|
||
@Override | ||
public boolean isArgLiteral(int idx) { | ||
return partIsLiteral.get(idx); | ||
} | ||
|
||
|
||
@Override | ||
public Object getArgLiteralValue(int idx) { | ||
return templateParts.get(idx); | ||
} | ||
|
||
|
||
@Override | ||
public Class<?> getArgType(int idx) { | ||
return partType.get(idx); | ||
} | ||
|
||
|
||
public StringTemplateBuilder and(String str) { | ||
templateParts.add(str); | ||
partType.add(String.class); | ||
partIsLiteral.add(true); | ||
partToString.add(null); | ||
return this; | ||
} | ||
|
||
|
||
public <T> StringTemplateBuilder and(Class<T> type, Function<T, String> toString) { | ||
templateParts.add(null); | ||
partType.add(type); | ||
partIsLiteral.add(false); | ||
@SuppressWarnings("unchecked") | ||
Function<Object, String> toStringFunc = (Function<Object, String>)toString; | ||
partToString.add(toStringFunc); | ||
nonLiteralTypesByIndex.add(new AbstractMap.SimpleImmutableEntry<>(templateParts.size() - 1, type)); | ||
return this; | ||
} | ||
|
||
|
||
public StringTemplateBuilder and(Class<?> type) { | ||
templateParts.add(null); | ||
partType.add(type); | ||
partIsLiteral.add(false); | ||
partToString.add(null); | ||
nonLiteralTypesByIndex.add(new AbstractMap.SimpleImmutableEntry<>(templateParts.size() - 1, type)); | ||
return this; | ||
} | ||
|
||
|
||
public StringTemplateBuilder andInt() { | ||
return this.and(Integer.TYPE); | ||
} | ||
|
||
|
||
public StringTemplateBuilder andString() { | ||
return this.and(String.class); | ||
} | ||
|
||
|
||
@Override | ||
public void compileTo(List<Object> args, Appendable dst) { | ||
try { | ||
int varIdx = 0; | ||
for(int i = 0, size = templateParts.size(); i < size; i++) { | ||
if(partIsLiteral.get(i)) { | ||
dst.append(templateParts.get(i).toString()); | ||
} | ||
else { | ||
Object arg = args.get(varIdx); | ||
if(arg == null) { | ||
Function<Object, String> toString = partToString.get(i); | ||
dst.append(toString != null ? toString.apply(arg) : "null"); | ||
} | ||
else { | ||
Class<?> argType = arg.getClass(); | ||
JPrimitiveType primitiveType = null; | ||
|
||
if(!partType.get(i).isAssignableFrom(argType) && | ||
((primitiveType = JPrimitiveType.tryFromType(argType)) == null) || (primitiveType != null && !partType.get(i).isAssignableFrom(primitiveType.getType()))) { | ||
throw new IllegalArgumentException("arg " + varIdx + " expected type '" + partType.get(i) + "' but arg type was '" + arg + "'"); | ||
} | ||
Function<Object, String> toString = partToString.get(i); | ||
dst.append(toString != null ? toString.apply(arg) : arg.toString()); | ||
} | ||
varIdx++; | ||
} | ||
} | ||
} catch(IOException ioe) { | ||
throw new UncheckedIOException(ioe); | ||
} | ||
} | ||
|
||
|
||
public static boolean isSingleIntTemplate(StringTemplateBuilder strTmpl) { | ||
List<Map.Entry<Integer, Class<?>>> types = strTmpl.nonLiteralTypesByIndex; | ||
Class<?> type0 = null; | ||
return types.size() == 1 && ((type0 = types.get(0).getValue()) == Integer.class || type0 == Integer.TYPE); | ||
} | ||
|
||
|
||
public static <T> boolean isSingleTypeTemplate(StringTemplateBuilder strTmpl, Class<T> type) { | ||
List<Map.Entry<Integer, Class<?>>> types = strTmpl.nonLiteralTypesByIndex; | ||
return types.size() == 1 && (types.get(0).getValue() == type); | ||
} | ||
|
||
} |
71 changes: 71 additions & 0 deletions
71
test/twg2/text/stringTemplate/test/StringTemplateTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package twg2.text.stringTemplate.test; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import twg2.text.stringTemplate.SingleIntTemplate; | ||
import twg2.text.stringTemplate.SingleVarTemplate; | ||
import twg2.text.stringTemplate.StringTemplateBuilder; | ||
import checks.CheckTask; | ||
|
||
/** | ||
* @author TeamworkGuy2 | ||
* @since 2015-7-28 | ||
*/ | ||
public class StringTemplateTest { | ||
|
||
private static class StrTmplData { | ||
private StringTemplateBuilder strTmplBldr; | ||
private List<List<Object>> testArgs; | ||
private List<String> expectResults; | ||
|
||
public StrTmplData(StringTemplateBuilder strTmplBldr, List<List<Object>> testArgs, List<String> expectResults) { | ||
this.strTmplBldr = strTmplBldr; | ||
this.testArgs = testArgs; | ||
this.expectResults = expectResults; | ||
} | ||
|
||
} | ||
|
||
|
||
@Test | ||
public void testStringTemplateCompile() { | ||
for(StrTmplData data : createTemplateBuilders()) { | ||
int i = 0; | ||
for(List<Object> args : data.testArgs) { | ||
Assert.assertEquals(data.expectResults.get(i), data.strTmplBldr.compile(args)); | ||
i++; | ||
} | ||
} | ||
} | ||
|
||
|
||
@Test | ||
public void testStringTemplateVar() { | ||
CheckTask.assertException(() -> SingleVarTemplate.of(new StringTemplateBuilder().and("none"), String.class)); | ||
CheckTask.assertException(() -> SingleIntTemplate.of(new StringTemplateBuilder().and("none"))); | ||
|
||
CheckTask.assertException(() -> SingleVarTemplate.of(new StringTemplateBuilder().and("none"), String.class).compile(new Object())); | ||
CheckTask.assertException(() -> SingleIntTemplate.of(new StringTemplateBuilder().and("none")).compile("25")); | ||
|
||
Assert.assertEquals("none", SingleVarTemplate.of(new StringTemplateBuilder().and("none").and(String.class), String.class).compile(new String())); | ||
Assert.assertEquals("none25", SingleIntTemplate.of(new StringTemplateBuilder().and("none").andInt()).compile(25)); | ||
} | ||
|
||
|
||
private static List<StrTmplData> createTemplateBuilders() { | ||
return Arrays.asList( | ||
new StrTmplData(new StringTemplateBuilder().and(":: ").andInt().and("-arc"), | ||
Arrays.asList(Arrays.asList(1), Arrays.asList(3), Arrays.asList(5), Arrays.asList((Object)null)), | ||
Arrays.asList(":: 1-arc", ":: 3-arc", ":: 5-arc", ":: null-arc")), | ||
|
||
new StrTmplData(new StringTemplateBuilder().and("most ").andString().and(byte[].class, Arrays::toString).and("!").and(Boolean.class), | ||
Arrays.asList(Arrays.asList("= ", new byte[] {}, true), Arrays.asList("()", new byte[] { 3 }, true), Arrays.asList("", new byte[] { 10, 20 }, false), Arrays.asList(",", null, true)), | ||
Arrays.asList("most = []!true", "most ()[3]!true", "most [10, 20]!false", "most ,null!true")) | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
-------- | ||
####0.1.0 | ||
date: 2016-2-28 | ||
|
||
commit: ? | ||
|
||
* Initial commit of code moved from [JTextUtil] (https://github.com/TeamworkGuy2/JTextFluff) |