2020-11-11 21:33:59 +01:00
|
|
|
package speiger.src.builder.example;
|
|
|
|
|
2020-11-12 02:13:54 +01:00
|
|
|
import java.nio.file.Path;
|
2020-11-11 21:33:59 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.LinkedHashSet;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.function.UnaryOperator;
|
|
|
|
|
2020-11-16 02:55:33 +01:00
|
|
|
import speiger.src.builder.mappers.ArgumentMapper;
|
2020-11-12 02:13:54 +01:00
|
|
|
import speiger.src.builder.mappers.InjectMapper;
|
2020-11-11 21:33:59 +01:00
|
|
|
import speiger.src.builder.mappers.SimpleMapper;
|
|
|
|
import speiger.src.builder.processor.TemplateProcess;
|
|
|
|
|
|
|
|
public class GlobalVariables
|
|
|
|
{
|
|
|
|
List<UnaryOperator<String>> operators = new ArrayList<>();
|
|
|
|
Set<String> flags = new LinkedHashSet<>();
|
2020-11-12 02:13:54 +01:00
|
|
|
ClassType type;
|
2020-11-11 21:33:59 +01:00
|
|
|
|
2020-11-12 02:13:54 +01:00
|
|
|
public GlobalVariables(ClassType type)
|
2020-11-11 21:33:59 +01:00
|
|
|
{
|
2020-11-12 02:13:54 +01:00
|
|
|
this.type = type;
|
2020-11-11 21:33:59 +01:00
|
|
|
}
|
|
|
|
|
2020-11-12 02:13:54 +01:00
|
|
|
public GlobalVariables createVariables()
|
2020-11-11 21:33:59 +01:00
|
|
|
{
|
2020-11-13 03:01:13 +01:00
|
|
|
addSimpleMapper("PACKAGE", type.getPathType());
|
|
|
|
addSimpleMapper("CLASS_TYPE", type.getClassType());
|
|
|
|
addSimpleMapper("KEY_TYPE", type.getKeyType());
|
|
|
|
addSimpleMapper(" KEY_GENERIC_TYPE", type == ClassType.OBJECT ? "<"+type.getKeyType()+">" : "");
|
2020-11-16 02:55:33 +01:00
|
|
|
addSimpleMapper(" GENERIC_BRACES", type == ClassType.OBJECT ? " <"+type.getKeyType()+">" : "");
|
|
|
|
addSimpleMapper("BRACES", type == ClassType.OBJECT ? "<>" : "");
|
2020-11-12 02:13:54 +01:00
|
|
|
if(type.needsCustomJDKType())
|
|
|
|
{
|
2020-11-13 03:01:13 +01:00
|
|
|
addSimpleMapper("JAVA_TYPE", type.getCustomJDKType().getKeyType());
|
|
|
|
addSimpleMapper("SANITY_CAST", "castTo"+type.getFileType());
|
2020-11-12 02:13:54 +01:00
|
|
|
}
|
2020-11-16 02:55:33 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public GlobalVariables createHelperVariables()
|
|
|
|
{
|
|
|
|
addArgumentMapper("EQUALS_KEY_TYPE", type == ClassType.OBJECT ? "Objects.equals(%1$s, %2$s)" : "Objects.equals(KEY_TO_OBJ(%1$s), %2$s)").removeBraces();
|
|
|
|
addArgumentMapper("EQUALS", type.getEquals()).removeBraces();
|
2020-11-13 03:01:13 +01:00
|
|
|
addSimpleMapper("KEY_TO_OBJ", type.getClassType()+".valueOf");
|
2020-11-16 02:55:33 +01:00
|
|
|
addInjectMapper("OBJ_TO_KEY", type == ClassType.OBJECT ? "%s" : "%s."+type.getKeyType()+"Value()").removeBraces();
|
|
|
|
addInjectMapper("CLASS_TO_KEY", "(("+type.getClassType()+")%s)."+type.getKeyType()+"Value()").removeBraces();
|
2020-11-11 21:33:59 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-11-12 02:13:54 +01:00
|
|
|
public GlobalVariables createClassTypes()
|
2020-11-11 21:33:59 +01:00
|
|
|
{
|
2020-11-13 03:01:13 +01:00
|
|
|
addSimpleMapper("JAVA_PREDICATE", type.isPrimitiveBlocking() ? "" : type.getCustomJDKType().getFileType()+"Predicate");
|
|
|
|
addSimpleMapper("JAVA_CONSUMER", type.isPrimitiveBlocking() ? "" : "java.util.function."+type.getCustomJDKType().getFileType()+"Consumer");
|
2020-11-16 02:55:33 +01:00
|
|
|
addClassMapper("CONSUMER", "Consumer");
|
|
|
|
addClassMapper("ITERATORS", "Iterators");
|
|
|
|
addClassMapper("BI_ITERATOR", "BidirectionalIterator");
|
|
|
|
addClassMapper("LIST_ITERATOR", "ListIterator");
|
|
|
|
addClassMapper("ITERATOR", "Iterator");
|
|
|
|
addClassMapper("ITERABLE", "Iterable");
|
|
|
|
addClassMapper("ABSTRACT_COLLECTION", "AbstractCollection");
|
|
|
|
addClassMapper("COLLECTION", "Collection");
|
|
|
|
addClassMapper("ARRAYS", "Arrays");
|
|
|
|
addClassMapper("ABSTRACT_LIST", "AbstractList");
|
|
|
|
addClassMapper("LIST_ITER", "ListIter");
|
|
|
|
addClassMapper("SUB_LIST", "SubList");
|
|
|
|
addClassMapper("ARRAY_LIST", "ArrayList");
|
|
|
|
addClassMapper("LIST", "List");
|
2020-11-12 02:13:54 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public GlobalVariables createFunctions()
|
|
|
|
{
|
2020-11-16 02:55:33 +01:00
|
|
|
addFunctionMapper("NEXT", "next");
|
|
|
|
addSimpleMapper("TO_ARRAY", "to"+type.getNonFileType()+"Array");
|
|
|
|
addFunctionMapper("GET_KEY", "get");
|
|
|
|
addFunctionMapper("REMOVE_KEY", "rem");
|
|
|
|
addFunctionMapper("REMOVE", "remove");
|
|
|
|
addFunctionMapper("PREVIOUS", "previous");
|
2020-11-12 02:13:54 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public GlobalVariables createFlags()
|
|
|
|
{
|
|
|
|
flags.add("TYPE_"+type.getCapType());
|
|
|
|
if(!type.needsCustomJDKType())
|
|
|
|
{
|
|
|
|
flags.add("JDK_CONSUMER");
|
|
|
|
}
|
2020-11-13 03:01:13 +01:00
|
|
|
if(!type.isPrimitiveBlocking())
|
|
|
|
{
|
|
|
|
flags.add("PRIMITIVES");
|
|
|
|
}
|
2020-11-11 21:33:59 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public TemplateProcess create(String fileName)
|
|
|
|
{
|
2020-11-12 02:13:54 +01:00
|
|
|
TemplateProcess process = new TemplateProcess(type.getFileType()+fileName+".java");
|
|
|
|
process.setPathBuilder(new PathBuilder(type.getPathType()));
|
|
|
|
process.addFlags(flags);
|
|
|
|
process.addMappers(operators);
|
|
|
|
return process;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ClassType getType()
|
|
|
|
{
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2020-11-16 02:55:33 +01:00
|
|
|
private void addClassMapper(String pattern, String replacement)
|
|
|
|
{
|
|
|
|
operators.add(new SimpleMapper(pattern, type.getFileType()+replacement));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void addFunctionMapper(String pattern, String replacement)
|
|
|
|
{
|
|
|
|
operators.add(new SimpleMapper(pattern, replacement+type.getNonFileType()));
|
|
|
|
}
|
|
|
|
|
2020-11-13 03:01:13 +01:00
|
|
|
private void addSimpleMapper(String pattern, String replacement)
|
|
|
|
{
|
|
|
|
operators.add(new SimpleMapper(pattern, replacement));
|
|
|
|
}
|
|
|
|
|
|
|
|
private InjectMapper addInjectMapper(String pattern, String replacement)
|
|
|
|
{
|
|
|
|
InjectMapper mapper = new InjectMapper(pattern, replacement);
|
|
|
|
operators.add(mapper);
|
|
|
|
return mapper;
|
|
|
|
}
|
|
|
|
|
2020-11-16 02:55:33 +01:00
|
|
|
private ArgumentMapper addArgumentMapper(String pattern, String replacement)
|
|
|
|
{
|
|
|
|
return addArgumentMapper(pattern, replacement, ", ");
|
|
|
|
}
|
|
|
|
|
|
|
|
private ArgumentMapper addArgumentMapper(String pattern, String replacement, String splitter)
|
|
|
|
{
|
|
|
|
ArgumentMapper mapper = new ArgumentMapper(pattern, replacement, splitter);
|
|
|
|
operators.add(mapper);
|
|
|
|
return mapper;
|
|
|
|
}
|
|
|
|
|
2020-11-12 02:13:54 +01:00
|
|
|
class PathBuilder implements UnaryOperator<Path>
|
|
|
|
{
|
|
|
|
String before;
|
|
|
|
|
|
|
|
public PathBuilder(String before)
|
|
|
|
{
|
|
|
|
this.before = before;
|
|
|
|
}
|
2020-11-11 21:33:59 +01:00
|
|
|
|
2020-11-12 02:13:54 +01:00
|
|
|
@Override
|
|
|
|
public Path apply(Path t)
|
|
|
|
{
|
|
|
|
return t.subpath(0, 6).resolve(before).resolve(t.subpath(6, t.getNameCount()));
|
|
|
|
}
|
2020-11-11 21:33:59 +01:00
|
|
|
}
|
|
|
|
}
|