mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-05 16:25:54 +00:00
test[array]: 测试用例ArrayIntList
This commit is contained in:
@@ -420,7 +420,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static List<IPacket> readPacketList(ByteBuf byteBuf, IProtocolRegistration protocolRegistration) {
|
||||
var length = readInt(byteBuf);
|
||||
var list = new FixedSizeList<IPacket>(length);
|
||||
var list = new FixedSizeList<IPacket>(CollectionUtils.comfortableCapacity(length));
|
||||
for (var i = 0; i < length; i++) {
|
||||
list.set(i, (IPacket) protocolRegistration.read(byteBuf));
|
||||
}
|
||||
@@ -709,7 +709,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static boolean[] readBooleanArray(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
var bytes = new byte[length];
|
||||
var bytes = new byte[CollectionUtils.comfortableLength(length)];
|
||||
var array = new boolean[length];
|
||||
byteBuf.readBytes(bytes);
|
||||
for (var i = 0; i < length; i++) {
|
||||
@@ -731,7 +731,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static Boolean[] readBooleanBoxArray(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
var array = new Boolean[length];
|
||||
var array = new Boolean[CollectionUtils.comfortableLength(length)];
|
||||
for (var i = 0; i < length; i++) {
|
||||
array[i] = readBooleanBox(byteBuf);
|
||||
}
|
||||
@@ -755,7 +755,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static List<Boolean> readBooleanList(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
var list = new FixedSizeListBoolean(length);
|
||||
var list = new FixedSizeListBoolean(CollectionUtils.comfortableCapacity(length));
|
||||
for (var i = 0; i < length; i++) {
|
||||
list.set(i, readBoolean(byteBuf));
|
||||
}
|
||||
@@ -791,7 +791,7 @@ public abstract class ByteBufUtils {
|
||||
return ArrayUtils.EMPTY_BYTE_ARRAY;
|
||||
}
|
||||
|
||||
var bytes = new byte[length];
|
||||
var bytes = new byte[CollectionUtils.comfortableLength(length)];
|
||||
byteBuf.readBytes(bytes);
|
||||
return bytes;
|
||||
}
|
||||
@@ -809,7 +809,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static Byte[] readByteBoxArray(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
var bytesBox = new Byte[length];
|
||||
var bytesBox = new Byte[CollectionUtils.comfortableLength(length)];
|
||||
for (var i = 0; i < length; i++) {
|
||||
bytesBox[i] = readByteBox(byteBuf);
|
||||
}
|
||||
@@ -833,7 +833,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static List<Byte> readByteList(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
var list = new FixedSizeListByte(length);
|
||||
var list = new FixedSizeListByte(CollectionUtils.comfortableCapacity(length));
|
||||
for (var i = 0; i < length; i++) {
|
||||
list.set(i, readByte(byteBuf));
|
||||
}
|
||||
@@ -872,7 +872,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static short[] readShortArray(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
var shorts = new short[length];
|
||||
var shorts = new short[CollectionUtils.comfortableLength(length)];
|
||||
var readIndex = byteBuf.readerIndex();
|
||||
for (var i = 0; i < length; i++) {
|
||||
shorts[i] = byteBuf.getShort(readIndex);
|
||||
@@ -895,7 +895,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static Short[] readShortBoxArray(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
var shorts = new Short[length];
|
||||
var shorts = new Short[CollectionUtils.comfortableLength(length)];
|
||||
for (var i = 0; i < length; i++) {
|
||||
shorts[i] = readShortBox(byteBuf);
|
||||
}
|
||||
@@ -919,7 +919,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static List<Short> readShortList(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
var list = new FixedSizeListShort(length);
|
||||
var list = new FixedSizeListShort(CollectionUtils.comfortableCapacity(length));
|
||||
for (var i = 0; i < length; i++) {
|
||||
list.set(i, readShort(byteBuf));
|
||||
}
|
||||
@@ -954,7 +954,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static int[] readIntArray(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
var ints = new int[length];
|
||||
var ints = new int[CollectionUtils.comfortableLength(length)];
|
||||
for (var i = 0; i < length; i++) {
|
||||
ints[i] = readInt(byteBuf);
|
||||
}
|
||||
@@ -974,7 +974,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static Integer[] readIntBoxArray(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
var ints = new Integer[length];
|
||||
var ints = new Integer[CollectionUtils.comfortableLength(length)];
|
||||
for (var i = 0; i < length; i++) {
|
||||
ints[i] = readIntBox(byteBuf);
|
||||
}
|
||||
@@ -998,7 +998,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static List<Integer> readIntList(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
var list = new FixedSizeListInt(length);
|
||||
var list = new ArrayIntList(CollectionUtils.comfortableCapacity(length));
|
||||
for (var i = 0; i < length; i++) {
|
||||
list.set(i, readInt(byteBuf));
|
||||
}
|
||||
@@ -1033,7 +1033,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static long[] readLongArray(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
var longs = new long[length];
|
||||
var longs = new long[CollectionUtils.comfortableLength(length)];
|
||||
for (var i = 0; i < length; i++) {
|
||||
longs[i] = readLong(byteBuf);
|
||||
}
|
||||
@@ -1053,7 +1053,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static Long[] readLongBoxArray(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
var longs = new Long[length];
|
||||
var longs = new Long[CollectionUtils.comfortableLength(length)];
|
||||
for (var i = 0; i < length; i++) {
|
||||
longs[i] = readLongBox(byteBuf);
|
||||
}
|
||||
@@ -1077,7 +1077,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static List<Long> readLongList(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
var list = new FixedSizeListLong(length);
|
||||
var list = new FixedSizeListLong(CollectionUtils.comfortableCapacity(length));
|
||||
for (var i = 0; i < length; i++) {
|
||||
list.set(i, readLong(byteBuf));
|
||||
}
|
||||
@@ -1116,7 +1116,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static float[] readFloatArray(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
var floats = new float[length];
|
||||
var floats = new float[CollectionUtils.comfortableLength(length)];
|
||||
var readIndex = byteBuf.readerIndex();
|
||||
for (var i = 0; i < length; i++) {
|
||||
floats[i] = byteBuf.getFloat(readIndex);
|
||||
@@ -1139,7 +1139,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static Float[] readFloatBoxArray(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
var floats = new Float[length];
|
||||
var floats = new Float[CollectionUtils.comfortableLength(length)];
|
||||
for (var i = 0; i < length; i++) {
|
||||
floats[i] = readFloatBox(byteBuf);
|
||||
}
|
||||
@@ -1163,7 +1163,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static List<Float> readFloatList(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
var list = new FixedSizeListFloat(length);
|
||||
var list = new FixedSizeListFloat(CollectionUtils.comfortableCapacity(length));
|
||||
for (var i = 0; i < length; i++) {
|
||||
list.set(i, readFloat(byteBuf));
|
||||
}
|
||||
@@ -1202,7 +1202,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static double[] readDoubleArray(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
var doubles = new double[length];
|
||||
var doubles = new double[CollectionUtils.comfortableLength(length)];
|
||||
var readIndex = byteBuf.readerIndex();
|
||||
for (var i = 0; i < length; i++) {
|
||||
doubles[i] = byteBuf.getDouble(readIndex);
|
||||
@@ -1225,7 +1225,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static Double[] readDoubleBoxArray(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
var doubles = new Double[length];
|
||||
var doubles = new Double[CollectionUtils.comfortableLength(length)];
|
||||
for (var i = 0; i < length; i++) {
|
||||
doubles[i] = readDoubleBox(byteBuf);
|
||||
}
|
||||
@@ -1249,7 +1249,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static List<Double> readDoubleList(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
var list = new FixedSizeListDouble(length);
|
||||
var list = new FixedSizeListDouble(CollectionUtils.comfortableCapacity(length));
|
||||
for (var i = 0; i < length; i++) {
|
||||
list.set(i, readDouble(byteBuf));
|
||||
}
|
||||
@@ -1283,7 +1283,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static String[] readStringArray(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
var strings = new String[length];
|
||||
var strings = new String[CollectionUtils.comfortableLength(length)];
|
||||
for (var i = 0; i < length; i++) {
|
||||
strings[i] = readString(byteBuf);
|
||||
}
|
||||
@@ -1307,7 +1307,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static List<String> readStringList(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
var list = new FixedSizeList(length);
|
||||
var list = new FixedSizeList(CollectionUtils.comfortableCapacity(length));
|
||||
for (var i = 0; i < length; i++) {
|
||||
list.set(i, readString(byteBuf));
|
||||
}
|
||||
@@ -1342,7 +1342,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static char[] readCharArray(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
var chars = new char[length];
|
||||
var chars = new char[CollectionUtils.comfortableLength(length)];
|
||||
for (var i = 0; i < length; i++) {
|
||||
chars[i] = readChar(byteBuf);
|
||||
}
|
||||
@@ -1362,7 +1362,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static Character[] readCharBoxArray(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
var chars = new Character[length];
|
||||
var chars = new Character[CollectionUtils.comfortableLength(length)];
|
||||
for (var i = 0; i < length; i++) {
|
||||
chars[i] = readCharBox(byteBuf);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,362 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.protocol.collection;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
* @version 3.0
|
||||
*/
|
||||
public class ArrayIntList implements List<Integer> {
|
||||
|
||||
private int[] array;
|
||||
private int size;
|
||||
|
||||
public ArrayIntList(int initialCapacity) {
|
||||
this.array = new int[initialCapacity];
|
||||
}
|
||||
|
||||
public ArrayIntList(int[] array) {
|
||||
this.array = array;
|
||||
this.size = array.length;
|
||||
}
|
||||
|
||||
public void fromList(List<Integer> list) {
|
||||
if (CollectionUtils.isEmpty(list)) {
|
||||
this.size = 0;
|
||||
return;
|
||||
}
|
||||
this.size = list.size();
|
||||
this.array = ArrayUtils.intToArray(list);
|
||||
}
|
||||
|
||||
public List<Integer> toList() {
|
||||
if (isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
return ArrayUtils.toList(toArrayPrimitive());
|
||||
}
|
||||
|
||||
public int[] toArrayPrimitive() {
|
||||
return Arrays.copyOf(array, size);
|
||||
}
|
||||
|
||||
private int getCapacity() {
|
||||
return array.length - size;
|
||||
}
|
||||
|
||||
private void ensureCapacity(int capacity) {
|
||||
var remainCapacity = getCapacity();
|
||||
if (capacity > remainCapacity) {
|
||||
array = Arrays.copyOf(array, size + capacity + 16);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkIndexForAdd(int index) {
|
||||
if (index < 0 || index > size) {
|
||||
throw new IndexOutOfBoundsException("Index " + index + " is out of range, size = " + size);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object ele) {
|
||||
return indexOfPrimitive(ArrayUtils.intValue((Integer) ele)) >= 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer get(int index) {
|
||||
return getPrimitive(index);
|
||||
}
|
||||
|
||||
public int getPrimitive(int index) {
|
||||
Objects.checkIndex(index, size);
|
||||
return array[index];
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer set(int index, Integer ele) {
|
||||
var old = array[index];
|
||||
setPrimitive(index, ArrayUtils.intValue(ele));
|
||||
return old;
|
||||
}
|
||||
|
||||
public void setPrimitive(int index, int ele) {
|
||||
Objects.checkIndex(index, size);
|
||||
array[index] = ele;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(Integer ele) {
|
||||
return addPrimitive(ArrayUtils.intValue(ele));
|
||||
}
|
||||
|
||||
public boolean addPrimitive(int ele) {
|
||||
ensureCapacity(1);
|
||||
array[size++] = ele;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(int index, Integer ele) {
|
||||
addPrimitive(index, ArrayUtils.intValue(ele));
|
||||
}
|
||||
|
||||
public void addPrimitive(int index, int ele) {
|
||||
checkIndexForAdd(index);
|
||||
ensureCapacity(1);
|
||||
System.arraycopy(array, index, array, index + 1, size - index);
|
||||
array[index] = ele;
|
||||
size++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(Collection<? extends Integer> collection) {
|
||||
if (CollectionUtils.isEmpty(collection)) {
|
||||
return false;
|
||||
}
|
||||
ensureCapacity(collection.size());
|
||||
for (var ele : collection) {
|
||||
add(ele);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(int index, Collection<? extends Integer> collection) {
|
||||
if (CollectionUtils.isEmpty(collection)) {
|
||||
return false;
|
||||
}
|
||||
checkIndexForAdd(index);
|
||||
ensureCapacity(collection.size());
|
||||
var appendArray = ArrayUtils.intToArray(new ArrayList<>(collection));
|
||||
var appendLength = appendArray.length;
|
||||
|
||||
int moved = size - index;
|
||||
if (moved > 0) {
|
||||
System.arraycopy(array, index, array, index + appendLength, moved);
|
||||
}
|
||||
|
||||
System.arraycopy(appendArray, 0, array, index, appendLength);
|
||||
size += appendLength;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Object o) {
|
||||
var index = indexOf(o);
|
||||
if (index < 0) {
|
||||
return false;
|
||||
}
|
||||
remove(index);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer remove(int index) {
|
||||
return removeAt(index);
|
||||
}
|
||||
|
||||
public int removeAt(int index) {
|
||||
Objects.checkIndex(index, size);
|
||||
var oldValue = array[index];
|
||||
var newSize = size - 1;
|
||||
if (newSize > index) {
|
||||
System.arraycopy(array, index + 1, array, index, newSize - index);
|
||||
}
|
||||
size--;
|
||||
return oldValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAll(Collection<?> collection) {
|
||||
if (CollectionUtils.isEmpty(collection)) {
|
||||
return false;
|
||||
}
|
||||
var list = toList();
|
||||
var flag = list.removeAll(collection);
|
||||
fromList(list);
|
||||
return flag;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean retainAll(Collection<?> collection) {
|
||||
var list = toList();
|
||||
var flag = list.retainAll(collection);
|
||||
fromList(list);
|
||||
return flag;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
size = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object[] toArray() {
|
||||
return toList().toArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T[] toArray(T[] arrays) {
|
||||
return toList().toArray(arrays);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsAll(Collection<?> collection) {
|
||||
return toList().containsAll(collection);
|
||||
}
|
||||
|
||||
public int indexOfPrimitive(int ele) {
|
||||
for (var i = 0; i < size; i++) {
|
||||
if (array[i] == ele) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public int lastIndexOfPrimitive(int ele) {
|
||||
for (var i = size - 1; i >= 0; i--) {
|
||||
if (array[i] == ele) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int indexOf(Object ele) {
|
||||
return indexOfPrimitive(ArrayUtils.intValue((Integer) ele));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int lastIndexOf(Object ele) {
|
||||
return lastIndexOfPrimitive(ArrayUtils.intValue((Integer) ele));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Integer> iterator() {
|
||||
return new FastIterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListIterator<Integer> listIterator() {
|
||||
return new FastListIterator(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListIterator<Integer> listIterator(int index) {
|
||||
return new FastListIterator(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Integer> subList(int fromIndex, int toIndex) {
|
||||
return ArrayUtils.toList(array).subList(fromIndex, toIndex);
|
||||
}
|
||||
|
||||
private class FastIterator implements Iterator<Integer> {
|
||||
int cursor; // index of next element to return
|
||||
int lastCursor = -1; // index of last element returned; -1 if no such
|
||||
|
||||
public boolean hasNext() {
|
||||
return cursor != size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer next() {
|
||||
if (cursor >= size) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
lastCursor = cursor;
|
||||
return array[cursor++];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
if (lastCursor < 0) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
removeAt(lastCursor);
|
||||
cursor = lastCursor;
|
||||
lastCursor = -1;
|
||||
}
|
||||
}
|
||||
|
||||
private class FastListIterator extends FastIterator implements ListIterator<Integer> {
|
||||
FastListIterator(int index) {
|
||||
super();
|
||||
cursor = index;
|
||||
}
|
||||
|
||||
public boolean hasPrevious() {
|
||||
return cursor != 0;
|
||||
}
|
||||
|
||||
public int nextIndex() {
|
||||
return cursor;
|
||||
}
|
||||
|
||||
public int previousIndex() {
|
||||
return cursor - 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer previous() {
|
||||
if (cursor <= 0) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
cursor--;
|
||||
lastCursor = cursor;
|
||||
return array[cursor];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(Integer e) {
|
||||
if (lastCursor < 0) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
setPrimitive(lastCursor, ArrayUtils.intValue(e));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(Integer e) {
|
||||
addPrimitive(cursor++, e);
|
||||
lastCursor = -1;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
var builder = new StringBuilder();
|
||||
builder.append('[');
|
||||
for (var i = 0; i < size; i++) {
|
||||
builder.append(array[i]);
|
||||
if (i < size - 1) {
|
||||
builder.append(", ");
|
||||
}
|
||||
}
|
||||
builder.append(']');
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
@@ -37,6 +37,41 @@ public abstract class ArrayUtils {
|
||||
|
||||
public static final Object EMPTY_OBJECT = new Object();
|
||||
|
||||
/**
|
||||
* unbox
|
||||
*/
|
||||
public static boolean booleanValue(Boolean value) {
|
||||
return value != null && value;
|
||||
}
|
||||
|
||||
public static byte byteValue(Byte value) {
|
||||
return value == null ? 0 : value;
|
||||
}
|
||||
|
||||
public static short shortValue(Short value) {
|
||||
return value == null ? 0 : value;
|
||||
}
|
||||
|
||||
public static int intValue(Integer value) {
|
||||
return value == null ? 0 : value;
|
||||
}
|
||||
|
||||
public static long longValue(Long value) {
|
||||
return value == null ? 0 : value;
|
||||
}
|
||||
|
||||
public static float floatValue(Float value) {
|
||||
return value == null ? 0 : value;
|
||||
}
|
||||
|
||||
public static double doubleValue(Double value) {
|
||||
return value == null ? 0 : value;
|
||||
}
|
||||
|
||||
public static char charValue(Character value) {
|
||||
return value == null ? Character.MIN_VALUE : value;
|
||||
}
|
||||
|
||||
/**
|
||||
* length
|
||||
*/
|
||||
@@ -264,7 +299,7 @@ public abstract class ArrayUtils {
|
||||
var size = list.size();
|
||||
var array = new boolean[size];
|
||||
for (var i = 0; i < size; i++) {
|
||||
array[i] = list.get(i);
|
||||
array[i] = booleanValue(list.get(i));
|
||||
}
|
||||
return array;
|
||||
}
|
||||
@@ -276,7 +311,7 @@ public abstract class ArrayUtils {
|
||||
var size = list.size();
|
||||
var array = new byte[size];
|
||||
for (var i = 0; i < size; i++) {
|
||||
array[i] = list.get(i);
|
||||
array[i] = byteValue(list.get(i));
|
||||
}
|
||||
return array;
|
||||
}
|
||||
@@ -288,7 +323,7 @@ public abstract class ArrayUtils {
|
||||
var size = list.size();
|
||||
var array = new short[size];
|
||||
for (var i = 0; i < size; i++) {
|
||||
array[i] = list.get(i);
|
||||
array[i] = shortValue(list.get(i));
|
||||
}
|
||||
return array;
|
||||
}
|
||||
@@ -300,7 +335,7 @@ public abstract class ArrayUtils {
|
||||
var size = list.size();
|
||||
var array = new int[size];
|
||||
for (var i = 0; i < size; i++) {
|
||||
array[i] = list.get(i);
|
||||
array[i] = intValue(list.get(i));
|
||||
}
|
||||
return array;
|
||||
}
|
||||
@@ -313,7 +348,7 @@ public abstract class ArrayUtils {
|
||||
var size = list.size();
|
||||
var array = new long[size];
|
||||
for (var i = 0; i < size; i++) {
|
||||
array[i] = list.get(i);
|
||||
array[i] = longValue(list.get(i));
|
||||
}
|
||||
return array;
|
||||
}
|
||||
@@ -325,7 +360,7 @@ public abstract class ArrayUtils {
|
||||
var size = list.size();
|
||||
var array = new float[size];
|
||||
for (var i = 0; i < size; i++) {
|
||||
array[i] = list.get(i);
|
||||
array[i] = floatValue(list.get(i));
|
||||
}
|
||||
return array;
|
||||
}
|
||||
@@ -337,7 +372,7 @@ public abstract class ArrayUtils {
|
||||
var size = list.size();
|
||||
var array = new double[size];
|
||||
for (var i = 0; i < size; i++) {
|
||||
array[i] = list.get(i);
|
||||
array[i] = doubleValue(list.get(i));
|
||||
}
|
||||
return array;
|
||||
}
|
||||
@@ -349,7 +384,7 @@ public abstract class ArrayUtils {
|
||||
var size = list.size();
|
||||
var array = new char[size];
|
||||
for (var i = 0; i < size; i++) {
|
||||
array[i] = list.get(i);
|
||||
array[i] = charValue(list.get(i));
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
@@ -1,158 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.protocol.collection;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
* @version 3.0
|
||||
*/
|
||||
public class FixedSizeListInt implements List<Integer> {
|
||||
|
||||
private final int[] array;
|
||||
|
||||
public FixedSizeListInt(int initialCapacity) {
|
||||
this.array = new int[initialCapacity];
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return array.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return ArrayUtils.isEmpty(array);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object ele) {
|
||||
return ArrayUtils.toList(array).stream().anyMatch(it -> ele.equals(it));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer get(int index) {
|
||||
return array[index];
|
||||
}
|
||||
|
||||
public int getRaw(int index) {
|
||||
return array[index];
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer set(int index, Integer ele) {
|
||||
var old = array[index];
|
||||
array[index] = ele;
|
||||
return old;
|
||||
}
|
||||
|
||||
public void set(int index, int ele) {
|
||||
array[index] = ele;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object[] toArray() {
|
||||
return ArrayUtils.toList(array).toArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T[] toArray(T[] arrays) {
|
||||
ArrayUtils.toList(array).toArray(arrays);
|
||||
return arrays;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsAll(Collection<?> collection) {
|
||||
return ArrayUtils.toList(array).containsAll(collection);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int indexOf(Object ele) {
|
||||
return ArrayUtils.toList(array).indexOf(ele);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int lastIndexOf(Object ele) {
|
||||
return ArrayUtils.toList(array).lastIndexOf(ele);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Integer> iterator() {
|
||||
return ArrayUtils.toList(array).iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListIterator<Integer> listIterator() {
|
||||
return ArrayUtils.toList(array).listIterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListIterator<Integer> listIterator(int index) {
|
||||
return ArrayUtils.toList(array).listIterator(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Integer> subList(int fromIndex, int toIndex) {
|
||||
return ArrayUtils.toList(array).subList(fromIndex, toIndex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(Integer e) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(int index, Integer element) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(Collection<? extends Integer> collection) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(int index, Collection<? extends Integer> collection) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Object o) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer remove(int index) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAll(Collection<?> collection) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean retainAll(Collection<?> collection) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,248 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.protocol.collection;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
* @version 3.0
|
||||
*/
|
||||
public class ArrayListTest {
|
||||
|
||||
@Test
|
||||
public void testAdd() {
|
||||
var list = new ArrayIntList(1);
|
||||
Assert.assertTrue(list.isEmpty());
|
||||
Assert.assertEquals(list.size(), 0);
|
||||
int[] array = new int[100];
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
array[i] = i;
|
||||
}
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
Assert.assertTrue(list.add(array[i]));
|
||||
Assert.assertArrayEquals(list.toArray(), ArrayUtils.toList(Arrays.copyOfRange(array, 0, i + 1)).toArray());
|
||||
}
|
||||
|
||||
Assert.assertFalse(list.isEmpty());
|
||||
Assert.assertEquals(list.size(), array.length);
|
||||
Assert.assertTrue(list.contains(55));
|
||||
Assert.assertFalse(list.contains(9999));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddAll() {
|
||||
var array1 = new int[]{0, 1, 2};
|
||||
var array2 = new int[]{3, 4, 5};
|
||||
var array3 = new int[]{6, 7, 8};
|
||||
var list1 = new ArrayIntList(array1);
|
||||
var list2 = new ArrayIntList(array2);
|
||||
var list3 = new ArrayIntList(array3);
|
||||
list2.addAll(0, list1);
|
||||
Assert.assertArrayEquals(list2.toArrayPrimitive(), new int[]{0, 1, 2, 3, 4, 5});
|
||||
try {
|
||||
list2.addAll(7, list3);
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// as expected
|
||||
}
|
||||
list2.addAll(6, list3);
|
||||
Assert.assertArrayEquals(list2.toArrayPrimitive(), new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8});
|
||||
list2.addAll(6, list3);
|
||||
Assert.assertArrayEquals(list2.toArrayPrimitive(), new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 6, 7, 8});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemove() {
|
||||
var list = new ArrayIntList(1);
|
||||
for (var i = 1; i <= 5; i++) {
|
||||
list.add(i);
|
||||
}
|
||||
Assert.assertArrayEquals(list.toArray(), ArrayUtils.toList(new int[]{1, 2, 3, 4, 5}).toArray());
|
||||
Assert.assertTrue(list.remove(Integer.valueOf(2)));
|
||||
Assert.assertArrayEquals(list.toArray(), ArrayUtils.toList(new int[]{1, 3, 4, 5}).toArray());
|
||||
Assert.assertFalse(list.remove(Integer.valueOf(6)));
|
||||
Assert.assertArrayEquals(list.toArray(), ArrayUtils.toList(new int[]{1, 3, 4, 5}).toArray());
|
||||
Assert.assertEquals(list.remove(2).intValue(), 4);
|
||||
Assert.assertArrayEquals(list.toArray(), ArrayUtils.toList(new int[]{1, 3, 5}).toArray());
|
||||
Assert.assertEquals(list.remove(2).intValue(), 5);
|
||||
Assert.assertArrayEquals(list.toArray(), ArrayUtils.toList(new int[]{1, 3}).toArray());
|
||||
try {
|
||||
list.remove(2);
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// as expected
|
||||
}
|
||||
try {
|
||||
list.remove(-1);
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// as expected
|
||||
}
|
||||
Assert.assertArrayEquals(list.toArrayPrimitive(), new int[]{1, 3});
|
||||
Assert.assertEquals(list.remove(0).intValue(), 1);
|
||||
Assert.assertArrayEquals(list.toArrayPrimitive(), new int[]{3});
|
||||
Assert.assertTrue(list.remove(Integer.valueOf(3)));
|
||||
Assert.assertArrayEquals(list.toArrayPrimitive(), new int[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveAll() {
|
||||
var array1 = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 6, 7, 8};
|
||||
var array2 = new int[]{3, 4, 5};
|
||||
var array3 = new int[]{6, 7, 8};
|
||||
var list1 = new ArrayIntList(array1);
|
||||
var list2 = new ArrayIntList(array2);
|
||||
var list3 = new ArrayIntList(array3);
|
||||
list1.removeAll(list3);
|
||||
Assert.assertArrayEquals(list1.toArrayPrimitive(), new int[]{0, 1, 2, 3, 4, 5});
|
||||
|
||||
list1.addAll(list3);
|
||||
list1.retainAll(list2);
|
||||
Assert.assertArrayEquals(list1.toArrayPrimitive(), new int[]{3, 4, 5});
|
||||
Assert.assertEquals(list1.size(), 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsert() {
|
||||
var list = new ArrayIntList(1);
|
||||
for (var i = 1; i <= 3; i++) {
|
||||
list.add(i);
|
||||
}
|
||||
Assert.assertArrayEquals(list.toArrayPrimitive(), new int[]{1, 2, 3});
|
||||
list.add(0, 4);
|
||||
Assert.assertArrayEquals(list.toArrayPrimitive(), new int[]{4, 1, 2, 3});
|
||||
list.add(2, 5);
|
||||
Assert.assertArrayEquals(list.toArrayPrimitive(), new int[]{4, 1, 5, 2, 3});
|
||||
list.add(5, 6);
|
||||
Assert.assertArrayEquals(list.toArrayPrimitive(), new int[]{4, 1, 5, 2, 3, 6});
|
||||
try {
|
||||
list.add(8, 7);
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// as expected
|
||||
}
|
||||
try {
|
||||
list.add(-1, 7);
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// as expected
|
||||
}
|
||||
Assert.assertArrayEquals(list.toArrayPrimitive(), new int[]{4, 1, 5, 2, 3, 6});
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSet() {
|
||||
var list = new ArrayIntList(1);
|
||||
for (var i = 1; i <= 3; i++) {
|
||||
list.add(i);
|
||||
}
|
||||
Assert.assertArrayEquals(list.toArrayPrimitive(), new int[]{1, 2, 3});
|
||||
list.set(0, 4);
|
||||
Assert.assertArrayEquals(list.toArrayPrimitive(), new int[]{4, 2, 3});
|
||||
list.set(2, 5);
|
||||
Assert.assertArrayEquals(list.toArrayPrimitive(), new int[]{4, 2, 5});
|
||||
try {
|
||||
list.set(3, 6);
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// as expected
|
||||
}
|
||||
try {
|
||||
list.set(-1, 6);
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// as expected
|
||||
}
|
||||
Assert.assertArrayEquals(list.toArrayPrimitive(), new int[]{4, 2, 5});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddRemoveLast() {
|
||||
var list = new ArrayIntList(1);
|
||||
for (var i = 1; i <= 3; i++) {
|
||||
list.add(i);
|
||||
}
|
||||
Assert.assertArrayEquals(list.toArrayPrimitive(), new int[]{1, 2, 3});
|
||||
list.add(4);
|
||||
Assert.assertArrayEquals(list.toArrayPrimitive(), new int[]{1, 2, 3, 4});
|
||||
Assert.assertEquals(list.getPrimitive(list.size() - 1), 4);
|
||||
Assert.assertEquals(list.remove(list.size() - 1).intValue(), 4);
|
||||
Assert.assertArrayEquals(list.toArrayPrimitive(), new int[]{1, 2, 3});
|
||||
for (int i = 3; i >= 1; i--) {
|
||||
Assert.assertEquals(list.remove(list.size() - 1).intValue(), i);
|
||||
}
|
||||
try {
|
||||
list.getPrimitive(list.size() - 1);
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// as expected
|
||||
}
|
||||
try {
|
||||
list.remove(list.size() - 1);
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// as expected
|
||||
}
|
||||
Assert.assertArrayEquals(list.toArrayPrimitive(), new int[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIndexOf() {
|
||||
var srcArray = new int[]{1, 2, 3, 2, 1, 2, 3, 2, 1};
|
||||
var list = new ArrayIntList(0);
|
||||
list.addAll(ArrayUtils.toList(srcArray));
|
||||
Assert.assertArrayEquals(list.toArrayPrimitive(), srcArray);
|
||||
Assert.assertEquals(list.indexOf(1), 0);
|
||||
Assert.assertEquals(list.indexOf(2), 1);
|
||||
Assert.assertEquals(list.indexOf(3), 2);
|
||||
Assert.assertEquals(list.indexOf(4), -1);
|
||||
Assert.assertEquals(list.lastIndexOf(1), 8);
|
||||
Assert.assertEquals(list.lastIndexOf(2), 7);
|
||||
Assert.assertEquals(list.lastIndexOf(3), 6);
|
||||
Assert.assertEquals(list.lastIndexOf(4), -1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIterator() {
|
||||
var list = new ArrayIntList(0);
|
||||
for (var i = 0; i < 42; i++) {
|
||||
Assert.assertTrue(list.add(i));
|
||||
}
|
||||
Assert.assertEquals(list.size(), 42);
|
||||
var it = list.iterator();
|
||||
for (var i = 0; i < 42; i++) {
|
||||
Assert.assertTrue(it.hasNext());
|
||||
Assert.assertEquals(it.next().intValue(), i);
|
||||
}
|
||||
Assert.assertFalse(it.hasNext());
|
||||
try {
|
||||
it.next();
|
||||
} catch (NoSuchElementException e) {
|
||||
// as expected
|
||||
}
|
||||
|
||||
var index = 0;
|
||||
for (var e : list) {
|
||||
Assert.assertEquals(e, list.get(index++));
|
||||
}
|
||||
|
||||
index = 0;
|
||||
var iterator = list.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Assert.assertEquals(iterator.next(), list.get(index++));
|
||||
}
|
||||
|
||||
index = 0;
|
||||
var listIterator = list.listIterator();
|
||||
while (listIterator.hasNext()) {
|
||||
Assert.assertEquals(listIterator.next(), list.get(index++));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.protocol.collection;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
* @version 3.0
|
||||
*/
|
||||
public class FixedCollectionTest {
|
||||
|
||||
@Test
|
||||
public void testFixedSizeListInt() {
|
||||
var list = new FixedSizeListInt(3);
|
||||
list.set(0, 1);
|
||||
list.set(1, 2);
|
||||
list.set(2, 3);
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
for (var ele : list) {
|
||||
// test iterator
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFixedSizeList() {
|
||||
var list = new FixedSizeList(3);
|
||||
list.set(0, "1");
|
||||
list.set(1, "2");
|
||||
list.set(2, "3");
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
for (var ele : list) {
|
||||
// test iterator
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testHashIntSet() {
|
||||
var set = new HashIntSet(3);
|
||||
set.add(1);
|
||||
set.add(2);
|
||||
set.add(3);
|
||||
for (int i = 0; i < set.size(); i++) {
|
||||
for (var ele : set) {
|
||||
// test iterator
|
||||
}
|
||||
}
|
||||
|
||||
Assert.assertTrue(set.contains(3));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user