feat[list]: 增加FixedSizeListByte

This commit is contained in:
godotg
2022-09-23 21:21:11 +08:00
parent 5f7b942d1f
commit 8783790724
4 changed files with 164 additions and 7 deletions
@@ -831,9 +831,9 @@ public abstract class ByteBufUtils {
public static List<Byte> readByteList(ByteBuf byteBuf) {
var length = readInt(byteBuf);
var list = (List<Byte>) 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<Integer> 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));
}
@@ -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<Byte> {
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> 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<Byte> iterator() {
return ArrayUtils.toList(array).iterator();
}
@Override
public ListIterator<Byte> listIterator() {
return ArrayUtils.toList(array).listIterator();
}
@Override
public ListIterator<Byte> listIterator(int index) {
return ArrayUtils.toList(array).listIterator(index);
}
@Override
public List<Byte> 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<? extends Byte> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(int index, Collection<? extends Byte> 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();
}
}
@@ -21,11 +21,11 @@ import java.util.ListIterator;
* @author godotg
* @version 3.0
*/
public class FixedSizeIntList implements List<Integer> {
public class FixedSizeListInt implements List<Integer> {
private final int[] array;
public FixedSizeIntList(int initialCapacity) {
public FixedSizeListInt(int initialCapacity) {
this.array = new int[initialCapacity];
}
@@ -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);