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-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-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-13 03:01:13 +01:00
|
|
|
addSimpleMapper("KEY_TO_OBJ", type.getClassType()+".valueOf");
|
|
|
|
addInjectMapper("OBJ_TO_KEY(\\([^)]+\\)|\\S)", "%s."+type.getKeyType()+"Value()").removeBraces();
|
|
|
|
addInjectMapper("CLASS_TO_KEY(\\([^)]+\\)|\\S)", "(("+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");
|
|
|
|
addSimpleMapper("CONSUMER", type.getFileType()+"Consumer");
|
|
|
|
addSimpleMapper("ITERATOR", type.getFileType()+"Iterator");
|
|
|
|
addSimpleMapper("ITERABLE", type.getFileType()+"Iterable");
|
|
|
|
addSimpleMapper("ABSTRACT_COLLECTION", type.getFileType()+"AbstractCollection");
|
|
|
|
addSimpleMapper("COLLECTION", type.getFileType()+"Collection");
|
2020-11-12 02:13:54 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public GlobalVariables createFunctions()
|
|
|
|
{
|
2020-11-13 03:01:13 +01:00
|
|
|
addSimpleMapper("NEXT()", "next"+type.getNonClassType()+"()");
|
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-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-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
|
|
|
}
|
|
|
|
}
|