Class Cowry


  • public final class Cowry
    extends Object
    Cowry is short for Copy On Write aRraY and contains utilities for doing this quickly and correctly. While a key goal of Paguro is to get away from working with arrays, you still need to do it sometimes and shouldn't have to re-implement common copy-on-write modifictions. Never, ever, ever do this:
    
    // Evil, bad, and wrong!
    (Class<T>) items[0].getClass()
    
    If you are using an array of Numbers and the first item is an Integer and the second a Double, You'll get an array of Integers which will blow up when you try to add a Double. This class is final and cannot be instantiated. Created by gpeterso on 5/21/2017.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> T[] arrayCopy​(T[] items, int length, @Nullable Class<T> tClass)
      Returns a new array containing the first n items of the given array.
      static <T> T[] emptyArray()  
      static <T> T[] insertIntoArrayAt​(T item, T[] items, int idx, @Nullable Class<T> tClass)
      Returns a new array one longer than the given one, with the specified item inserted at the specified index.
      static <T> T[] replaceInArrayAt​(T replacedItem, T[] origItems, int idx, @Nullable Class<T> tClass)  
      static <T> T[] singleElementArray​(T elem, @Nullable Class<T> tClass)
      Helper function to avoid type warnings.
      static <A> A[] spliceIntoArrayAt​(A[] insertedItems, A[] origItems, int idx, @Nullable Class<A> tClass)
      Called splice, but handles precat (idx = 0) and concat (idx = origItems.length).
      static int[][] splitArray​(int[] orig, int splitIndex)
      Only call this if the array actually needs to be split (0 < splitPoint < orig.length).
      static <T> Tuple2<T[],​T[]> splitArray​(T[] orig, int splitIndex)
      Only call this if the array actually needs to be split (0 < splitPoint < orig.length).
    • Method Detail

      • emptyArray

        public static <T> T[] emptyArray()
      • singleElementArray

        public static <T> T[] singleElementArray​(T elem,
                                                 @Nullable
                                                 @Nullable Class<T> tClass)
        Helper function to avoid type warnings.
        Parameters:
        elem - the item to put in the array.
        tClass - the class of the array.
        Returns:
        an array of the appropriate class containing the single element.
      • insertIntoArrayAt

        public static <T> T[] insertIntoArrayAt​(T item,
                                                T[] items,
                                                int idx,
                                                @Nullable
                                                @Nullable Class<T> tClass)
        Returns a new array one longer than the given one, with the specified item inserted at the specified index.
        Parameters:
        item - The item to insert
        items - The original array (will not be modified)
        idx - the index to insert the item at.
        tClass - The runtime class to store in the new array (or null for an Object array).
        Returns:
        A copy of the given array with the additional item at the appropriate index (will be one longer than the original array).
      • arrayCopy

        public static <T> T[] arrayCopy​(T[] items,
                                        int length,
                                        @Nullable
                                        @Nullable Class<T> tClass)
        Returns a new array containing the first n items of the given array.
        Parameters:
        items - the items to copy
        length - the maximum length of the new array
        tClass - the class of the items in the array (null for Object)
        Returns:
        a copy of the original array with the given length
      • spliceIntoArrayAt

        public static <A> A[] spliceIntoArrayAt​(A[] insertedItems,
                                                A[] origItems,
                                                int idx,
                                                @Nullable
                                                @Nullable Class<A> tClass)
        Called splice, but handles precat (idx = 0) and concat (idx = origItems.length).
        Parameters:
        insertedItems - the items to insert
        origItems - the original items.
        idx - the index to insert new items at
        tClass - the class of the resulting new array
        Returns:
        a new array with the new items inserted at the proper position of the old array.
      • replaceInArrayAt

        public static <T> T[] replaceInArrayAt​(T replacedItem,
                                               T[] origItems,
                                               int idx,
                                               @Nullable
                                               @Nullable Class<T> tClass)
      • splitArray

        public static <T> Tuple2<T[],​T[]> splitArray​(T[] orig,
                                                           int splitIndex)
        Only call this if the array actually needs to be split (0 < splitPoint < orig.length).
        Parameters:
        orig - array to split
        splitIndex - items less than this index go in the left, equal or greater in the right.
        Returns:
        a 2D array of leftItems then rightItems
      • splitArray

        public static int[][] splitArray​(int[] orig,
                                         int splitIndex)
        Only call this if the array actually needs to be split (0 < splitPoint < orig.length).
        Parameters:
        orig - array to split
        splitIndex - items less than this index go in the left, equal or greater in the right.
        Returns:
        a 2D array of leftItems then rightItems