From 51f447b0d162e3d2c2679fd1652a124d448d83fa Mon Sep 17 00:00:00 2001 From: Speiger Date: Wed, 11 Nov 2020 00:54:28 +0100 Subject: [PATCH] Variable Overrider Implemented. --- .../base}/ConditionedSegment.java | 6 +- .../builder => builder/base}/Segment.java | 2 +- .../speiger/src/builder/base/Template.java | 97 +++++++++++++++++++ .../builder/conditions/AndCondition.java | 2 +- .../builder/conditions/FlagCondition.java | 2 +- .../builder/conditions/ICondition.java | 2 +- .../builder/conditions/OrCondition.java | 2 +- .../src/builder/example/TestBuilder.java | 86 ++++++++++++++++ .../src/builder/mappers/InjectMapper.java | 44 +++++++++ .../src/builder/mappers/SimpleMapper.java | 22 +++++ .../speiger/src/builder/misc/FileUtils.java | 90 +++++++++++++++++ .../{collections => }/builder/misc/Tuple.java | 2 +- .../src/builder/processor/BuildTask.java | 41 ++++++++ .../builder/processor/TemplateProcess.java | 39 ++++++++ .../builder/processor/TemplateProcessor.java | 81 ++++++++++++++++ .../java/speiger/src/collections/.gitignore | 1 + .../src/collections/builder/Template.java | 75 -------------- .../speiger/assets/collections/cache.bin | 1 + .../collections/templates/List.template | 40 ++------ 19 files changed, 518 insertions(+), 117 deletions(-) rename Primitive-Collections/src/main/java/speiger/src/{collections/builder => builder/base}/ConditionedSegment.java (94%) rename Primitive-Collections/src/main/java/speiger/src/{collections/builder => builder/base}/Segment.java (93%) create mode 100644 Primitive-Collections/src/main/java/speiger/src/builder/base/Template.java rename Primitive-Collections/src/main/java/speiger/src/{collections => }/builder/conditions/AndCondition.java (90%) rename Primitive-Collections/src/main/java/speiger/src/{collections => }/builder/conditions/FlagCondition.java (86%) rename Primitive-Collections/src/main/java/speiger/src/{collections => }/builder/conditions/ICondition.java (96%) rename Primitive-Collections/src/main/java/speiger/src/{collections => }/builder/conditions/OrCondition.java (89%) create mode 100644 Primitive-Collections/src/main/java/speiger/src/builder/example/TestBuilder.java create mode 100644 Primitive-Collections/src/main/java/speiger/src/builder/mappers/InjectMapper.java create mode 100644 Primitive-Collections/src/main/java/speiger/src/builder/mappers/SimpleMapper.java create mode 100644 Primitive-Collections/src/main/java/speiger/src/builder/misc/FileUtils.java rename Primitive-Collections/src/main/java/speiger/src/{collections => }/builder/misc/Tuple.java (82%) create mode 100644 Primitive-Collections/src/main/java/speiger/src/builder/processor/BuildTask.java create mode 100644 Primitive-Collections/src/main/java/speiger/src/builder/processor/TemplateProcess.java create mode 100644 Primitive-Collections/src/main/java/speiger/src/builder/processor/TemplateProcessor.java create mode 100644 Primitive-Collections/src/main/java/speiger/src/collections/.gitignore delete mode 100644 Primitive-Collections/src/main/java/speiger/src/collections/builder/Template.java create mode 100644 Primitive-Collections/src/main/resources/speiger/assets/collections/cache.bin diff --git a/Primitive-Collections/src/main/java/speiger/src/collections/builder/ConditionedSegment.java b/Primitive-Collections/src/main/java/speiger/src/builder/base/ConditionedSegment.java similarity index 94% rename from Primitive-Collections/src/main/java/speiger/src/collections/builder/ConditionedSegment.java rename to Primitive-Collections/src/main/java/speiger/src/builder/base/ConditionedSegment.java index bebea0e..2610c53 100644 --- a/Primitive-Collections/src/main/java/speiger/src/collections/builder/ConditionedSegment.java +++ b/Primitive-Collections/src/main/java/speiger/src/builder/base/ConditionedSegment.java @@ -1,4 +1,4 @@ -package speiger.src.collections.builder; +package speiger.src.builder.base; import java.util.ArrayList; import java.util.List; @@ -6,8 +6,8 @@ import java.util.Set; import java.util.StringJoiner; import java.util.regex.Pattern; -import speiger.src.collections.builder.conditions.ICondition; -import speiger.src.collections.builder.misc.Tuple; +import speiger.src.builder.conditions.ICondition; +import speiger.src.builder.misc.Tuple; public class ConditionedSegment { diff --git a/Primitive-Collections/src/main/java/speiger/src/collections/builder/Segment.java b/Primitive-Collections/src/main/java/speiger/src/builder/base/Segment.java similarity index 93% rename from Primitive-Collections/src/main/java/speiger/src/collections/builder/Segment.java rename to Primitive-Collections/src/main/java/speiger/src/builder/base/Segment.java index 1d94838..51addc0 100644 --- a/Primitive-Collections/src/main/java/speiger/src/collections/builder/Segment.java +++ b/Primitive-Collections/src/main/java/speiger/src/builder/base/Segment.java @@ -1,4 +1,4 @@ -package speiger.src.collections.builder; +package speiger.src.builder.base; import java.util.ArrayList; import java.util.List; diff --git a/Primitive-Collections/src/main/java/speiger/src/builder/base/Template.java b/Primitive-Collections/src/main/java/speiger/src/builder/base/Template.java new file mode 100644 index 0000000..7ff7a7b --- /dev/null +++ b/Primitive-Collections/src/main/java/speiger/src/builder/base/Template.java @@ -0,0 +1,97 @@ +package speiger.src.builder.base; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.StringJoiner; +import java.util.function.UnaryOperator; + +import speiger.src.builder.misc.FileUtils; + +public class Template +{ + String fileName; + String textFile; + List segments; + + public Template(String fileName, String textFile, List segments) + { + this.fileName = fileName; + this.textFile = textFile; + this.segments = segments; + } + + public String getFileName() + { + return fileName; + } + + public String build(Set parsePool, List> mappers) + { + StringBuilder builder = new StringBuilder(textFile); + for(int i = 0,offset=0,m=segments.size();i parsePool = new HashSet<>(); + parsePool.add("DEPEND"); + parsePool.add("SUB_TEST"); + parsePool.add("TEST_0"); + parsePool.add("TEST_1"); + parsePool.add("TEST_2"); + System.out.println(path.getFileName().toString()); + System.out.println(template.build(parsePool, Collections.emptyList())); + } + catch(IOException e) + { + e.printStackTrace(); + } + } + + public static Template parse(Path file) throws IOException + { + List segments = new ArrayList(); + StringJoiner joiner = new StringJoiner("\n"); + List lines = Files.readAllLines(file); + for(int i = 0;i>> data; + + 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\\")); + } + + @Override + protected boolean isFileValid(Path fileName) + { + return true; + } + + @Override + protected void init() + { + data = new LinkedHashMap<>(); + data.put("Byte", createForType("byte", "Byte", "Byte")); + data.put("Short", createForType("short", "Short", "Short")); + data.put("Int", createForType("int", "Integer", "Int")); + data.put("Long", createForType("long", "Long", "Long")); + data.put("Float", createForType("float", "Float", "Float")); + data.put("Double", createForType("double", "Double", "Double")); + data.put("Object", createForType("Object", "T", "Object")); + } + + private List> createForType(String lowercase, String upperCase, String classType) + { + List> list = new ArrayList<>(); + list.add(new SimpleMapper("LIST", classType+"List")); + list.add(new SimpleMapper("CLASS_TYPE", upperCase)); + list.add(new SimpleMapper("KEY_TYPE", lowercase)); + list.add(new SimpleMapper("KEY_GENERIC_TYPE", "")); + return list; + } + + @Override + public void createProcesses(String name, Consumer acceptor) + { + for(Entry>> entry : data.entrySet()) + { + TemplateProcess process = new TemplateProcess(entry.getKey()+name+".java", Paths.get("")); + process.addMappers(entry.getValue()); + if(entry.getKey().equals("Object")) + { + process.addFlags("OBJECT"); + } + acceptor.accept(process); + } + } + + public static void main(String...args) + { + try + { + new TestBuilder().process(true); + } + catch(InterruptedException e) + { + e.printStackTrace(); + } + catch(IOException e) + { + e.printStackTrace(); + } + } +} diff --git a/Primitive-Collections/src/main/java/speiger/src/builder/mappers/InjectMapper.java b/Primitive-Collections/src/main/java/speiger/src/builder/mappers/InjectMapper.java new file mode 100644 index 0000000..315838f --- /dev/null +++ b/Primitive-Collections/src/main/java/speiger/src/builder/mappers/InjectMapper.java @@ -0,0 +1,44 @@ +package speiger.src.builder.mappers; + +import java.util.function.UnaryOperator; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class InjectMapper implements UnaryOperator +{ + Pattern pattern; + String replacement; + boolean removeBraces; + + public InjectMapper(String pattern, String replacement) + { + this.pattern = Pattern.compile(pattern); + this.replacement = replacement; + } + + public InjectMapper removeBraces() + { + removeBraces = true; + return this; + } + + @Override + public String apply(String t) + { + Matcher matcher = pattern.matcher(t); + if(matcher.find()) + { + StringBuffer buffer = new StringBuffer(); + do { matcher.appendReplacement(buffer, String.format(replacement, getString(matcher.group(1)))); + } while (matcher.find()); + matcher.appendTail(buffer); + return buffer.toString(); + } + return t; + } + + private String getString(String s) + { + return removeBraces ? s.substring(1, s.length() - 1) : s; + } +} diff --git a/Primitive-Collections/src/main/java/speiger/src/builder/mappers/SimpleMapper.java b/Primitive-Collections/src/main/java/speiger/src/builder/mappers/SimpleMapper.java new file mode 100644 index 0000000..7feff31 --- /dev/null +++ b/Primitive-Collections/src/main/java/speiger/src/builder/mappers/SimpleMapper.java @@ -0,0 +1,22 @@ +package speiger.src.builder.mappers; + +import java.util.function.UnaryOperator; +import java.util.regex.Pattern; + +public class SimpleMapper implements UnaryOperator +{ + Pattern pattern; + String replacement; + + public SimpleMapper(String pattern, String replacement) + { + this.pattern = Pattern.compile(pattern); + this.replacement = replacement; + } + + @Override + public String apply(String t) + { + return pattern.matcher(t).replaceAll(replacement); + } +} diff --git a/Primitive-Collections/src/main/java/speiger/src/builder/misc/FileUtils.java b/Primitive-Collections/src/main/java/speiger/src/builder/misc/FileUtils.java new file mode 100644 index 0000000..d4b0945 --- /dev/null +++ b/Primitive-Collections/src/main/java/speiger/src/builder/misc/FileUtils.java @@ -0,0 +1,90 @@ +package speiger.src.builder.misc; + +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; +import java.security.MessageDigest; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Map.Entry; + +public class FileUtils +{ + public static boolean isValid(Path path, Map existing) + { + String s = existing.get(getFileName(path)); + return s == null || !s.equals(FileUtils.getMD5String(path)); + } + + public static Map loadMappings(Path dataFolder) throws IOException + { + Map result = new LinkedHashMap<>(); + dataFolder = dataFolder.resolve("cache.bin"); + if(Files.notExists(dataFolder)) + { + return result; + } + for(String s : Files.readAllLines(dataFolder)) + { + String[] array = s.split("="); + if(array.length == 2) + { + result.put(array[0], array[1]); + } + } + return result; + } + + public static void saveMappings(Map mappings, Path dataFolder) + { + try(BufferedWriter writer = Files.newBufferedWriter(dataFolder.resolve("cache.bin"), StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.DSYNC)) + { + for(Entry entry : mappings.entrySet()) + { + writer.write(entry.getKey()+"="+entry.getValue()); + writer.newLine(); + } + writer.flush(); + } + catch(Exception e) + { + e.printStackTrace(); + } + } + + + public static String getFileName(Path path) + { + String name = path.getFileName().toString(); + int index = name.indexOf("."); + return index == -1 ? name : name.substring(0, index); + } + + public static String getMD5String(Path path) + { + try(InputStream stream = Files.newInputStream(path)) + { + MessageDigest digest = MessageDigest.getInstance("MD5"); + byte[] byteArray = new byte[2048]; + int bytesCount = 0; + while((bytesCount = stream.read(byteArray)) != -1) + { + digest.update(byteArray, 0, bytesCount); + } + byte[] bytes = digest.digest(); + StringBuilder sb = new StringBuilder(); + for(int i = 0;i < bytes.length;i++) + { + sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1)); + } + return sb.toString(); + } + catch(Exception e) + { + return null; + } + } +} diff --git a/Primitive-Collections/src/main/java/speiger/src/collections/builder/misc/Tuple.java b/Primitive-Collections/src/main/java/speiger/src/builder/misc/Tuple.java similarity index 82% rename from Primitive-Collections/src/main/java/speiger/src/collections/builder/misc/Tuple.java rename to Primitive-Collections/src/main/java/speiger/src/builder/misc/Tuple.java index fda1836..0e76e2c 100644 --- a/Primitive-Collections/src/main/java/speiger/src/collections/builder/misc/Tuple.java +++ b/Primitive-Collections/src/main/java/speiger/src/builder/misc/Tuple.java @@ -1,4 +1,4 @@ -package speiger.src.collections.builder.misc; +package speiger.src.builder.misc; public class Tuple { diff --git a/Primitive-Collections/src/main/java/speiger/src/builder/processor/BuildTask.java b/Primitive-Collections/src/main/java/speiger/src/builder/processor/BuildTask.java new file mode 100644 index 0000000..ea8004e --- /dev/null +++ b/Primitive-Collections/src/main/java/speiger/src/builder/processor/BuildTask.java @@ -0,0 +1,41 @@ +package speiger.src.builder.processor; + +import java.io.BufferedWriter; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; + +import speiger.src.builder.base.Template; + +public class BuildTask implements Runnable +{ + Path basePath; + Template template; + TemplateProcess process; + + public BuildTask(Path basePath, Template template, TemplateProcess process) + { + super(); + this.basePath = basePath; + this.template = template; + this.process = process; + } + + @Override + public void run() + { + String s = template.build(process.parsePool, process.mappers); + Path path = basePath.resolve(process.path).resolve(process.fileName); + try(BufferedWriter writer = Files.newBufferedWriter(path, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE, StandardOpenOption.SYNC)) + { + writer.write(s); + writer.flush(); + System.out.println("Created: "+process.fileName); + } + catch(Exception e) + { + e.printStackTrace(); + } + } + +} diff --git a/Primitive-Collections/src/main/java/speiger/src/builder/processor/TemplateProcess.java b/Primitive-Collections/src/main/java/speiger/src/builder/processor/TemplateProcess.java new file mode 100644 index 0000000..f6bd586 --- /dev/null +++ b/Primitive-Collections/src/main/java/speiger/src/builder/processor/TemplateProcess.java @@ -0,0 +1,39 @@ +package speiger.src.builder.processor; + +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.function.UnaryOperator; + +public class TemplateProcess +{ + Path path; + String fileName; + Set parsePool = new HashSet<>(); + List> mappers = new ArrayList<>(); + + public TemplateProcess(String fileName, Path path) + { + this.fileName = fileName; + this.path = path; + } + + public void addFlags(String...flags) + { + parsePool.addAll(Arrays.asList(flags)); + } + + public void addMapper(UnaryOperator mapper) + { + mappers.add(mapper); + } + + public void addMappers(Collection> mappers) + { + this.mappers.addAll(mappers); + } +} diff --git a/Primitive-Collections/src/main/java/speiger/src/builder/processor/TemplateProcessor.java b/Primitive-Collections/src/main/java/speiger/src/builder/processor/TemplateProcessor.java new file mode 100644 index 0000000..5394f29 --- /dev/null +++ b/Primitive-Collections/src/main/java/speiger/src/builder/processor/TemplateProcessor.java @@ -0,0 +1,81 @@ +package speiger.src.builder.processor; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.Map; +import java.util.concurrent.Executors; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; +import java.util.stream.Collectors; + +import speiger.src.builder.base.Template; +import speiger.src.builder.misc.FileUtils; + +public abstract class TemplateProcessor +{ + Path sourceFolder; + Path outputFolder; + Path dataFolder; + + public TemplateProcessor(Path sourceFolder, Path outputFolder, Path dataFolder) + { + this.sourceFolder = sourceFolder; + this.outputFolder = outputFolder; + this.dataFolder = dataFolder; + init(); + } + + protected abstract void init(); + + protected abstract boolean isFileValid(Path fileName); + + public abstract void createProcesses(String fileName, Consumer process); + + public boolean process(boolean force) throws IOException, InterruptedException + { + Map existing = FileUtils.loadMappings(dataFolder); + List pathsLeft = Files.walk(sourceFolder).filter(Files::isRegularFile).filter(T -> isFileValid(T.getFileName())).filter(T -> force || FileUtils.isValid(T, existing)).collect(Collectors.toList()); + if(pathsLeft.isEmpty() && !force) + { + System.out.println("Nothing has changed"); + return false; + } + ThreadPoolExecutor service = (ThreadPoolExecutor)Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); + service.setKeepAliveTime(10, TimeUnit.MILLISECONDS); + service.allowCoreThreadTimeOut(true); + service.submit(() -> { + for(int i = 0,m=pathsLeft.size();i service.execute(new BuildTask(outputFolder, template, T))); + } + catch(Exception e) + { + e.printStackTrace(); + continue; + } + } + }); + long start = System.currentTimeMillis(); + System.out.println("Started Tasks"); + for(int i = 0,m=pathsLeft.size();i 0) + { + Thread.sleep(10); + } + System.out.println("Finished Tasks: "+(System.currentTimeMillis() - start)+"ms"); + FileUtils.saveMappings(existing, dataFolder); + System.out.print("Saved Changes"); + return true; + } +} diff --git a/Primitive-Collections/src/main/java/speiger/src/collections/.gitignore b/Primitive-Collections/src/main/java/speiger/src/collections/.gitignore new file mode 100644 index 0000000..3bde5c3 --- /dev/null +++ b/Primitive-Collections/src/main/java/speiger/src/collections/.gitignore @@ -0,0 +1 @@ +/example/ diff --git a/Primitive-Collections/src/main/java/speiger/src/collections/builder/Template.java b/Primitive-Collections/src/main/java/speiger/src/collections/builder/Template.java deleted file mode 100644 index 837a297..0000000 --- a/Primitive-Collections/src/main/java/speiger/src/collections/builder/Template.java +++ /dev/null @@ -1,75 +0,0 @@ -package speiger.src.collections.builder; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.StringJoiner; - -public class Template -{ - String textFile; - List segments; - - public Template(String textFile, List segments) - { - this.textFile = textFile; - this.segments = segments; - } - - public String build(Set parsePool) - { - StringBuilder builder = new StringBuilder(textFile); - for(int i = 0,offset=0,m=segments.size();i parsePool = new HashSet<>(); - parsePool.add("DEPEND"); - parsePool.add("SUB_TEST"); - parsePool.add("TEST_0"); - parsePool.add("TEST_1"); - parsePool.add("TEST_2"); - System.out.println(template.build(parsePool)); - } - catch(IOException e) - { - e.printStackTrace(); - } - } - - public static Template parse(Path file) throws IOException - { - List segments = new ArrayList(); - StringJoiner joiner = new StringJoiner("\n"); - List lines = Files.readAllLines(file); - for(int i = 0;i file, int index) - { - return 0; - } -} diff --git a/Primitive-Collections/src/main/resources/speiger/assets/collections/cache.bin b/Primitive-Collections/src/main/resources/speiger/assets/collections/cache.bin new file mode 100644 index 0000000..bda9a21 --- /dev/null +++ b/Primitive-Collections/src/main/resources/speiger/assets/collections/cache.bin @@ -0,0 +1 @@ +List=81eefe88c5fd5080b139b1b85e11f85d diff --git a/Primitive-Collections/src/main/resources/speiger/assets/collections/templates/List.template b/Primitive-Collections/src/main/resources/speiger/assets/collections/templates/List.template index 67d5b10..cbb4069 100644 --- a/Primitive-Collections/src/main/resources/speiger/assets/collections/templates/List.template +++ b/Primitive-Collections/src/main/resources/speiger/assets/collections/templates/List.template @@ -1,37 +1,11 @@ -package speiger.src.collections.test +package speiger.src.collections.example; -public class List +import java.util.List; + +public interface LISTKEY_GENERIC_TYPE extends List { - public List() - { - } -#if TEST_FLAG - - public void addOne() - { - } -#else if TESTING_FLAG - - public void addTwo() - { -#if DEPEND - int i = 0; -#endif -#if SUB_TEST - i += 100; -#endif - } -#else if TEST_0 && !TEST_1 || TEST_2 - - public void addThree() - { - - } -#else - - public void addFour() - { - - } +#if !OBJECT + public void add(KEY_TYPE e); #endif + public default boolean add(CLASS_TYPE e) {return true;} } \ No newline at end of file