Latest Update

- Added: Iterator
- Added: Iterable
- Fixed: Simple Mapper is always literal.
- Fixed: Conusmer bugs.
- Changed: Path designer is now a function.
- Removed: Cache from the repo. Not needed.
This commit is contained in:
Speiger 2020-11-12 02:13:54 +01:00
parent 151e466b87
commit 8fcf370e87
12 changed files with 316 additions and 67 deletions

View File

@ -0,0 +1,81 @@
package speiger.src.builder.example;
public enum ClassType
{
BOOLEAN("boolean", "Boolean", "Boolean", "booleans", "BOOLEAN"),
BYTE("byte", "Byte", "Byte", "bytes", "BYTES"),
SHORT("short", "Short", "Short", "shorts", "SHORT"),
CHAR("char", "Character", "Char", "chars", "CHAR"),
INT("int", "Integer", "Int", "ints", "INT"),
LONG("long", "Long", "Long", "longs", "LONG"),
FLOAT("float", "Float", "Float", "floats", "FLOAT"),
DOUBLE("double", "Double", "Double", "doubles", "DOUBLE"),
OBJECT("T", "T", "Object", "objects", "OBJECT");
String keyType;
String classType;
String fileType;
String pathType;
String capType;
private ClassType(String keyType, String classType, String fileType, String pathType, String capType)
{
this.keyType = keyType;
this.classType = classType;
this.fileType = fileType;
this.pathType = pathType;
this.capType = capType;
}
public String getKeyType()
{
return keyType;
}
public String getClassType()
{
return classType;
}
public String getNonClassType()
{
return this == OBJECT ? "" : classType;
}
public String getFileType()
{
return fileType;
}
public String getPathType()
{
return pathType;
}
public String getCapType()
{
return capType;
}
public boolean isPrimitiveBlocking()
{
return this == BOOLEAN || this == OBJECT;
}
public boolean needsCustomJDKType()
{
return this == BYTE || this == SHORT || this == CHAR || this == FLOAT;
}
public ClassType getCustomJDKType()
{
switch(this)
{
case BYTE: return INT;
case CHAR: return INT;
case FLOAT: return DOUBLE;
case SHORT: return INT;
default: return this;
}
}
}

View File

@ -1,44 +1,95 @@
package speiger.src.builder.example; package speiger.src.builder.example;
import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.function.UnaryOperator; import java.util.function.UnaryOperator;
import speiger.src.builder.mappers.InjectMapper;
import speiger.src.builder.mappers.SimpleMapper; import speiger.src.builder.mappers.SimpleMapper;
import speiger.src.builder.processor.TemplateProcess; import speiger.src.builder.processor.TemplateProcess;
public class GlobalVariables public class GlobalVariables
{ {
String fileName;
String folderName;
List<UnaryOperator<String>> operators = new ArrayList<>(); List<UnaryOperator<String>> operators = new ArrayList<>();
Set<String> flags = new LinkedHashSet<>(); Set<String> flags = new LinkedHashSet<>();
ClassType type;
public GlobalVariables(String fileName, String folderName) public GlobalVariables(ClassType type)
{ {
this.fileName = fileName; this.type = type;
this.folderName = folderName;
} }
public GlobalVariables createInitials(String classType, String keyType) public GlobalVariables createVariables()
{ {
operators.add(new SimpleMapper("CLASS_TYPE", classType)); operators.add(new SimpleMapper("PACKAGE", type.getPathType()));
operators.add(new SimpleMapper("KEY_TYPE", keyType)); 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());
return this; return this;
} }
public GlobalVariables createClassTypes(String fileType) public GlobalVariables createClassTypes()
{ {
operators.add(new SimpleMapper("CONSUMER", fileType+"Consumer")); 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");
}
return this; return this;
} }
public TemplateProcess create(String fileName) public TemplateProcess create(String fileName)
{ {
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;
// TemplateProcess process = new TemplateProcess(entry.getKey()+name+".java", Paths.get("")); public PathBuilder(String before)
return null; {
this.before = before;
}
@Override
public Path apply(Path t)
{
return t.subpath(0, 6).resolve(before).resolve(t.subpath(6, t.getNameCount()));
}
} }
} }

View File

@ -4,27 +4,25 @@ import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map.Entry; import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.UnaryOperator;
import speiger.src.builder.mappers.InjectMapper;
import speiger.src.builder.mappers.SimpleMapper;
import speiger.src.builder.processor.TemplateProcess; import speiger.src.builder.processor.TemplateProcess;
import speiger.src.builder.processor.TemplateProcessor; import speiger.src.builder.processor.TemplateProcessor;
public class TestBuilder extends TemplateProcessor public class TestBuilder extends TemplateProcessor
{ {
public static final String[] KEY_TYPE = new String[]{"byte", "short", "int", "long", "float", "double", "T"}; Map<String, EnumSet<ClassType>> blocked = new HashMap<String, EnumSet<ClassType>>();
public static final String[] CLASS_TYPE = new String[]{"Byte", "Short", "Integer", "Long", "Float", "Double", "Object"}; public static final ClassType[] TYPE = ClassType.values();
public static final String[] FILE_TYPE = new String[]{"Byte", "Short", "Int", "Long", "Float", "Double", "Object"};
List<GlobalVariables> varibles = new ArrayList<GlobalVariables>(); List<GlobalVariables> varibles = new ArrayList<GlobalVariables>();
public TestBuilder() public TestBuilder()
{ {
super(Paths.get("src\\main\\resources\\speiger\\assets\\collections\\templates\\"), Paths.get("src\\main\\java\\speiger\\src\\collections\\example\\"), Paths.get("src\\main\\resources\\speiger\\assets\\collections\\")); super(Paths.get("src\\main\\resources\\speiger\\assets\\collections\\templates\\"), Paths.get("src\\main\\java\\speiger\\src\\collections\\"), Paths.get("src\\main\\resources\\speiger\\assets\\collections\\"));
} }
@Override @Override
@ -43,59 +41,45 @@ public class TestBuilder extends TemplateProcessor
protected void init() protected void init()
{ {
varibles.clear(); varibles.clear();
for(int i = 0,m=KEY_TYPE.length;i<m;i++) for(ClassType clzType : TYPE)
{ {
GlobalVariables type = new GlobalVariables(FILE_TYPE[i]); GlobalVariables type = new GlobalVariables(clzType);
type.createInitials(CLASS_TYPE[i], KEY_TYPE[i]); type.createFlags();
type.createClassTypes(FILE_TYPE[i]); type.createVariables();
type.createClassTypes();
type.createFunctions();
varibles.add(type); varibles.add(type);
} }
blocked.put("Consumer", EnumSet.of(ClassType.OBJECT));
} }
private List<UnaryOperator<String>> createForType(String lowercase, String upperCase, String classType, String consumer)
{
Files
List<UnaryOperator<String>> list = new ArrayList<>();
list.add(new SimpleMapper("JAVA_CONSUMER", "java.util.function."+consumer));
list.add(new SimpleMapper("CONSUMER", classType+"Consumer"));
list.add(new SimpleMapper("CLASS_TYPE", upperCase));
list.add(new SimpleMapper("KEY_TYPE", lowercase));
list.add(new InjectMapper("OBJ_TO_KEY(\\([^)]+\\)|\\S)", "%s."+lowercase+"Value()").removeBraces());
return list;
}
@Override @Override
public void createProcesses(String name, Consumer<TemplateProcess> acceptor) public void createProcesses(String name, Consumer<TemplateProcess> acceptor)
{ {
for(Entry<String, List<UnaryOperator<String>>> entry : data.entrySet()) EnumSet<ClassType> types = blocked.get(name);
for(int i = 0,m=varibles.size();i<m;i++)
{ {
TemplateProcess process = new TemplateProcess(entry.getKey()+name+".java", Paths.get("")); GlobalVariables type = varibles.get(i);
process.addMappers(entry.getValue()); if(types == null || !types.contains(type.getType()))
acceptor.accept(process); {
acceptor.accept(type.create(name));
}
} }
} }
public static void main(String...args) public static void main(String...args)
{ {
Path path = Paths.get("").toAbsolutePath(); try
System.out.println(path.toString());
for(int i = 0,m=path.getNameCount();i<m;i++)
{ {
System.out.println(path.getName(i).toString()); new TestBuilder().process(true);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
} }
// try
// {
// new TestBuilder().process(true);
// }
// catch(InterruptedException e)
// {
// e.printStackTrace();
// }
// catch(IOException e)
// {
// e.printStackTrace();
// }
} }
} }

View File

@ -10,7 +10,7 @@ public class SimpleMapper implements UnaryOperator<String>
public SimpleMapper(String pattern, String replacement) public SimpleMapper(String pattern, String replacement)
{ {
this.pattern = Pattern.compile(pattern); this.pattern = Pattern.compile(pattern, Pattern.LITERAL);
this.replacement = replacement; this.replacement = replacement;
} }

View File

@ -24,7 +24,7 @@ public class BuildTask implements Runnable
public void run() public void run()
{ {
String s = template.build(process.parsePool, process.mappers); String s = template.build(process.parsePool, process.mappers);
Path path = basePath.resolve(process.path).resolve(process.fileName); Path path = (process.pathBuilder != null ? process.pathBuilder.apply(basePath) : basePath).resolve(process.fileName);
try try
{ {
Files.createDirectories(path.getParent()); Files.createDirectories(path.getParent());

View File

@ -1,5 +1,6 @@
package speiger.src.builder.processor; package speiger.src.builder.processor;
import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
@ -10,7 +11,7 @@ import java.util.function.UnaryOperator;
public class TemplateProcess public class TemplateProcess
{ {
UnaryOperator pathBuilder; UnaryOperator<Path> pathBuilder;
String fileName; String fileName;
Set<String> parsePool = new HashSet<>(); Set<String> parsePool = new HashSet<>();
List<UnaryOperator<String>> mappers = new ArrayList<>(); List<UnaryOperator<String>> mappers = new ArrayList<>();
@ -20,11 +21,21 @@ public class TemplateProcess
this.fileName = fileName; this.fileName = fileName;
} }
public void setPathBuilder(UnaryOperator<Path> pathBuilder)
{
this.pathBuilder = pathBuilder;
}
public void addFlags(String...flags) public void addFlags(String...flags)
{ {
parsePool.addAll(Arrays.asList(flags)); parsePool.addAll(Arrays.asList(flags));
} }
public void addFlags(Collection<String> flags)
{
parsePool.addAll(flags);
}
public void addMapper(UnaryOperator<String> mapper) public void addMapper(UnaryOperator<String> mapper)
{ {
mappers.add(mapper); mappers.add(mapper);

View File

@ -27,6 +27,7 @@ public abstract class TemplateProcessor
this.sourceFolder = sourceFolder; this.sourceFolder = sourceFolder;
this.outputFolder = outputFolder; this.outputFolder = outputFolder;
this.dataFolder = dataFolder; this.dataFolder = dataFolder;
System.out.println(outputFolder.toAbsolutePath().toString());
} }
protected abstract void init(); protected abstract void init();

View File

@ -0,0 +1,30 @@
package speiger.src.collections.utils;
public class SanityChecks
{
public static byte castToByte(int value)
{
if(value > Byte.MAX_VALUE || value < Byte.MIN_VALUE) throw new IllegalStateException("Value ["+value+"] out of Byte[-128,127] range");
return (byte)value;
}
public static short castToShort(int value)
{
if(value > Short.MAX_VALUE || value < Short.MIN_VALUE) throw new IllegalStateException("Value ["+value+"] out of Short[-32768,32767] range");
return (short)value;
}
public static char castToChar(int value)
{
if(value > Character.MAX_VALUE || value < Character.MIN_VALUE) throw new IllegalStateException("Value ["+value+"] out of Character[0,65535] range");
return (char)value;
}
public static float castToFloat(double value)
{
if(Double.isNaN(value)) return Float.NaN;
if(Double.isInfinite(value)) return value > 0 ? Float.POSITIVE_INFINITY : Float.NEGATIVE_INFINITY;
if(value < -Float.MAX_VALUE || value > Float.MAX_VALUE) throw new IllegalStateException("Value ["+value+"] out of Float range");
return (float)value;
}
}

View File

@ -1,2 +0,0 @@
List=8346cdbb0624aa07f1c54d00f765cbd5
Consumer=e0112061d850a9ea6049cfe6571e2750

View File

@ -0,0 +1,28 @@
package speiger.src.collections.PACKAGE.collections;
#if !TYPE_OBJECT
import java.util.Objects;
import java.util.function.Consumer;
import speiger.src.collections.PACKAGE.functions.CONSUMER;
#endif
public interface ITERABLE KEY_GENERIC_TYPE extends Iterable<CLASS_TYPE>
{
@Override
ITERATOR KEY_GENERIC_TYPE iterator();
#if !TYPE_OBJECT
default void forEach(CONSUMER action) {
Objects.requireNonNull(action);
iterator().forEachRemaining(action);
}
@Deprecated
@Override
default void forEach(Consumer<? super CLASS_TYPE> action) {
Objects.requireNonNull(action);
iterator().forEachRemaining(action);
}
#endif
}

View File

@ -0,0 +1,40 @@
package speiger.src.collections.PACKAGE.collections;
import java.util.Iterator;
#if !TYPE_OBJECT
import java.util.Objects;
import java.util.function.Consumer;
import speiger.src.collections.PACKAGE.functions.CONSUMER;
#endif
public interface ITERATOR KEY_GENERIC_TYPE extends Iterator<CLASS_TYPE>
{
#if !TYPE_OBJECT
public KEY_TYPE NEXT();
@Deprecated
@Override
public default CLASS_TYPE next() { return KEY_TO_OBJ(next()); }
public default void forEachRemaining(CONSUMER action) {
Objects.requireNonNull(action);
while(hasNext()) { action.accept(NEXT()); }
}
@Deprecated
@Override
default void forEachRemaining(Consumer<? super CLASS_TYPE> action) {
Objects.requireNonNull(action);
forEachRemaining(action::accept);
}
#endif
default int skip(int amount) {
if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
int temp = amount;
for(;amount > 0 && hasNext();NEXT(), amount--);
return temp - amount;
}
}

View File

@ -1,20 +1,45 @@
package speiger.src.collections.example.functions; package speiger.src.collections.PACKAGE.functions;
import java.util.Objects; import java.util.Objects;
import java.util.function.Consumer; import java.util.function.Consumer;
#if !TYPE_BOOLEAN
#if !JDK_CONSUMER
import speiger.src.collections.utils.SanityChecks;
#endif
public interface CONSUMER extends Consumer<CLASS_TYPE>, JAVA_CONSUMER public interface CONSUMER extends Consumer<CLASS_TYPE>, JAVA_CONSUMER
{ #else
public interface CONSUMER extends Consumer<CLASS_TYPE>
#endif
{
void accept(KEY_TYPE t); void accept(KEY_TYPE t);
#if !JDK_CONSUMER
default void accept(CLASS_TYPE t) { accept(OBJ_TO_KEY(t)); }
@Override
default void accept(JAVA_TYPE t) { accept(SanityChecks.SANITY_CAST(t)); }
#endif
@Override
default void accept(CLASS_TYPE t) { accept(OBJ_TO_KEY(t)); }
@Deprecated
@Override @Override
default CONSUMER andThen(Consumer<? super CLASS_TYPE> after) { default CONSUMER andThen(Consumer<? super CLASS_TYPE> after) {
Objects.requireNonNull(after);
return T -> {accept(T); after.accept(KEY_TO_OBJ(T));};
}
#if !TYPE_BOOLEAN
@Deprecated
@Override
default CONSUMER andThen(JAVA_CONSUMER after) {
Objects.requireNonNull(after); Objects.requireNonNull(after);
return T -> {accept(T); after.accept(T);}; return T -> {accept(T); after.accept(T);};
} }
#endif
default CONSUMER andThen(CONSUMER after) { default CONSUMER andThen(CONSUMER after) {
Objects.requireNonNull(after); Objects.requireNonNull(after);
return T -> {accept(T); after.accept(T);}; return T -> {accept(T); after.accept(T);};