Coverage Summary for Class: ListArrayUtil (co.rsk.util)

Class Class, % Method, % Line, %
ListArrayUtil 100% (1/1) 42.9% (3/7) 16.7% (5/30)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2019 RSK Labs Ltd. 4  * 5  * This program is free software: you can redistribute it and/or modify 6  * it under the terms of the GNU Lesser 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 Lesser General Public License for more details. 14  * 15  * You should have received a copy of the GNU Lesser General Public License 16  * along with this program. If not, see <http://www.gnu.org/licenses/>. 17  */ 18  19 package co.rsk.util; 20  21 import javax.annotation.Nullable; 22 import java.util.Arrays; 23 import java.util.List; 24  25 /** 26  * Utility Class 27  * basic operations with arrays and lists 28  * @since 23/04/2019 29  * @author alexbraz 30  */ 31 public class ListArrayUtil { 32  private ListArrayUtil() { 33  // utility class 34  } 35  36  /** 37  * Converts primitive byte[] to List<Byte> 38  * 39  * @param primitiveByteArray The primitive array to convert, cannot be null. 40  * @return The input byte array as a List<Byte>, never null but can be empty. 41  */ 42  public static List<Byte> asByteList(byte[] primitiveByteArray) { 43  Byte[] arrayObj = convertTo(primitiveByteArray); 44  return Arrays.asList(arrayObj); 45  } 46  47  private static Byte[] convertTo(byte[] primitiveByteArray) { 48  Byte[] byteObjectArray = new Byte[primitiveByteArray.length]; 49  50  Arrays.setAll(byteObjectArray, n -> primitiveByteArray[n]); 51  52  return byteObjectArray; 53  } 54  55  /** 56  * @return true if the array is empty or null 57  */ 58  public static boolean isEmpty(@Nullable byte[] array) { 59  return array == null || array.length == 0; 60  } 61  62  /** 63  * @return the length of the array, or zero if null 64  */ 65  public static int getLength(@Nullable byte[] array) { 66  if (array == null) { 67  return 0; 68  } 69  70  return array.length; 71  } 72  73  /** 74  * @return the same array if not null, or else an empty array 75  */ 76  public static byte[] nullToEmpty(@Nullable byte[] array) { 77  if (array == null) { 78  return new byte[0]; 79  } 80  81  return array; 82  } 83  84  public static int lastIndexOfSubList(byte[] source, byte[] target) { 85  if (source == null || target == null) { 86  return -1; 87  } 88  if (target.length == 0) { 89  return source.length; 90  } 91  if (source.length < target.length) { 92  return -1; 93  } 94  95  final int max = source.length - target.length; 96  97  for (int i = max; i >= 0; i--) { 98  boolean found = true; 99  100  for (int j = 0; j < target.length; j++) { 101  if (source[i + j] != target[j]) { 102  found = false; 103  break; 104  } 105  } 106  107  if (found) { 108  return i; 109  } 110  } 111  112  return -1; 113  } 114 }