From c61c8a78067de5e817b0235880f0bf5c0e844580 Mon Sep 17 00:00:00 2001 From: Speiger Date: Fri, 13 Nov 2020 03:01:13 +0100 Subject: [PATCH] Added RuleSheet and Collection & AbstractCollection. Optimized Variables --- RuleSheet.md | 16 ++++ .../src/builder/example/GlobalVariables.java | 47 +++++++---- .../src/builder/example/TestBuilder.java | 2 +- .../collections/AbstractCollection.template | 78 +++++++++++++++++++ .../templates/collections/Collection.template | 70 +++++++++++++++++ .../templates/functions/Consumer.template | 2 +- 6 files changed, 200 insertions(+), 15 deletions(-) create mode 100644 RuleSheet.md create mode 100644 src/main/resources/speiger/assets/collections/templates/collections/AbstractCollection.template create mode 100644 src/main/resources/speiger/assets/collections/templates/collections/Collection.template diff --git a/RuleSheet.md b/RuleSheet.md new file mode 100644 index 0000000..ed9befb --- /dev/null +++ b/RuleSheet.md @@ -0,0 +1,16 @@ +# Rule Sheet + +This file documents all of the formatting rules that will exist in the Library, +to ensure consistency in the Library. + +Some Base Rules: + +- Packages should be sorted to approbate folders. To Ensure fast finding of classes and not running through hundreds of classes in 1 package. +- Class Header braces always start in new line to help with if a if is needed in the class header. +- Otherwise braces should be in the same line as a function name. +- If a function could be done in a single line it should be. +- Function overloads should be avoided. They are only causing problems. +- If a primitive function would cause a overload then a rename should try to add a shortened primitive name in it. If that destroys the naming flow then a cut version name. If that does not work a new rule has to be made up. Example: "get -> getInt" while "removeIf => removeIfInt" does not work so we default to "removeIf => remIf". +- If a function would break the java interface rules (ArrayList.trimToSize) a dedicated interface should be created for it. + +Rules can be added or altered, but should only be so if there is a real reason to be present. \ No newline at end of file diff --git a/src/main/java/speiger/src/builder/example/GlobalVariables.java b/src/main/java/speiger/src/builder/example/GlobalVariables.java index f3a5cb0..26e1c32 100644 --- a/src/main/java/speiger/src/builder/example/GlobalVariables.java +++ b/src/main/java/speiger/src/builder/example/GlobalVariables.java @@ -24,32 +24,37 @@ public class GlobalVariables public GlobalVariables createVariables() { - 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()+">" : "")); + addSimpleMapper("PACKAGE", type.getPathType()); + addSimpleMapper("CLASS_TYPE", type.getClassType()); + addSimpleMapper("KEY_TYPE", type.getKeyType()); + addSimpleMapper(" 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())); + addSimpleMapper("JAVA_TYPE", type.getCustomJDKType().getKeyType()); + addSimpleMapper("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()); + addSimpleMapper("KEY_TO_OBJ", type.getClassType()+".valueOf"); + addInjectMapper("OBJ_TO_KEY(\\([^)]+\\)|\\S)", "%s."+type.getKeyType()+"Value()").removeBraces(); + addInjectMapper("CLASS_TO_KEY(\\([^)]+\\)|\\S)", "(("+type.getClassType()+")%s)."+type.getKeyType()+"Value()").removeBraces(); + return this; } public GlobalVariables createClassTypes() { - 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")); + addSimpleMapper("JAVA_PREDICATE", type.isPrimitiveBlocking() ? "" : type.getCustomJDKType().getFileType()+"Predicate"); + addSimpleMapper("JAVA_CONSUMER", type.isPrimitiveBlocking() ? "" : "java.util.function."+type.getCustomJDKType().getFileType()+"Consumer"); + addSimpleMapper("CONSUMER", type.getFileType()+"Consumer"); + addSimpleMapper("ITERATOR", type.getFileType()+"Iterator"); + addSimpleMapper("ITERABLE", type.getFileType()+"Iterable"); + addSimpleMapper("ABSTRACT_COLLECTION", type.getFileType()+"AbstractCollection"); + addSimpleMapper("COLLECTION", type.getFileType()+"Collection"); return this; } public GlobalVariables createFunctions() { - operators.add(new SimpleMapper("NEXT()", "next"+type.getNonClassType()+"()")); + addSimpleMapper("NEXT()", "next"+type.getNonClassType()+"()"); return this; } @@ -60,6 +65,10 @@ public class GlobalVariables { flags.add("JDK_CONSUMER"); } + if(!type.isPrimitiveBlocking()) + { + flags.add("PRIMITIVES"); + } return this; } @@ -77,6 +86,18 @@ public class GlobalVariables return type; } + private void addSimpleMapper(String pattern, String replacement) + { + operators.add(new SimpleMapper(pattern, replacement)); + } + + private InjectMapper addInjectMapper(String pattern, String replacement) + { + InjectMapper mapper = new InjectMapper(pattern, replacement); + operators.add(mapper); + return mapper; + } + class PathBuilder implements UnaryOperator { String before; diff --git a/src/main/java/speiger/src/builder/example/TestBuilder.java b/src/main/java/speiger/src/builder/example/TestBuilder.java index 924c338..5a522b5 100644 --- a/src/main/java/speiger/src/builder/example/TestBuilder.java +++ b/src/main/java/speiger/src/builder/example/TestBuilder.java @@ -71,7 +71,7 @@ public class TestBuilder extends TemplateProcessor { try { - new TestBuilder().process(true); + new TestBuilder().process(false); } catch(InterruptedException e) { diff --git a/src/main/resources/speiger/assets/collections/templates/collections/AbstractCollection.template b/src/main/resources/speiger/assets/collections/templates/collections/AbstractCollection.template new file mode 100644 index 0000000..3b6b226 --- /dev/null +++ b/src/main/resources/speiger/assets/collections/templates/collections/AbstractCollection.template @@ -0,0 +1,78 @@ +package speiger.src.collections.PACKAGE.collections; + +import java.util.AbstractCollection; + +public abstract class ABSTRACT_COLLECTION KEY_GENERIC_TYPE extends AbstractCollection implements COLLECTION KEY_GENERIC_TYPE +{ + @Override + public abstract ITERATOR KEY_GENERIC_TYPE iterator(); + +#if !TYPE_OBJECT + @Override + @Deprecated + public boolean add(CLASS_TYPE e) { return COLLECTION.super.add(e); } + + @Override + public boolean addAll(COLLECTION c) { + boolean modified = false; + for(KEY_TYPE e : c) { + modified |= add(e); + } + return modified; + } + + @Override + @Deprecated + public boolean contains(Object e) { return COLLECTION.super.contains(e); } + + @Override + public boolean contains(KEY_TYPE e) { + for(KEY_TYPE entry : this) { if(entry == e) return true; } + return false; + } + + @Override + public boolean containsAll(COLLECTION c) { + for(KEY_TYPE e : c) + if(!contains(e)) + return false; + return true; + } + + @Override + public boolean containsAny(COLLECTION c) { + for(KEY_TYPE e : c) + if(contains(e)) + return true; + return false; + } + + @Override + @Deprecated + public boolean remove(Object e) { return COLLECTION.super.remove(e); } + + @Override + public boolean removeAll(COLLECTION c) { + boolean modified = false; + for(ITERATOR iter = iterator();iter.hasNext();) { + if(c.contains(iter.NEXT())) { + iter.remove(); + modified = true; + } + } + return modified; + } + + @Override + public boolean retainAll(COLLECTION c) { + boolean modified = false; + for(ITERATOR iter = iterator();iter.hasNext();) { + if(!c.contains(iter.NEXT())) { + iter.remove(); + modified = true; + } + } + return modified; + } +#endif +} \ No newline at end of file diff --git a/src/main/resources/speiger/assets/collections/templates/collections/Collection.template b/src/main/resources/speiger/assets/collections/templates/collections/Collection.template new file mode 100644 index 0000000..1a9e1f2 --- /dev/null +++ b/src/main/resources/speiger/assets/collections/templates/collections/Collection.template @@ -0,0 +1,70 @@ +package speiger.src.collections.PACKAGE.collections; + +import java.util.Collection; +#if PRIMITIVES +import java.util.Objects; +import java.util.function.JAVA_PREDICATE; +#endif + +public interface COLLECTION KEY_GENERIC_TYPE extends Collection, ITERABLE KEY_GENERIC_TYPE +{ +#if !TYPE_OBJECT + public boolean add(KEY_TYPE o); + + public boolean addAll(COLLECTION c); + + public boolean contains(KEY_TYPE o); + + public boolean containsAll(COLLECTION c); + + public boolean containsAny(COLLECTION c); + + public boolean remove(KEY_TYPE o); + + public boolean removeAll(COLLECTION c); + + public boolean retainAll(COLLECTION c); + + public KEY_TYPE[] TO_ARRAY(); + + public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] a); + +#if PRIMITIVES + public default boolean remIf(JAVA_PREDICATE filter) { + Objects.requireNonNull(filter); + boolean removed = false; + final ITERATOR each = iterator(); + while (each.hasNext()) { + if (filter.test(each.NEXT())) { + each.remove(); + removed = true; + } + } + return removed; + } + +#endif + @Override + @Deprecated + public default boolean add(CLASS_TYPE o) { return add(OBJ_TO_KEY(o)); } + + @Override + @Deprecated + public default boolean contains(Object o) { return o != null && contains(CLASS_TO_KEY(o)); } + + @Override + @Deprecated + public default boolean remove(Object o) { return o != null && remove(CLASS_TO_KEY(o)); } + + @Override + @Deprecated + public K[] toArray(K[] a); + + @Override + @Deprecated + public Object[] toArray(); + +#endif + @Override + public ITERATOR KEY_GENERIC_TYPE iterator(); +} \ 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 7a0ee0e..b80a39f 100644 --- a/src/main/resources/speiger/assets/collections/templates/functions/Consumer.template +++ b/src/main/resources/speiger/assets/collections/templates/functions/Consumer.template @@ -31,7 +31,7 @@ public interface CONSUMER extends Consumer return T -> {accept(T); after.accept(KEY_TO_OBJ(T));}; } -#if !TYPE_BOOLEAN +#if PRIMITIVES @Deprecated @Override default CONSUMER andThen(JAVA_CONSUMER after) {