diff --git a/java/fury-core/src/main/java/org/apache/fury/serializer/ArraySerializers.java b/java/fury-core/src/main/java/org/apache/fury/serializer/ArraySerializers.java index cf8790edf5..6ed4284d20 100644 --- a/java/fury-core/src/main/java/org/apache/fury/serializer/ArraySerializers.java +++ b/java/fury-core/src/main/java/org/apache/fury/serializer/ArraySerializers.java @@ -27,7 +27,6 @@ import org.apache.fury.resolver.ClassInfoHolder; import org.apache.fury.resolver.ClassResolver; import org.apache.fury.resolver.RefResolver; -import org.apache.fury.serializer.collection.ArrayAsList; import org.apache.fury.serializer.collection.CollectionFlags; import org.apache.fury.serializer.collection.FuryArrayAsListSerializer; import org.apache.fury.type.Type; @@ -545,14 +544,14 @@ public double[] read(MemoryBuffer buffer) { public static final class StringArraySerializer extends Serializer { private final StringSerializer stringSerializer; private final FuryArrayAsListSerializer collectionSerializer; - private final ArrayAsList list; + private final FuryArrayAsListSerializer.ArrayAsList list; public StringArraySerializer(Fury fury) { super(fury, String[].class); stringSerializer = new StringSerializer(fury); collectionSerializer = new FuryArrayAsListSerializer(fury); collectionSerializer.setElementSerializer(stringSerializer); - list = new ArrayAsList<>(0); + list = new FuryArrayAsListSerializer.ArrayAsList<>(0); } @Override @@ -571,7 +570,7 @@ public void write(MemoryBuffer buffer, String[] value) { // TODO reference support // this method won't throw exception. int flags = collectionSerializer.writeNullabilityHeader(buffer, list); - list.setArray(null); // clear for gc + list.clearArray(); // clear for gc StringSerializer stringSerializer = this.stringSerializer; if ((flags & CollectionFlags.HAS_NULL) != CollectionFlags.HAS_NULL) { for (String elem : value) { diff --git a/java/fury-core/src/main/java/org/apache/fury/serializer/collection/ArrayAsList.java b/java/fury-core/src/main/java/org/apache/fury/serializer/collection/ArrayAsList.java deleted file mode 100644 index 443ebc6718..0000000000 --- a/java/fury-core/src/main/java/org/apache/fury/serializer/collection/ArrayAsList.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 org.apache.fury.serializer.collection; - -import java.util.AbstractList; -import java.util.Iterator; -import java.util.Objects; -import java.util.RandomAccess; -import org.apache.fury.annotation.Internal; - -/** - * A List which wrap a Java array like `java.util.Arrays.ArrayList`, but allow to replace wrapped - * array. Used for serialization only, do not use it in other scenarios. - */ -@Internal -public class ArrayAsList extends AbstractList implements RandomAccess, java.io.Serializable { - private E[] array; - private int size; - - @SuppressWarnings("unchecked") - public ArrayAsList(int size) { - array = (E[]) new Object[size]; - } - - public ArrayAsList(E[] array) { - this.array = Objects.requireNonNull(array); - size = array.length; - } - - @Override - public int size() { - return size; - } - - @Override - public boolean add(E e) { - array[size++] = e; - return true; - } - - @Override - public E get(int index) { - return array[index]; - } - - @Override - public E set(int index, E element) { - E prev = array[index]; - array[index] = element; - return prev; - } - - public void setArray(E[] a) { - this.array = a; - } - - public E[] getArray() { - return array; - } - - @Override - public int indexOf(Object o) { - E[] array = this.array; - if (o == null) { - for (int i = 0; i < array.length; i++) { - if (array[i] == null) { - return i; - } - } - } else { - for (int i = 0; i < array.length; i++) { - if (o.equals(array[i])) { - return i; - } - } - } - return -1; - } - - @Override - public boolean contains(Object o) { - return indexOf(o) >= 0; - } - - /** Returns original array without copy. */ - @Override - public Object[] toArray() { - return array; - } - - @Override - public Iterator iterator() { - return new Iterator() { - private int index; - - @Override - public boolean hasNext() { - return index < array.length; - } - - @Override - public E next() { - return array[index++]; - } - }; - } -} diff --git a/java/fury-core/src/main/java/org/apache/fury/serializer/collection/FuryArrayAsListSerializer.java b/java/fury-core/src/main/java/org/apache/fury/serializer/collection/FuryArrayAsListSerializer.java index dd11d0dfe2..727cb811ad 100644 --- a/java/fury-core/src/main/java/org/apache/fury/serializer/collection/FuryArrayAsListSerializer.java +++ b/java/fury-core/src/main/java/org/apache/fury/serializer/collection/FuryArrayAsListSerializer.java @@ -19,7 +19,10 @@ package org.apache.fury.serializer.collection; +import java.util.AbstractList; import java.util.Collection; +import java.util.Iterator; +import java.util.RandomAccess; import org.apache.fury.Fury; import org.apache.fury.annotation.Internal; import org.apache.fury.memory.MemoryBuffer; @@ -28,7 +31,8 @@ /** Serializer for {@link ArrayAsList}. Helper for serialization of other classes. */ @Internal @SuppressWarnings("rawtypes") -public final class FuryArrayAsListSerializer extends CollectionSerializer { +public final class FuryArrayAsListSerializer + extends CollectionSerializer { public FuryArrayAsListSerializer(Fury fury) { super(fury, ArrayAsList.class, true); } @@ -43,4 +47,91 @@ public Collection newCollection(MemoryBuffer buffer) { setNumElements(numElements); return new ArrayAsList(numElements); } + + /** + * A List which wrap a Java array like `java.util.Arrays.ArrayList`, but allow to replace wrapped + * array. Used for serialization only, do not use it in other scenarios. + */ + @Internal + public static class ArrayAsList extends AbstractList + implements RandomAccess, java.io.Serializable { + private static final Object[] EMPTY = new Object[0]; + + private E[] array; + private int size; + + @SuppressWarnings("unchecked") + public ArrayAsList(int size) { + array = (E[]) new Object[size]; + } + + @Override + public int size() { + return size; + } + + @Override + public boolean add(E e) { + array[size++] = e; + return true; + } + + @Override + public E get(int index) { + return array[index]; + } + + @SuppressWarnings("unchecked") + public void clearArray() { + size = 0; + array = (E[]) EMPTY; + } + + public void setArray(E[] a) { + array = a; + size = a.length; + } + + public E[] getArray() { + return array; + } + + @Override + public E set(int index, E element) { + throw new UnsupportedOperationException(); + } + + @Override + public int indexOf(Object o) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean contains(Object o) { + throw new UnsupportedOperationException(); + } + + /** Returns original array without copy. */ + @Override + public Object[] toArray() { + return array; + } + + @Override + public Iterator iterator() { + return new Iterator() { + private int index; + + @Override + public boolean hasNext() { + return index < array.length; + } + + @Override + public E next() { + return array[index++]; + } + }; + } + } }