|
| 1 | +/* |
| 2 | + * Copyright (C) 2024-2025 OnixByte. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.onixbyte.devkit.utils; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +/** |
| 24 | + * A utility class for splitting a List into multiple sub lists, where each sublist has a maximum |
| 25 | + * number of elements specified by the user. |
| 26 | + * |
| 27 | + * @author siujamo |
| 28 | + */ |
| 29 | +public final class ListUtil { |
| 30 | + |
| 31 | + /** |
| 32 | + * Private constructor to prevent instantiation of this utility class. |
| 33 | + * <p> |
| 34 | + * This class provides static methods for list manipulation and is not intended to be |
| 35 | + * instantiated. The private constructor ensures that no instances can be created, enforcing |
| 36 | + * the utility nature of the class. |
| 37 | + */ |
| 38 | + private ListUtil() { |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Splits a given List into a List of sub lists, where each sublist contains at most |
| 43 | + * {@code maxSize} elements. The original list is not modified, and new sub lists are created |
| 44 | + * to hold the partitioned data. |
| 45 | + * <p> |
| 46 | + * If the original list's size is less than or equal to {@code maxSize}, a single sublist |
| 47 | + * containing all elements is returned. If the list is empty, an empty list of sub lists |
| 48 | + * is returned. |
| 49 | + * |
| 50 | + * @param <T> the type of elements in the list |
| 51 | + * @param originalList the list to be split, must not be null |
| 52 | + * @param maxSize the maximum number of elements in each sublist, must be positive |
| 53 | + * @return a List of sub lists, where each sublist has at most {@code maxSize} elements |
| 54 | + * @throws IllegalArgumentException if {@code originalList} is null or {@code maxSize} is less |
| 55 | + * than or equal to 0 |
| 56 | + */ |
| 57 | + public static <T> List<List<T>> splitList(List<T> originalList, int maxSize) { |
| 58 | + // check input |
| 59 | + if (originalList == null || maxSize <= 0) { |
| 60 | + throw new IllegalArgumentException("List cannot be null and maxSize must be positive"); |
| 61 | + } |
| 62 | + |
| 63 | + var result = new ArrayList<List<T>>(); |
| 64 | + var size = originalList.size(); |
| 65 | + |
| 66 | + // if the original list is empty or smaller than maxSize, return it as a single sublist |
| 67 | + if (size <= maxSize) { |
| 68 | + result.add(new ArrayList<>(originalList)); |
| 69 | + return result; |
| 70 | + } |
| 71 | + |
| 72 | + // split the list |
| 73 | + for (var i = 0; i < size; i += maxSize) { |
| 74 | + var end = Math.min(i + maxSize, size); // ensure not to exceed list length |
| 75 | + List<T> subList = originalList.subList(i, end); |
| 76 | + result.add(new ArrayList<>(subList)); // create a new list to avoid reference issues |
| 77 | + } |
| 78 | + |
| 79 | + return result; |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments