From c084e16e14196137038500fe656a84fa7dfea3bb Mon Sep 17 00:00:00 2001 From: godotg Date: Fri, 23 Sep 2022 21:02:53 +0800 Subject: [PATCH] =?UTF-8?q?feat[list]:=20=E6=B7=BB=E5=8A=A0=E6=80=A7?= =?UTF-8?q?=E8=83=BD=E6=9B=B4=E5=8A=A0=E9=AB=98=E7=9A=84=E5=8E=9F=E7=94=9F?= =?UTF-8?q?=E5=9F=BA=E7=A1=80=E7=B1=BB=E5=9E=8B=E7=9A=84=E5=9B=BA=E5=AE=9A?= =?UTF-8?q?=E5=A4=A7=E5=B0=8Flist?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../collection/FixedSizeBooleanList.java | 157 +++++++++++++++++ .../collection/FixedSizeDoubleList.java | 157 +++++++++++++++++ .../collection/FixedSizeFloatList.java | 157 +++++++++++++++++ .../protocol/collection/FixedSizeIntList.java | 158 ++++++++++++++++++ .../collection/FixedSizeLongList.java | 157 +++++++++++++++++ .../collection/FixedSizeShortList.java | 157 +++++++++++++++++ .../collection/FixedSizeStringList.java | 149 +++++++++++++++++ .../zfoo/protocol/collection/ListTest.java | 49 ++++++ 8 files changed, 1141 insertions(+) create mode 100644 protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeBooleanList.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeDoubleList.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeFloatList.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeIntList.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeLongList.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeShortList.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeStringList.java create mode 100644 protocol/src/test/java/com/zfoo/protocol/collection/ListTest.java diff --git a/protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeBooleanList.java b/protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeBooleanList.java new file mode 100644 index 00000000..231de816 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeBooleanList.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 FixedSizeBooleanList implements List { + + private final boolean[] array; + + public FixedSizeBooleanList(int initialCapacity) { + this.array = new boolean[initialCapacity]; + } + + @Override + public int size() { + return array.length; + } + + @Override + public boolean isEmpty() { + return ArrayUtils.isEmpty(array); + } + + @Override + public Boolean get(int index) { + return array[index]; + } + + public boolean getRaw(int index) { + return array[index]; + } + + @Override + public Boolean set(int index, Boolean ele) { + var old = array[index]; + array[index] = ele; + return old; + } + + public void set(int index, boolean 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(Boolean e) { + throw new UnsupportedOperationException(); + } + + @Override + public void add(int index, Boolean 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 Boolean 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/FixedSizeDoubleList.java b/protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeDoubleList.java new file mode 100644 index 00000000..9b995831 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeDoubleList.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 FixedSizeDoubleList implements List { + + private final double[] array; + + public FixedSizeDoubleList(int initialCapacity) { + this.array = new double[initialCapacity]; + } + + @Override + public int size() { + return array.length; + } + + @Override + public boolean isEmpty() { + return ArrayUtils.isEmpty(array); + } + + @Override + public Double get(int index) { + return array[index]; + } + + public double getRaw(int index) { + return array[index]; + } + + @Override + public Double set(int index, Double ele) { + var old = array[index]; + array[index] = ele; + return old; + } + + public void set(int index, double 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(Double e) { + throw new UnsupportedOperationException(); + } + + @Override + public void add(int index, Double 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 Double 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/FixedSizeFloatList.java b/protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeFloatList.java new file mode 100644 index 00000000..c831a82d --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeFloatList.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 FixedSizeFloatList implements List { + + private final float[] array; + + public FixedSizeFloatList(int initialCapacity) { + this.array = new float[initialCapacity]; + } + + @Override + public int size() { + return array.length; + } + + @Override + public boolean isEmpty() { + return ArrayUtils.isEmpty(array); + } + + @Override + public Float get(int index) { + return array[index]; + } + + public float getRaw(int index) { + return array[index]; + } + + @Override + public Float set(int index, Float ele) { + var old = array[index]; + array[index] = ele; + return old; + } + + public void set(int index, float 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(Float e) { + throw new UnsupportedOperationException(); + } + + @Override + public void add(int index, Float 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 Float 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/FixedSizeIntList.java new file mode 100644 index 00000000..a751826a --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeIntList.java @@ -0,0 +1,158 @@ +/* + * 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 FixedSizeIntList implements List { + + private final int[] array; + + public FixedSizeIntList(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[] 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(Integer e) { + throw new UnsupportedOperationException(); + } + + @Override + public void add(int index, Integer 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 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(); + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeLongList.java b/protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeLongList.java new file mode 100644 index 00000000..dcae016e --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeLongList.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 FixedSizeLongList implements List { + + private final long[] array; + + public FixedSizeLongList(int initialCapacity) { + this.array = new long[initialCapacity]; + } + + @Override + public int size() { + return array.length; + } + + @Override + public boolean isEmpty() { + return ArrayUtils.isEmpty(array); + } + + @Override + public Long get(int index) { + return array[index]; + } + + public long getRaw(int index) { + return array[index]; + } + + @Override + public Long set(int index, Long ele) { + var old = array[index]; + array[index] = ele; + return old; + } + + public void set(int index, long 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(Long e) { + throw new UnsupportedOperationException(); + } + + @Override + public void add(int index, Long 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 Long 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/FixedSizeShortList.java b/protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeShortList.java new file mode 100644 index 00000000..74a4e5bc --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeShortList.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 FixedSizeShortList implements List { + + private final short[] array; + + public FixedSizeShortList(int initialCapacity) { + this.array = new short[initialCapacity]; + } + + @Override + public int size() { + return array.length; + } + + @Override + public boolean isEmpty() { + return ArrayUtils.isEmpty(array); + } + + @Override + public Short get(int index) { + return array[index]; + } + + public short getRaw(int index) { + return array[index]; + } + + @Override + public Short set(int index, Short ele) { + var old = array[index]; + array[index] = ele; + return old; + } + + public void set(int index, short 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(Short e) { + throw new UnsupportedOperationException(); + } + + @Override + public void add(int index, Short 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 Short 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/FixedSizeStringList.java b/protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeStringList.java new file mode 100644 index 00000000..a20d95b4 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/collection/FixedSizeStringList.java @@ -0,0 +1,149 @@ +/* + * 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 FixedSizeStringList implements List { + + private final String[] array; + + public FixedSizeStringList(int initialCapacity) { + this.array = new String[initialCapacity]; + } + + @Override + public int size() { + return array.length; + } + + @Override + public boolean isEmpty() { + return ArrayUtils.isEmpty(array); + } + + @Override + public String get(int index) { + return array[index]; + } + + @Override + public String set(int index, String ele) { + var old = array[index]; + array[index] = ele; + return old; + } + + @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(String e) { + throw new UnsupportedOperationException(); + } + + @Override + public void add(int index, String 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 String 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/test/java/com/zfoo/protocol/collection/ListTest.java b/protocol/src/test/java/com/zfoo/protocol/collection/ListTest.java new file mode 100644 index 00000000..58d0a705 --- /dev/null +++ b/protocol/src/test/java/com/zfoo/protocol/collection/ListTest.java @@ -0,0 +1,49 @@ +/* + * 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.Test; + +/** + * @author godotg + * @version 3.0 + */ +public class ListTest { + + @Test + public void testFixedSizeIntList() { + var list = new FixedSizeIntList(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 testFixedSizeStringList() { + var list = new FixedSizeStringList(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 + } + } + } + +}