diff --git a/src/main/java/speiger/src/builder/example/ClassType.java b/src/main/java/speiger/src/builder/example/ClassType.java index 7203255f..18d6f142 100644 --- a/src/main/java/speiger/src/builder/example/ClassType.java +++ b/src/main/java/speiger/src/builder/example/ClassType.java @@ -2,29 +2,31 @@ 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"); + BOOLEAN("boolean", "Boolean", "Boolean", "booleans", "BOOLEAN", "false"), + BYTE("byte", "Byte", "Byte", "bytes", "BYTES", "(byte)0"), + SHORT("short", "Short", "Short", "shorts", "SHORT", "(short)0"), + CHAR("char", "Character", "Char", "chars", "CHAR", "(char)0"), + INT("int", "Integer", "Int", "ints", "INT", "0"), + LONG("long", "Long", "Long", "longs", "LONG", "0L"), + FLOAT("float", "Float", "Float", "floats", "FLOAT", "0F"), + DOUBLE("double", "Double", "Double", "doubles", "DOUBLE", "0D"), + OBJECT("T", "T", "Object", "objects", "OBJECT", "null"); String keyType; String classType; String fileType; String pathType; String capType; + String emptyValue; - private ClassType(String keyType, String classType, String fileType, String pathType, String capType) + private ClassType(String keyType, String classType, String fileType, String pathType, String capType, String emptyValue) { this.keyType = keyType; this.classType = classType; this.fileType = fileType; this.pathType = pathType; this.capType = capType; + this.emptyValue = emptyValue; } public String getKeyType() @@ -57,6 +59,11 @@ public enum ClassType return capType; } + public String getEmptyValue() + { + return emptyValue; + } + public boolean isPrimitiveBlocking() { return this == BOOLEAN || this == OBJECT; diff --git a/src/main/java/speiger/src/builder/example/GlobalVariables.java b/src/main/java/speiger/src/builder/example/GlobalVariables.java index 5e05ffb6..69268890 100644 --- a/src/main/java/speiger/src/builder/example/GlobalVariables.java +++ b/src/main/java/speiger/src/builder/example/GlobalVariables.java @@ -28,6 +28,7 @@ public class GlobalVariables addSimpleMapper("PACKAGE", type.getPathType()); addSimpleMapper("CLASS_TYPE", type.getClassType()); addSimpleMapper("KEY_TYPE", type.getKeyType()); + addSimpleMapper("EMPTY_VALUE", type.getEmptyValue()); addSimpleMapper(" KEY_GENERIC_TYPE", type == ClassType.OBJECT ? "<"+type.getKeyType()+">" : ""); addSimpleMapper(" GENERIC_BRACES", type == ClassType.OBJECT ? " <"+type.getKeyType()+">" : ""); addSimpleMapper("BRACES", type == ClassType.OBJECT ? "<>" : ""); @@ -67,6 +68,7 @@ public class GlobalVariables addClassMapper("SUB_LIST", "SubList"); addClassMapper("ARRAY_LIST", "ArrayList"); addClassMapper("LIST", "List"); + addClassMapper("STACK", "Stack"); return this; } @@ -78,6 +80,10 @@ public class GlobalVariables addFunctionMapper("REMOVE_KEY", "rem"); addFunctionMapper("REMOVE", "remove"); addFunctionMapper("PREVIOUS", "previous"); + addFunctionMapper("PEEK", "peek"); + addFunctionMapper("POP", "pop"); + addFunctionMapper("PUSH", "push"); + addFunctionMapper("TOP", "top"); return this; } diff --git a/src/main/java/speiger/src/builder/example/TestBuilder.java b/src/main/java/speiger/src/builder/example/TestBuilder.java index 39fe25a2..ef5f1fce 100644 --- a/src/main/java/speiger/src/builder/example/TestBuilder.java +++ b/src/main/java/speiger/src/builder/example/TestBuilder.java @@ -17,7 +17,6 @@ public class TestBuilder extends TemplateProcessor { Map> blocked = new HashMap>(); public static final ClassType[] TYPE = ClassType.values(); - List varibles = new ArrayList(); public TestBuilder() @@ -52,6 +51,7 @@ public class TestBuilder extends TemplateProcessor varibles.add(type); } blocked.put("Consumer", EnumSet.of(ClassType.OBJECT)); + blocked.put("Stack", EnumSet.of(ClassType.OBJECT)); } @Override diff --git a/src/main/java/speiger/src/collections/utils/IArray.java b/src/main/java/speiger/src/collections/utils/IArray.java new file mode 100644 index 00000000..2d0b5747 --- /dev/null +++ b/src/main/java/speiger/src/collections/utils/IArray.java @@ -0,0 +1,8 @@ +package speiger.src.collections.utils; + +import java.util.RandomAccess; + +public interface IArray extends RandomAccess, ITrimmable +{ + public void ensureCapacity(int size); +} diff --git a/src/main/java/speiger/src/collections/utils/ITrimmable.java b/src/main/java/speiger/src/collections/utils/ITrimmable.java new file mode 100644 index 00000000..06358c2b --- /dev/null +++ b/src/main/java/speiger/src/collections/utils/ITrimmable.java @@ -0,0 +1,10 @@ +package speiger.src.collections.utils; + +public interface ITrimmable +{ + public default void trim() { + trim(0); + } + + public void trim(int size); +} diff --git a/src/main/java/speiger/src/collections/utils/Stack.java b/src/main/java/speiger/src/collections/utils/Stack.java new file mode 100644 index 00000000..ec08db98 --- /dev/null +++ b/src/main/java/speiger/src/collections/utils/Stack.java @@ -0,0 +1,18 @@ +package speiger.src.collections.utils; + +public interface Stack +{ + public void push(T e); + + public T pop(); + + public int size(); + + public void clear(); + + public default T top() { + return peek(0); + } + + public T peek(int index); +} diff --git a/src/main/resources/speiger/assets/collections/templates/collections/Stack.template b/src/main/resources/speiger/assets/collections/templates/collections/Stack.template new file mode 100644 index 00000000..996edcf9 --- /dev/null +++ b/src/main/resources/speiger/assets/collections/templates/collections/Stack.template @@ -0,0 +1,37 @@ +package speiger.src.collections.PACKAGE.collections; + +import speiger.src.collections.utils.Stack; + +public interface STACK extends Stack +{ + public void PUSH(KEY_TYPE e); + + public KEY_TYPE POP(); + + public default KEY_TYPE TOP() { + return PEEK(0); + } + + public KEY_TYPE PEEK(int index); + +#if !OBJECT_TYPE + @Deprecated + public default void push(CLASS_TYPE e) { PUSH(OBJ_TO_KEY(e)); } + + @Deprecated + public default CLASS_TYPE pop() { return KEY_TO_OBJ(POP()); } + + @Deprecated + public default CLASS_TYPE top() { + return peek(size() - 1); + } + + @Deprecated + public default CLASS_TYPE bottom() { + return peek(0); + } + + @Deprecated + public default CLASS_TYPE peek(int index) { return KEY_TO_OBJ(PEEK(index)); } +#endif +} \ No newline at end of file diff --git a/src/main/resources/speiger/assets/collections/templates/lists/AbstractList.template b/src/main/resources/speiger/assets/collections/templates/lists/AbstractList.template index 81f0dfca..b4222eab 100644 --- a/src/main/resources/speiger/assets/collections/templates/lists/AbstractList.template +++ b/src/main/resources/speiger/assets/collections/templates/lists/AbstractList.template @@ -53,7 +53,7 @@ public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION } return -1; } - + @Override public int lastIndexOf(Object o) { LIST_ITERATOR KEY_GENERIC_TYPE iter = listIterator(size()); @@ -106,7 +106,7 @@ public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION public ITERATOR KEY_GENERIC_TYPE iterator() { return listIterator(0); } - + @Override public LIST_ITERATOR KEY_GENERIC_TYPE listIterator() { return listIterator(0); @@ -117,6 +117,12 @@ public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION return new LIST_ITER(index); } + @Override + public void size(int size) { + while(size > size()) add(EMPTY_VALUE); + while(size < size()) REMOVE(size() - 1); + } + private class SUB_LIST extends ABSTRACT_LIST KEY_GENERIC_TYPE { ABSTRACT_LIST KEY_GENERIC_TYPE l; int offset; diff --git a/src/main/resources/speiger/assets/collections/templates/lists/ArrayList.template b/src/main/resources/speiger/assets/collections/templates/lists/ArrayList.template index 0f4be7ce..6ef68006 100644 --- a/src/main/resources/speiger/assets/collections/templates/lists/ArrayList.template +++ b/src/main/resources/speiger/assets/collections/templates/lists/ArrayList.template @@ -5,19 +5,38 @@ import java.util.Collection; import java.util.Iterator; import speiger.src.collections.PACKAGE.collections.COLLECTION; +#if !TYPE_OBJECT +import speiger.src.collections.PACKAGE.collections.STACK; +#endif import speiger.src.collections.PACKAGE.collections.ITERATOR; import speiger.src.collections.PACKAGE.utils.ARRAYS; +import speiger.src.collections.PACKAGE.utils.ITERATORS; +#if TYPE_OBJECT +import speiger.src.collections.utils.Stack; +#endif +import speiger.src.collections.utils.IArray; import speiger.src.collections.utils.SanityChecks; -public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE +#if TYPE_OBJECT +public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE implements IArray, Stack +#else +public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE implements IArray, STACK +#endif { static final int DEFAULT_ARRAY_SIZE = 10; protected transient KEY_TYPE[] data; protected int size = 0; - public ARRAY_LIST(int size) - { + public ARRAY_LIST() { +#if TYPE_OBJECT + data = (KEY_TYPE[])ARRAYS.EMPTY_ARRAY; +#else + data = ARRAYS.EMPTY_ARRAY; +#endif + } + + public ARRAY_LIST(int size) { #if TYPE_OBJECT data = (KEY_TYPE[])new Object[size]; #else @@ -25,6 +44,49 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE #endif } + public ARRAY_LIST(Collection c) { + this(c.size()); + size = ITERATORS.unwrap(data, c.iterator()); + } + + public ARRAY_LIST(COLLECTION KEY_GENERIC_TYPE c) { + this(c.size()); + size = ITERATORS.unwrap(data, c.iterator()); + } + + public ARRAY_LIST(LIST KEY_GENERIC_TYPE l) { + this(l.size()); + size = l.size(); + l.getElements(0, data, 0, size); + } + + public ARRAY_LIST(KEY_TYPE[] a) { + this(a, 0, a.length); + } + + public ARRAY_LIST(KEY_TYPE[] a, int length) { + this(a, 0, length); + } + + public ARRAY_LIST(KEY_TYPE[] a, int offset, int length) { + this(length); + SanityChecks.checkArrayCapacity(a.length, offset, length); + System.arraycopy(a, offset, data, 0, length); + size = length; + } + + public static GENERIC_BRACES ARRAY_LIST KEY_GENERIC_TYPE wrap(KEY_TYPE[] a) { + return wrap(a, a.length); + } + + public static GENERIC_BRACES ARRAY_LIST KEY_GENERIC_TYPE wrap(KEY_TYPE[] a, int length) { + SanityChecks.checkArrayCapacity(a.length, 0, length); + ARRAY_LIST KEY_GENERIC_TYPE list = new ARRAY_LISTBRACES(); + list.data = a; + list.size = length; + return list; + } + @Override public void add(int index, CLASS_TYPE element) { checkAddRange(index); @@ -155,7 +217,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE checkRange(index); return data[index]; } - + @Override public KEY_TYPE set(int index, KEY_TYPE e) { checkRange(index); @@ -163,7 +225,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE data[index] = e; return old; } - + @Override public KEY_TYPE REMOVE(int index) { checkRange(index); @@ -181,6 +243,14 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE return size; } + @Override + public void size(int size) { + if(size > data.length) + data = Arrays.copyOf(data, size); + else if(size < size() && size >= 0) + Arrays.fill(data, size, size(), EMPTY_VALUE); + } + @Override public void clear() { #if TYPE_OBJECT @@ -189,6 +259,38 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE size = 0; } + @Override + public void trim(int size) { + if(size > size() || size() == data.length) return; + int value = Math.min(size, size()); +#if TYPE_OBJECT + data = value == 0 ? (KEY_TYPE[])ARRAYS.EMPTY_ARRAY : Arrays.copyOf(data, value); +#else + data = value == 0 ? ARRAYS.EMPTY_ARRAY : Arrays.copyOf(data, value); +#endif + } + + @Override + public void ensureCapacity(int size) { + grow(size); + } + + @Override + public void PUSH(KEY_TYPE e) { + add(e); + } + + @Override + public KEY_TYPE POP() { + return REMOVE(size() - 1); + } + + @Override + public KEY_TYPE PEEK(int index) { + checkRange((size() - 1) - index); + return data[(size() - 1) - index]; + } + protected void grow(int capacity) { if(capacity < data.length) return; diff --git a/src/main/resources/speiger/assets/collections/templates/lists/List.template b/src/main/resources/speiger/assets/collections/templates/lists/List.template index bbfafc12..1b8a3e36 100644 --- a/src/main/resources/speiger/assets/collections/templates/lists/List.template +++ b/src/main/resources/speiger/assets/collections/templates/lists/List.template @@ -56,6 +56,8 @@ public interface LIST KEY_GENERIC_TYPE extends COLLECTION KEY_GENERIC_TYPE, List @Override public LIST KEY_GENERIC_TYPE subList(int from, int to); + public void size(int size); + #if !TYPE_OBJECT @Override @Deprecated diff --git a/src/main/resources/speiger/assets/collections/templates/utils/Iterators.template b/src/main/resources/speiger/assets/collections/templates/utils/Iterators.template index 33a68b6a..809a3a55 100644 --- a/src/main/resources/speiger/assets/collections/templates/utils/Iterators.template +++ b/src/main/resources/speiger/assets/collections/templates/utils/Iterators.template @@ -1,5 +1,6 @@ package speiger.src.collections.PACKAGE.utils; +import java.util.Iterator; import speiger.src.collections.PACKAGE.collections.ITERATOR; public class ITERATORS @@ -12,6 +13,22 @@ public class ITERATORS return new ArrayIteratorBRACES(a, start, end); } + public static GENERIC_BRACES int unwrap(KEY_TYPE[] a, Iterator i) { + return unwrap(a, i, 0, a.length); + } + + public static GENERIC_BRACES int unwrap(KEY_TYPE[] a, Iterator i, int offset) { + return unwrap(a, i, offset, a.length); + } + + public static GENERIC_BRACES int unwrap(KEY_TYPE[] a, Iterator i, int offset, int max) { + if(max < 0) throw new IllegalStateException("The max size is smaller then 0"); + if(offset + max >= a.length) throw new IllegalStateException("largest array index exceeds array size"); + int index = 0; + for(;index