|
| 1 | +/* |
| 2 | + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion |
| 3 | + * Copyright (C) 2016-2024 ViaVersion and contributors |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | +package com.viaversion.viaversion.util; |
| 19 | + |
| 20 | +import java.util.Arrays; |
| 21 | + |
| 22 | +/** |
| 23 | + * For type safety and effort reasons, buffer types use arrays instead of lists. |
| 24 | + * <p> |
| 25 | + * This class includes simple methods to work with these arrays in case they need to be modified |
| 26 | + * (obviously being more expensive due to the required array copies for every modification). |
| 27 | + */ |
| 28 | +public final class ArrayUtil { |
| 29 | + |
| 30 | + public static <T> T[] add(final T[] array, final T element) { |
| 31 | + final int length = array.length; |
| 32 | + final T[] newArray = Arrays.copyOf(array, length + 1); |
| 33 | + newArray[length] = element; |
| 34 | + return newArray; |
| 35 | + } |
| 36 | + |
| 37 | + @SafeVarargs |
| 38 | + public static <T> T[] add(final T[] array, final T... elements) { |
| 39 | + final int length = array.length; |
| 40 | + final T[] newArray = Arrays.copyOf(array, length + elements.length); |
| 41 | + System.arraycopy(elements, 0, newArray, length, elements.length); |
| 42 | + return newArray; |
| 43 | + } |
| 44 | + |
| 45 | + public static <T> T[] remove(final T[] array, final int index) { |
| 46 | + final T[] newArray = Arrays.copyOf(array, array.length - 1); |
| 47 | + System.arraycopy(array, index + 1, newArray, index, newArray.length - index); |
| 48 | + return newArray; |
| 49 | + } |
| 50 | +} |
0 commit comments