001package jmri.util; 002 003import java.util.Objects; 004 005import javax.annotation.Nonnull; 006 007/** 008 * Useful array methods. 009 * 010 * @author Bob Jacobsen 2022 011 */ 012public final class ArrayUtil { 013 014 // Class only supplies static methods 015 private ArrayUtil() {} 016 017 /** 018 * Reverse an array of objects. 019 * <p> 020 * Not suitable for primitive types. 021 * 022 * @param <T> the Type of the array contents 023 * @param elementsArray the array 024 * @return the reversed array 025 */ 026 public static <T> T[] reverse(@Nonnull T[] elementsArray) { 027 T[] elements = Objects.requireNonNull(elementsArray); 028 var results = java.util.Arrays.copyOf(elements, elements.length); 029 for (int i = 0; i < elements.length; i++) { 030 results[i] = elements[elements.length-i-1]; 031 } 032 return results; 033 } 034 035 /** 036 * Reverse an array of ints. 037 * 038 * @param elementsArray the array 039 * @return the reversed array 040 */ 041 public static int[] reverse(@Nonnull int[] elementsArray) { 042 int[] elements = Objects.requireNonNull(elementsArray); 043 var results = new int[elements.length]; 044 for (int i = 0; i < elements.length; i++) { 045 results[i] = elements[elements.length-i-1]; 046 } 047 return results; 048 } 049 050 /** 051 * Reverse an array of longs. 052 * 053 * @param elementsArray the array 054 * @return the reversed array 055 */ 056 public static long[] reverse(@Nonnull long[] elementsArray) { 057 long[] elements = Objects.requireNonNull(elementsArray); 058 var results = new long[elements.length]; 059 for (int i = 0; i < elements.length; i++) { 060 results[i] = elements[elements.length-i-1]; 061 } 062 return results; 063 } 064 065 /** 066 * Reverse an array of doubles. 067 * 068 * @param elementsArray the array 069 * @return the reversed array 070 */ 071 public static double[] reverse(@Nonnull double[] elementsArray) { 072 double[] elements = Objects.requireNonNull(elementsArray); 073 var results = new double[elements.length]; 074 for (int i = 0; i < elements.length; i++) { 075 results[i] = elements[elements.length-i-1]; 076 } 077 return results; 078 } 079 080 /** 081 * Reverse an array of floats. 082 * 083 * @param elementsArray the array 084 * @return the reversed array 085 */ 086 public static float[] reverse(@Nonnull float[] elementsArray) { 087 float[] elements = Objects.requireNonNull(elementsArray); 088 var results = new float[elements.length]; 089 for (int i = 0; i < elements.length; i++) { 090 results[i] = elements[elements.length-i-1]; 091 } 092 return results; 093 } 094 095 /** 096 * Reverse an array of booleans. 097 * 098 * @param elementsArray the array 099 * @return the reversed array 100 */ 101 public static boolean[] reverse(@Nonnull boolean[] elementsArray) { 102 boolean[] elements = Objects.requireNonNull(elementsArray); 103 var results = new boolean[elements.length]; 104 for (int i = 0; i < elements.length; i++) { 105 results[i] = elements[elements.length-i-1]; 106 } 107 return results; 108 } 109 110 // private transient final static Logger log = LoggerFactory.getLogger(ArrayUtil.class); 111}