From 87837907241418f559db234021d32bafbd7bf2d5 Mon Sep 17 00:00:00 2001 From: godotg Date: Fri, 23 Sep 2022 21:21:11 +0800 Subject: [PATCH] =?UTF-8?q?feat[list]:=20=E5=A2=9E=E5=8A=A0FixedSizeListBy?= =?UTF-8?q?te?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zfoo/protocol/buffer/ByteBufUtils.java | 6 +- .../collection/FixedSizeListByte.java | 157 ++++++++++++++++++ ...SizeIntList.java => FixedSizeListInt.java} | 4 +- .../zfoo/protocol/collection/ListTest.java | 4 +- 4 files changed, 164 insertions(+), 7 deletions(-) create mode 100644 protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeListByte.java rename protocol/src/main/java/com/zfoo/protocol/collection/{FixedSizeIntList.java => FixedSizeListInt.java} (97%) diff --git a/protocol/src/main/java/com/zfoo/protocol/buffer/ByteBufUtils.java b/protocol/src/main/java/com/zfoo/protocol/buffer/ByteBufUtils.java index c0c676e7..300d6d92 100644 --- a/protocol/src/main/java/com/zfoo/protocol/buffer/ByteBufUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/buffer/ByteBufUtils.java @@ -831,9 +831,9 @@ public abstract class ByteBufUtils { public static List readByteList(ByteBuf byteBuf) { var length = readInt(byteBuf); - var list = (List) CollectionUtils.newFixedList(length); + var list = new FixedSizeListByte(length); for (var i = 0; i < length; i++) { - list.add(readByteBox(byteBuf)); + list.set(i, readByte(byteBuf)); } return list; } @@ -996,7 +996,7 @@ public abstract class ByteBufUtils { public static List readIntList(ByteBuf byteBuf) { var length = readInt(byteBuf); - var list = new FixedSizeIntList(length); + var list = new FixedSizeListInt(length); for (var i = 0; i < length; i++) { list.set(i, readInt(byteBuf)); } diff --git a/protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeListByte.java b/protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeListByte.java new file mode 100644 index 00000000..45810b5b --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeListByte.java @@ -0,0 +1,157 @@ +/* + * 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 FixedSizeListByte implements List { + + private final byte[] array; + + public FixedSizeListByte(int initialCapacity) { + this.array = new byte[initialCapacity]; + } + + @Override + public int size() { + return array.length; + } + + @Override + public boolean isEmpty() { + return ArrayUtils.isEmpty(array); + } + + @Override + public Byte get(int index) { + return array[index]; + } + + public byte getRaw(int index) { + return array[index]; + } + + @Override + public Byte set(int index, Byte ele) { + var old = array[index]; + array[index] = ele; + return old; + } + + public void set(int index, byte ele) { + array[index] = ele; + } + + @Override + public boolean contains(Object ele) { + return ArrayUtils.toList(array).stream().anyMatch(it -> ele.equals(it)); + } + + @Override + public Object[] toArray() { + return ArrayUtils.toList(array).toArray(); + } + + @Override + public 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 iterator() { + return ArrayUtils.toList(array).iterator(); + } + + @Override + public ListIterator listIterator() { + return ArrayUtils.toList(array).listIterator(); + } + + @Override + public ListIterator listIterator(int index) { + return ArrayUtils.toList(array).listIterator(index); + } + + @Override + public List subList(int fromIndex, int toIndex) { + return ArrayUtils.toList(array).subList(fromIndex, toIndex); + } + + @Override + public boolean add(Byte e) { + throw new UnsupportedOperationException(); + } + + @Override + public void add(int index, Byte element) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean addAll(Collection collection) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean addAll(int index, Collection collection) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean remove(Object o) { + throw new UnsupportedOperationException(); + } + + @Override + public Byte 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(); + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeIntList.java b/protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeListInt.java similarity index 97% rename from protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeIntList.java rename to protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeListInt.java index a751826a..d5306048 100644 --- a/protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeIntList.java +++ b/protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeListInt.java @@ -21,11 +21,11 @@ import java.util.ListIterator; * @author godotg * @version 3.0 */ -public class FixedSizeIntList implements List { +public class FixedSizeListInt implements List { private final int[] array; - public FixedSizeIntList(int initialCapacity) { + public FixedSizeListInt(int initialCapacity) { this.array = new int[initialCapacity]; } diff --git a/protocol/src/test/java/com/zfoo/protocol/collection/ListTest.java b/protocol/src/test/java/com/zfoo/protocol/collection/ListTest.java index bad50c8d..c3cf8d7c 100644 --- a/protocol/src/test/java/com/zfoo/protocol/collection/ListTest.java +++ b/protocol/src/test/java/com/zfoo/protocol/collection/ListTest.java @@ -21,8 +21,8 @@ import org.junit.Test; public class ListTest { @Test - public void testFixedSizeIntList() { - var list = new FixedSizeIntList(3); + public void testFixedSizeListInt() { + var list = new FixedSizeListInt(3); list.set(0, 1); list.set(1, 2); list.set(2, 3);