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-30 00:11:40 +01:00
|
|
|
import speiger.src.builder.mappers.LineMapper;
|
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());
|
2020-11-28 15:05:28 +01:00
|
|
|
addSimpleMapper("EMPTY_VALUE", type.getEmptyValue());
|
2020-11-30 00:11:40 +01:00
|
|
|
addSimpleMapper(" KEY_GENERIC_TYPE", type.isObject() ? "<"+type.getKeyType()+">" : "");
|
2020-12-01 02:43:13 +01:00
|
|
|
addSimpleMapper(" NO_GENERIC_TYPE", type.isObject() ? "<?>" : "");
|
2020-11-30 00:11:40 +01:00
|
|
|
addSimpleMapper(" KEY_COMPAREABLE_TYPE", type.isObject() ? "<"+type.getKeyType()+" extends Comparable<T>>" : "");
|
2020-12-01 02:43:13 +01:00
|
|
|
addSimpleMapper(" KEY_SUPER_GENERIC_TYPE", type.isObject() ? "<? super "+type.getKeyType()+">" : "");
|
2020-11-30 00:11:40 +01:00
|
|
|
addSimpleMapper(" GENERIC_BRACES", type.isObject() ? " <"+type.getKeyType()+">" : "");
|
|
|
|
addSimpleMapper(" COMPAREABLE_BRACES", type.isObject() ? " <"+type.getKeyType()+" extends Comparable<T>>" : "");
|
|
|
|
addSimpleMapper("BRACES", type.isObject() ? "<>" : "");
|
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-12-06 09:32:22 +01:00
|
|
|
addSimpleMapper("@PrimitiveDoc", "");
|
|
|
|
addDeprication("@Primitive");
|
2020-11-16 02:55:33 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-12-06 09:32:22 +01:00
|
|
|
|
|
|
|
|
2020-11-16 02:55:33 +01:00
|
|
|
public GlobalVariables createHelperVariables()
|
|
|
|
{
|
2020-12-06 09:32:22 +01:00
|
|
|
addArgumentMapper("EQUALS_KEY_TYPE", type.isObject() ? "Objects.equals(%2$s, %1$s)" : "Objects.equals(%2$s, KEY_TO_OBJ(%1$s))").removeBraces();
|
2020-12-13 20:14:22 +01:00
|
|
|
addInjectMapper("EQUALS_NOT_NULL", type.getComparableValue()+" != "+(type.isPrimitiveBlocking() ? type.getEmptyValue() : (type.needsCast() ? type.getEmptyValue() : "0"))).removeBraces();
|
|
|
|
addInjectMapper("EQUALS_NULL", type.getComparableValue()+" == "+(type.isPrimitiveBlocking() ? type.getEmptyValue() : (type.needsCast() ? type.getEmptyValue() : "0"))).removeBraces();
|
|
|
|
addArgumentMapper("EQUALS_NOT", type.getEquals(true)).removeBraces();
|
|
|
|
addArgumentMapper("EQUALS", type.getEquals(false)).removeBraces();
|
2020-12-19 21:11:21 +01:00
|
|
|
addArgumentMapper("COMPARE_TO_KEY", type.isObject() ? "((Comparable<T>)%1$s).compareTo((T)%2$s)" : type.getClassType()+".compare(%1$s, %2$s)").removeBraces();
|
2020-11-30 00:11:40 +01:00
|
|
|
addArgumentMapper("COMPARE_TO", type.isObject() ? "%1$s.compareTo(%2$s)" : type.getClassType()+".compare(%1$s, %2$s)").removeBraces();
|
|
|
|
addInjectMapper("KEY_TO_OBJ", type.isObject() ? "%s" : type.getClassType()+".valueOf(%s)").removeBraces();
|
|
|
|
addInjectMapper("OBJ_TO_KEY", type.isObject() ? "%s" : "%s."+type.getKeyType()+"Value()").removeBraces();
|
2020-11-16 02:55:33 +01:00
|
|
|
addInjectMapper("CLASS_TO_KEY", "(("+type.getClassType()+")%s)."+type.getKeyType()+"Value()").removeBraces();
|
2020-11-30 00:11:40 +01:00
|
|
|
addSimpleMapper("APPLY", "applyAs"+type.getCustomJDKType().getNonFileType());
|
2020-12-13 20:14:22 +01:00
|
|
|
addInjectMapper("TO_HASH", type.isObject() ? "%s.hashCode()" : type.getClassType()+".hashCode(%s)").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-12-01 02:43:13 +01:00
|
|
|
addSimpleMapper("UNARY_OPERATOR", type.isObject() ? "" : type == ClassType.BOOLEAN ? "BinaryOperator" : type.getCustomJDKType().getFileType()+"UnaryOperator");
|
2020-12-13 20:14:22 +01:00
|
|
|
|
|
|
|
//Final Classes
|
|
|
|
addClassMapper("ARRAY_LIST", "ArrayList");
|
2020-12-13 23:41:09 +01:00
|
|
|
addClassMapper("LINKED_CUSTOM_HASH_SET", "LinkedOpenCustomHashSet");
|
2020-12-13 20:14:22 +01:00
|
|
|
addClassMapper("LINKED_HASH_SET", "LinkedOpenHashSet");
|
2020-12-13 23:41:09 +01:00
|
|
|
addClassMapper("CUSTOM_HASH_SET", "OpenCustomHashSet");
|
2020-12-13 20:14:22 +01:00
|
|
|
addClassMapper("HASH_SET", "OpenHashSet");
|
2020-12-19 21:11:21 +01:00
|
|
|
addClassMapper("RB_TREE_SET", "RBTreeSet");
|
2020-12-13 23:41:09 +01:00
|
|
|
addClassMapper("ARRAY_SET", "ArraySet");
|
2020-12-13 20:14:22 +01:00
|
|
|
|
|
|
|
//Abstract Classes
|
2020-12-13 23:41:09 +01:00
|
|
|
addAbstractMapper("ABSTRACT_COLLECTION", "Abstract%sCollection");
|
|
|
|
addAbstractMapper("ABSTRACT_SET", "Abstract%sSet");
|
|
|
|
addAbstractMapper("ABSTRACT_LIST", "Abstract%sList");
|
2020-12-13 20:14:22 +01:00
|
|
|
addClassMapper("SUB_LIST", "SubList");
|
|
|
|
|
|
|
|
//Helper Classes
|
|
|
|
addClassMapper("LISTS", "Lists");
|
|
|
|
addClassMapper("COLLECTIONS", "Collections");
|
|
|
|
addClassMapper("ARRAYS", "Arrays");
|
|
|
|
addClassMapper("ITERATORS", "Iterators");
|
|
|
|
|
|
|
|
//Interfaces
|
|
|
|
addClassMapper("LIST_ITERATOR", "ListIterator");
|
|
|
|
addClassMapper("BI_ITERATOR", "BidirectionalIterator");
|
|
|
|
addClassMapper("ITERATOR", "Iterator");
|
|
|
|
addClassMapper("ITERABLE", "Iterable");
|
|
|
|
addClassMapper("COLLECTION", "Collection");
|
2020-12-13 23:41:09 +01:00
|
|
|
addClassMapper("LIST_ITER", "ListIter");
|
2020-12-13 20:14:22 +01:00
|
|
|
addClassMapper("LIST", "List");
|
2020-12-19 21:11:21 +01:00
|
|
|
addClassMapper("NAVIGABLE_SET", "NavigableSet");
|
2020-12-13 20:14:22 +01:00
|
|
|
addClassMapper("SORTED_SET", "SortedSet");
|
|
|
|
addClassMapper("SET", "Set");
|
2020-12-13 23:41:09 +01:00
|
|
|
addClassMapper("STRATEGY", "Strategy");
|
2020-12-13 20:14:22 +01:00
|
|
|
addClassMapper("STACK", "Stack");
|
2020-11-30 00:11:40 +01:00
|
|
|
if(type.isObject())
|
|
|
|
{
|
|
|
|
addSimpleMapper("CONSUMER", "Consumer");
|
|
|
|
addSimpleMapper("COMPARATOR", "Comparator");
|
2020-12-01 02:43:13 +01:00
|
|
|
addSimpleMapper("IARRAY", "IObjectArray");
|
2020-11-30 00:11:40 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
addClassMapper("CONSUMER", "Consumer");
|
|
|
|
addClassMapper("COMPARATOR", "Comparator");
|
2020-12-01 02:43:13 +01:00
|
|
|
addFunctionMappers("IARRAY", "I%sArray");
|
2020-11-30 00:11:40 +01:00
|
|
|
}
|
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-28 15:05:28 +01:00
|
|
|
addFunctionMapper("PEEK", "peek");
|
|
|
|
addFunctionMapper("POP", "pop");
|
|
|
|
addFunctionMapper("PUSH", "push");
|
|
|
|
addFunctionMapper("TOP", "top");
|
2020-11-30 00:11:40 +01:00
|
|
|
addFunctionMappers("REPLACE", "replace%ss");
|
2020-12-01 02:43:13 +01:00
|
|
|
addFunctionMappers("SORT", "sort%ss");
|
2020-12-13 20:14:22 +01:00
|
|
|
addFunctionMapper("POLL_FIRST_KEY", "pollFirst");
|
|
|
|
addFunctionMapper("FIRST_KEY", "first");
|
|
|
|
addFunctionMapper("POLL_LAST_KEY", "pollLast");
|
|
|
|
addFunctionMapper("LAST_KEY", "last");
|
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-12-01 02:43:13 +01:00
|
|
|
TemplateProcess process = new TemplateProcess(String.format(fileName+".java", type.getFileType()));
|
2020-11-12 02:13:54 +01:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2020-12-13 23:41:09 +01:00
|
|
|
private void addAbstractMapper(String pattern, String replacement)
|
|
|
|
{
|
|
|
|
operators.add(new SimpleMapper(pattern, String.format(replacement, type.getFileType())));
|
|
|
|
}
|
|
|
|
|
2020-11-16 02:55:33 +01:00
|
|
|
private void addFunctionMapper(String pattern, String replacement)
|
|
|
|
{
|
|
|
|
operators.add(new SimpleMapper(pattern, replacement+type.getNonFileType()));
|
|
|
|
}
|
|
|
|
|
2020-11-30 00:11:40 +01:00
|
|
|
private void addFunctionMappers(String pattern, String replacement)
|
|
|
|
{
|
|
|
|
operators.add(new SimpleMapper(pattern, String.format(replacement, type.getNonFileType())));
|
|
|
|
}
|
|
|
|
|
2020-11-13 03:01:13 +01:00
|
|
|
private void addSimpleMapper(String pattern, String replacement)
|
|
|
|
{
|
|
|
|
operators.add(new SimpleMapper(pattern, replacement));
|
|
|
|
}
|
|
|
|
|
2020-11-30 00:11:40 +01:00
|
|
|
private void addDeprication(String pattern)
|
|
|
|
{
|
|
|
|
if(type == ClassType.OBJECT) operators.add(new LineMapper(pattern));
|
|
|
|
else operators.add(new SimpleMapper(pattern, "@Deprecated"));
|
|
|
|
}
|
|
|
|
|
2020-11-13 03:01:13 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|