xiangfu0 commented on code in PR #13354: URL: https://github.com/apache/pinot/pull/13354#discussion_r1634393371
########## pinot-common/src/main/java/org/apache/pinot/common/utils/ArrayListUtils.java: ########## @@ -0,0 +1,124 @@ +/** + * 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.pinot.common.utils; + +import it.unimi.dsi.fastutil.doubles.DoubleArrayList; +import it.unimi.dsi.fastutil.floats.FloatArrayList; +import it.unimi.dsi.fastutil.ints.IntArrayList; +import it.unimi.dsi.fastutil.longs.LongArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; + + +/** + * Utility class for {@link IntArrayList}, {@link LongArrayList}, {@link FloatArrayList}, {@link DoubleArrayList}, + * {@link ObjectArrayList}. + */ +public class ArrayListUtils { + private ArrayListUtils() { + } + + /** + * Best effort extract the given {@link IntArrayList} to an int array without copying the elements. + * The {@link IntArrayList#elements()} returned int array may be longer than the actual size of the + * {@link IntArrayList}, and the actual size of the {@link IntArrayList} can be retrieved using + * {@link IntArrayList#size()}. + * This method checks the length of the returned int array and returns the same if it is equal to the size of the + * {@link IntArrayList}, otherwise, it copies the elements to a new int array and returns it. + * + * <p>Use this method only if you are sure that the returned int array will not be modified. + * <p>Otherwise, use {@link IntArrayList#toIntArray()}. + * + * @param intArrayList Input {@link IntArrayList} + * @return Best effort extracted int array without copying the elements + */ + public static int[] toIntArray(IntArrayList intArrayList) { + int[] intArrayListElements = intArrayList.elements(); + return intArrayListElements.length == intArrayList.size() ? intArrayListElements : intArrayList.toIntArray(); + } + + /** + * Best effort extract the given {@link LongArrayList} to a long array without copying the elements. + * The {@link LongArrayList#elements()} returned long array may be longer than the actual size of the + * {@link LongArrayList}, and the actual size of the {@link LongArrayList} can be retrieved using + * {@link LongArrayList#size()}. + * This method checks the length of the returned long array and returns the same if it is equal to the size of the + * {@link LongArrayList}, otherwise, it copies the elements to a new long array and returns it. + * + * <p>Use this method only if you are sure that the returned long array will not be modified. + * <p>Otherwise, use {@link LongArrayList#toLongArray()}. + * + * @param longArrayList Input {@link LongArrayList} + * @return Best effort extracted long array without copying the elements + */ + public static long[] toLongArray(LongArrayList longArrayList) { + long[] longArrayListElements = longArrayList.elements(); + return longArrayListElements.length == longArrayList.size() ? longArrayListElements : longArrayList.toLongArray(); + } + + /** + * Best effort extract the given {@link FloatArrayList} to a float array without copying the elements. + * The {@link FloatArrayList#elements()} returned float array may be longer than the actual size of the + * {@link FloatArrayList}, and the actual size of the {@link FloatArrayList} can be retrieved using + * {@link FloatArrayList#size()}. + * This method checks the length of the returned float array and returns the same if it is equal to the size of the + * {@link FloatArrayList}, otherwise, it copies the elements to a new float array and returns it. + * + * <p>Use this method only if you are sure that the returned float array will not be modified. + * <p>Otherwise, use {@link FloatArrayList#toFloatArray()}. + * + * @param floatArrayList Input {@link FloatArrayList} + * @return Best effort extracted float array without copying the elements + */ + public static float[] toFloatArray(FloatArrayList floatArrayList) { + float[] floatArrayListElements = floatArrayList.elements(); + return floatArrayListElements.length == floatArrayList.size() ? floatArrayListElements + : floatArrayList.toFloatArray(); + } + + /** + * Best effort extract the given {@link DoubleArrayList} to a double array without copying the elements. + * The {@link DoubleArrayList#elements()} returned double array may be longer than the actual size of the + * {@link DoubleArrayList}, and the actual size of the {@link DoubleArrayList} can be retrieved using + * {@link DoubleArrayList#size()}. + * This method checks the length of the returned double array and returns the same if it is equal to the size of the + * {@link DoubleArrayList}, otherwise, it copies the elements to a new double array and returns it. + * + * <p>Use this method only if you are sure that the returned double array will not be modified. + * <p>Otherwise, use {@link DoubleArrayList#toDoubleArray()}. + * + * @param doubleArrayList Input {@link DoubleArrayList} + * @return Best effort extracted double array without copying the elements + */ + public static double[] toDoubleArray(DoubleArrayList doubleArrayList) { + double[] doubleArrayListElements = doubleArrayList.elements(); + return doubleArrayListElements.length == doubleArrayList.size() ? doubleArrayListElements + : doubleArrayList.toDoubleArray(); + } + + /** + * Convert the given {@link ObjectArrayList} to a string array. + * This method always copies the elements to a new string array and returns it. + * + * @param stringArrayList Input {@link ObjectArrayList} + * @return Copied string array + */ + public static String[] toStringArray(ObjectArrayList<String> stringArrayList) { + return stringArrayList.toArray(new String[0]); Review Comment: `ObjectArrayList#elements()` might return either Object[] or String[] depends on the interal implementation. So the type cast might not work always. Updated this logic to check this scenario. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
