Ranides Atterwim 9 роки тому
батько
коміт
43f87228ab

+ 122 - 0
assira/src/main/java/net/ranides/assira/collection/arrays/ArrayBuffer.java

@@ -0,0 +1,122 @@
+package net.ranides.assira.collection.arrays;
+
+import net.ranides.assira.collection.lists.NativeArrayList;
+import net.ranides.assira.text.StrBuffer;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ *
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+public class ArrayBuffer implements Serializable {
+
+    private static final long serialVersionUID = 2L;
+
+    private NativeArray buffer;
+    private int offset;
+
+    public Object array() {
+        return NativeArrayUtils.head(buffer, offset).array();
+    }
+
+    @SuppressWarnings("unchecked")
+    public <Auto> Auto $array() {
+        return NativeArrayUtils.head(buffer, offset).$array();
+    }
+
+    public int size() {
+        return offset;
+    }
+
+    public int capacity() {
+        return buffer.size();
+    }
+
+    public ArrayBuffer resize(int length, Object value) {
+        int dt = length - offset;
+        buffer = NativeArrayAllocator.resize(buffer, length);
+        if(dt > 0) {
+            buffer.set(offset, offset + dt, value);
+        }
+        offset = length;
+        return this;
+    }
+
+    public Class<?> component() {
+        return buffer.component();
+    }
+
+    public ArrayBuffer set(int index, Object value) {
+        buffer.set(index, value);
+        return this;
+    }
+
+    public ArrayBuffer set(int begin, int end, Object value) {
+        buffer.set(begin,end,value);
+        return this;
+    }
+
+    public ArrayBuffer clear() {
+        offset = 0;
+        return this;
+    }
+
+    public ArrayBuffer push(Object value) {
+        buffer.set(offset++, value);
+        return this;
+    }
+
+    public ArrayBuffer append(Object value) {
+        if(value instanceof Collection) {
+            return concat((Collection<?>)value);
+        }
+        if(value instanceof NativeArray) {
+            return append((NativeArray)value);
+        }
+        return append(NativeArray.wrap(value));
+    }
+
+    public <T> ArrayBuffer append(NativeArray values) {
+        int n = values.size();
+        NativeArrayUtils.copy(values, 0, buffer, offset, n);
+        offset += n;
+        return this;
+    }
+
+    public <T> ArrayBuffer append(T[] values) {
+        return append(NativeArray.wrap(values));
+    }
+
+    public <T> ArrayBuffer concat(Collection<T> values) {
+        for(T v : values) {
+            buffer.set(offset++, v);
+        }
+        return this;
+    }
+
+    @Override
+    public boolean equals(Object object) {
+        return super.equals(object);
+    }
+
+    @Override
+    public int hashCode() {
+        return super.hashCode();
+    }
+
+    @Override
+    public String toString() {
+        return super.toString();
+    }
+
+    public NativeArray asNativeArray() {
+        return buffer;
+    }
+
+    public List<Object> asList() {
+        return new NativeArrayList.NativeList(buffer, 0, offset);
+    }
+}

+ 126 - 0
assira/src/main/java/net/ranides/assira/collection/arrays/ArrayBuilder.java

@@ -0,0 +1,126 @@
+package net.ranides.assira.collection.arrays;
+
+import net.ranides.assira.collection.lists.NativeArrayList;
+
+import java.util.Collection;
+import java.util.List;
+
+/**
+ *
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+public class ArrayBuilder {
+
+    private static final long serialVersionUID = 2L;
+
+    private NativeArray buffer;
+
+    private int offset;
+
+    public Object array() {
+        return NativeArrayUtils.head(buffer, offset).array();
+    }
+
+    @SuppressWarnings("unchecked")
+    public <Auto> Auto $array() {
+        return NativeArrayUtils.head(buffer, offset).$array();
+    }
+
+    public int size() {
+        return offset;
+    }
+
+    public int capacity() {
+        return buffer.size();
+    }
+
+    public ArrayBuilder resize(int length, Object value) {
+        int dt = length - offset;
+        buffer = NativeArrayAllocator.resize(buffer, length);
+        if(dt > 0) {
+            buffer.set(offset, offset + dt, value);
+        }
+        offset = length;
+        return this;
+    }
+
+    public Class<?> component() {
+        return buffer.component();
+    }
+
+    public ArrayBuilder set(int index, Object value) {
+
+        buffer.set(index, value);
+        return this;
+    }
+
+    public ArrayBuilder set(int begin, int end, Object value) {
+        buffer.set(begin,end,value);
+        return this;
+    }
+
+    public ArrayBuilder clear() {
+        offset = 0;
+        return this;
+    }
+
+    public ArrayBuilder push(Object value) {
+        buffer = NativeArrayAllocator.grow(buffer, offset+1);
+        buffer.set(offset++, value);
+        return this;
+    }
+
+    public ArrayBuilder append(Object value) {
+        if(value instanceof Collection) {
+            return concat((Collection<?>)value);
+        }
+        if(value instanceof NativeArray) {
+            return append((NativeArray)value);
+        }
+        return append(NativeArray.wrap(value));
+    }
+
+    public <T> ArrayBuilder append(NativeArray values) {
+        int n = values.size();
+        buffer = NativeArrayAllocator.grow(buffer, offset+n);
+        NativeArrayUtils.copy(values, 0, buffer, offset, n);
+        offset += n;
+        return this;
+    }
+
+    public <T> ArrayBuilder append(T[] values) {
+        return append(NativeArray.wrap(values));
+    }
+
+    public <T> ArrayBuilder concat(Collection<T> values) {
+        buffer = NativeArrayAllocator.grow(buffer, offset+values.size());
+        for(T v : values) {
+            buffer.set(offset++, v);
+        }
+        return this;
+    }
+
+    @Override
+    public boolean equals(Object object) {
+        return super.equals(object);
+    }
+
+    @Override
+    public int hashCode() {
+        return super.hashCode();
+    }
+
+    @Override
+    public String toString() {
+        return super.toString();
+    }
+
+    public NativeArray asNativeArray() {
+        return buffer;
+    }
+
+    public List<Object> asList() {
+        return new NativeArrayList.NativeList(buffer, 0, offset);
+    }
+
+}