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-12 02:13:54 +01:00
|
|
|
operators.add(new SimpleMapper("PACKAGE", type.getPathType()));
|
|
|
|
operators.add(new SimpleMapper("CLASS_TYPE", type.getClassType()));
|
|
|
|
operators.add(new SimpleMapper("KEY_TYPE", type.getKeyType()));
|
|
|
|
operators.add(new SimpleMapper(" KEY_GENERIC_TYPE", type == ClassType.OBJECT ? "<"+type.getKeyType()+">" : ""));
|
|
|
|
if(type.needsCustomJDKType())
|
|
|
|
{
|
|
|
|
operators.add(new SimpleMapper("JAVA_TYPE", type.getCustomJDKType().getKeyType()));
|
|
|
|
operators.add(new SimpleMapper("SANITY_CAST", "castTo"+type.getFileType()));
|
|
|
|
}
|
|
|
|
operators.add(new SimpleMapper("KEY_TO_OBJ", type.getClassType()+".valueOf"));
|
|
|
|
operators.add(new InjectMapper("OBJ_TO_KEY(\\([^)]+\\)|\\S)", "%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-12 02:13:54 +01:00
|
|
|
operators.add(new SimpleMapper("JAVA_CONSUMER", type.isPrimitiveBlocking() ? "" : "java.util.function."+type.getCustomJDKType().getFileType()+"Consumer"));
|
|
|
|
operators.add(new SimpleMapper("CONSUMER", type.getFileType()+"Consumer"));
|
|
|
|
operators.add(new SimpleMapper("ITERATOR", type.getFileType()+"Iterator"));
|
|
|
|
operators.add(new SimpleMapper("ITERABLE", type.getFileType()+"Iterable"));
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public GlobalVariables createFunctions()
|
|
|
|
{
|
|
|
|
operators.add(new SimpleMapper("NEXT()", "next"+type.getNonClassType()+"()"));
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public GlobalVariables createFlags()
|
|
|
|
{
|
|
|
|
flags.add("TYPE_"+type.getCapType());
|
|
|
|
if(!type.needsCustomJDKType())
|
|
|
|
{
|
|
|
|
flags.add("JDK_CONSUMER");
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|