diff --git a/src/main/java/speiger/src/builder/example/ClassType.java b/src/main/java/speiger/src/builder/example/ClassType.java new file mode 100644 index 00000000..3e147dd1 --- /dev/null +++ b/src/main/java/speiger/src/builder/example/ClassType.java @@ -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; + } + } +} diff --git a/src/main/java/speiger/src/builder/example/GlobalVariables.java b/src/main/java/speiger/src/builder/example/GlobalVariables.java index 99df5bbd..f3a5cb0f 100644 --- a/src/main/java/speiger/src/builder/example/GlobalVariables.java +++ b/src/main/java/speiger/src/builder/example/GlobalVariables.java @@ -1,44 +1,95 @@ package speiger.src.builder.example; +import java.nio.file.Path; import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; import java.util.function.UnaryOperator; +import speiger.src.builder.mappers.InjectMapper; import speiger.src.builder.mappers.SimpleMapper; import speiger.src.builder.processor.TemplateProcess; public class GlobalVariables { - String fileName; - String folderName; List> operators = new ArrayList<>(); Set flags = new LinkedHashSet<>(); + ClassType type; - public GlobalVariables(String fileName, String folderName) + public GlobalVariables(ClassType type) { - this.fileName = fileName; - this.folderName = folderName; + this.type = type; } - public GlobalVariables createInitials(String classType, String keyType) + public GlobalVariables createVariables() { - operators.add(new SimpleMapper("CLASS_TYPE", classType)); - operators.add(new SimpleMapper("KEY_TYPE", keyType)); + 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()); 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; } 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 + { + String before; -// TemplateProcess process = new TemplateProcess(entry.getKey()+name+".java", Paths.get("")); - return null; + public PathBuilder(String before) + { + this.before = before; + } + + @Override + public Path apply(Path t) + { + return t.subpath(0, 6).resolve(before).resolve(t.subpath(6, t.getNameCount())); + } } } diff --git a/src/main/java/speiger/src/builder/example/TestBuilder.java b/src/main/java/speiger/src/builder/example/TestBuilder.java index a380ce75..924c3385 100644 --- a/src/main/java/speiger/src/builder/example/TestBuilder.java +++ b/src/main/java/speiger/src/builder/example/TestBuilder.java @@ -4,27 +4,25 @@ import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.EnumSet; +import java.util.HashMap; import java.util.List; -import java.util.Map.Entry; +import java.util.Map; 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.TemplateProcessor; public class TestBuilder extends TemplateProcessor { - public static final String[] KEY_TYPE = new String[]{"byte", "short", "int", "long", "float", "double", "T"}; - public static final String[] CLASS_TYPE = new String[]{"Byte", "Short", "Integer", "Long", "Float", "Double", "Object"}; - public static final String[] FILE_TYPE = new String[]{"Byte", "Short", "Int", "Long", "Float", "Double", "Object"}; + Map> blocked = new HashMap>(); + public static final ClassType[] TYPE = ClassType.values(); List varibles = new ArrayList(); 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 @@ -43,59 +41,45 @@ public class TestBuilder extends TemplateProcessor protected void init() { varibles.clear(); - for(int i = 0,m=KEY_TYPE.length;i> createForType(String lowercase, String upperCase, String classType, String consumer) - { - Files - List> 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 public void createProcesses(String name, Consumer acceptor) { - for(Entry>> entry : data.entrySet()) + EnumSet types = blocked.get(name); + for(int i = 0,m=varibles.size();i public SimpleMapper(String pattern, String replacement) { - this.pattern = Pattern.compile(pattern); + this.pattern = Pattern.compile(pattern, Pattern.LITERAL); this.replacement = replacement; } diff --git a/src/main/java/speiger/src/builder/processor/BuildTask.java b/src/main/java/speiger/src/builder/processor/BuildTask.java index e5a07b39..6517ea1b 100644 --- a/src/main/java/speiger/src/builder/processor/BuildTask.java +++ b/src/main/java/speiger/src/builder/processor/BuildTask.java @@ -24,7 +24,7 @@ public class BuildTask implements Runnable public void run() { 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 { Files.createDirectories(path.getParent()); diff --git a/src/main/java/speiger/src/builder/processor/TemplateProcess.java b/src/main/java/speiger/src/builder/processor/TemplateProcess.java index c2b07569..567364bc 100644 --- a/src/main/java/speiger/src/builder/processor/TemplateProcess.java +++ b/src/main/java/speiger/src/builder/processor/TemplateProcess.java @@ -1,5 +1,6 @@ package speiger.src.builder.processor; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -10,7 +11,7 @@ import java.util.function.UnaryOperator; public class TemplateProcess { - UnaryOperator pathBuilder; + UnaryOperator pathBuilder; String fileName; Set parsePool = new HashSet<>(); List> mappers = new ArrayList<>(); @@ -20,11 +21,21 @@ public class TemplateProcess this.fileName = fileName; } + public void setPathBuilder(UnaryOperator pathBuilder) + { + this.pathBuilder = pathBuilder; + } + public void addFlags(String...flags) { parsePool.addAll(Arrays.asList(flags)); } + public void addFlags(Collection flags) + { + parsePool.addAll(flags); + } + public void addMapper(UnaryOperator mapper) { mappers.add(mapper); diff --git a/src/main/java/speiger/src/builder/processor/TemplateProcessor.java b/src/main/java/speiger/src/builder/processor/TemplateProcessor.java index 035695b3..71071c3b 100644 --- a/src/main/java/speiger/src/builder/processor/TemplateProcessor.java +++ b/src/main/java/speiger/src/builder/processor/TemplateProcessor.java @@ -27,6 +27,7 @@ public abstract class TemplateProcessor this.sourceFolder = sourceFolder; this.outputFolder = outputFolder; this.dataFolder = dataFolder; + System.out.println(outputFolder.toAbsolutePath().toString()); } protected abstract void init(); diff --git a/src/main/java/speiger/src/collections/utils/SanityChecks.java b/src/main/java/speiger/src/collections/utils/SanityChecks.java new file mode 100644 index 00000000..18f64ff2 --- /dev/null +++ b/src/main/java/speiger/src/collections/utils/SanityChecks.java @@ -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; + } +} diff --git a/src/main/resources/speiger/assets/collections/cache.bin b/src/main/resources/speiger/assets/collections/cache.bin deleted file mode 100644 index 8bf93c70..00000000 --- a/src/main/resources/speiger/assets/collections/cache.bin +++ /dev/null @@ -1,2 +0,0 @@ -List=8346cdbb0624aa07f1c54d00f765cbd5 -Consumer=e0112061d850a9ea6049cfe6571e2750 diff --git a/src/main/resources/speiger/assets/collections/templates/collections/Iterable.template b/src/main/resources/speiger/assets/collections/templates/collections/Iterable.template new file mode 100644 index 00000000..2a5f4511 --- /dev/null +++ b/src/main/resources/speiger/assets/collections/templates/collections/Iterable.template @@ -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 +{ + @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 action) { + Objects.requireNonNull(action); + iterator().forEachRemaining(action); + } +#endif +} diff --git a/src/main/resources/speiger/assets/collections/templates/collections/Iterator.template b/src/main/resources/speiger/assets/collections/templates/collections/Iterator.template new file mode 100644 index 00000000..e90c25d2 --- /dev/null +++ b/src/main/resources/speiger/assets/collections/templates/collections/Iterator.template @@ -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 +{ +#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 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; + } +} \ No newline at end of file diff --git a/src/main/resources/speiger/assets/collections/templates/functions/Consumer.template b/src/main/resources/speiger/assets/collections/templates/functions/Consumer.template index 4b8cbe93..7a0ee0e2 100644 --- a/src/main/resources/speiger/assets/collections/templates/functions/Consumer.template +++ b/src/main/resources/speiger/assets/collections/templates/functions/Consumer.template @@ -1,20 +1,45 @@ -package speiger.src.collections.example.functions; +package speiger.src.collections.PACKAGE.functions; import java.util.Objects; import java.util.function.Consumer; +#if !TYPE_BOOLEAN +#if !JDK_CONSUMER +import speiger.src.collections.utils.SanityChecks; + +#endif public interface CONSUMER extends Consumer, JAVA_CONSUMER -{ +#else + +public interface CONSUMER extends Consumer +#endif +{ void accept(KEY_TYPE t); - - default void accept(CLASS_TYPE t) { accept(OBJ_TO_KEY(t)); } +#if !JDK_CONSUMER + @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 default CONSUMER andThen(Consumer 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); return T -> {accept(T); after.accept(T);}; } +#endif default CONSUMER andThen(CONSUMER after) { Objects.requireNonNull(after); return T -> {accept(T); after.accept(T);};