|
| 1 | +/************************************************************************************************* |
| 2 | +* This file is part of the Nebula Framework project, released under the MIT License. * |
| 3 | +* See LICENSE file or go to https://github.com/jongpie/NebulaFramework for full license details. * |
| 4 | +*************************************************************************************************/ |
| 5 | + |
| 6 | +/** |
| 7 | +* |
| 8 | +* @group Utils |
| 9 | +* |
| 10 | +* @description A utility class to help with dealing with collections (lists, sets & maps) |
| 11 | +* |
| 12 | +*/ |
| 13 | +public without sharing class CollectionUtils { |
| 14 | + |
| 15 | + /** |
| 16 | + * @description Returns the last item in a list |
| 17 | + * @param listOfItems the list to check |
| 18 | + * @return The last Object in the provided list |
| 19 | + * @example |
| 20 | + * List<String> myList = new List<String>{'A', B', 'C'}; |
| 21 | + * String lastItem = CollectionUtils.getLastItem(myList); |
| 22 | + * System.assertEquals('C', lastItem); |
| 23 | + */ |
| 24 | + public static Object getLastItem(List<Object> listOfItems) { |
| 25 | + Integer indexOfItem = listOfItems.size() - 1; |
| 26 | + return listOfItems[indexOfItem]; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @description Removes the last item in the provided list & returns the item |
| 31 | + * @param listOfItems the list to check |
| 32 | + * @return The last Object in the provided list |
| 33 | + * @example |
| 34 | + * List<String> myList = new List<String>{'A', B', 'C'}; |
| 35 | + * System.assertEquals(3, myList.size()); |
| 36 | + * String lastItem = CollectionUtils.getLastItem(myList); |
| 37 | + * System.assertEquals('C', lastItem); |
| 38 | + * System.assertEquals(2, myList.size()); |
| 39 | + */ |
| 40 | + public static Object pop(List<Object> listToSplice) { |
| 41 | + return splice(listToSplice, listToSplice.size() - 1); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @description Removes the item in the specified index from the provided list & returns the item |
| 46 | + * @param listOfItems The list to check |
| 47 | + * @param indexOfItem The index of the item to remove |
| 48 | + * @return The Object at the specified index |
| 49 | + * @example |
| 50 | + * List<String> myList = new List<String>{'A', B', 'C'}; |
| 51 | + * System.assertEquals(3, myList.size()); |
| 52 | + * String itemToRemove = CollectionUtils.splice(myList, 1); |
| 53 | + * System.assertEquals('B', itemToRemove); |
| 54 | + * System.assertEquals(2, myList.size()); |
| 55 | + */ |
| 56 | + public static Object splice(List<Object> listToSplice, Integer indexOfItem) { |
| 57 | + Object itemToRemove = listToSplice[indexOfItem]; |
| 58 | + listToSplice.remove(indexOfItem); |
| 59 | + return itemToRemove; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @description Determines if the provided input is a type of collection (list, set or map) |
| 64 | + * @param input The Object to check |
| 65 | + * @return true if the item is a type of collection, otherwise returns false |
| 66 | + * @example |
| 67 | + * List<String> myList = new List<String>{'A', 'B', 'C'}; |
| 68 | + * System.assert(CollectionUtils.isCollection(myList)); |
| 69 | + */ |
| 70 | + public static Boolean isCollection(Object input) { |
| 71 | + return isList(input) || isSet(input) || isMap(input); |
| 72 | + } |
| 73 | + |
| 74 | + public static Boolean isList(Object input) { |
| 75 | + // If we can cast the object to a list of objects, then it's a list |
| 76 | + try { |
| 77 | + Object convertedInput = (List<Object>)input; |
| 78 | + return true; |
| 79 | + } catch(System.TypeException ex) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public static Boolean isSet(Object input) { |
| 85 | + // We can't cast the object to a set of objects |
| 86 | + // But if we try to cast it to a list of objects & it's a set, |
| 87 | + // then a TypeException is thrown so we know it's a set |
| 88 | + try { |
| 89 | + Object convertedInput = (List<Object>)input; |
| 90 | + return false; |
| 91 | + } catch(System.TypeException ex) { |
| 92 | + return ex.getMessage().contains('Set<'); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + public static Boolean isMap(Object input) { |
| 97 | + // We can't cast the object to a map of objects |
| 98 | + // But if we try to cast it to a list of objects & it's a map, |
| 99 | + // then a TypeException is thrown so we know it's a map |
| 100 | + try { |
| 101 | + Object convertedInput = (List<Object>)input; |
| 102 | + return false; |
| 103 | + } catch(System.TypeException ex) { |
| 104 | + return ex.getMessage().contains('Map<'); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments