src/main/java/org/organicdesign/fp/collections/Cowry.java
public static <A> A[] spliceIntoArrayAt(A[] insertedItems, A[] origItems, int idx,
                                            Class<A> tClass) {
        // Make an array that big enough.  It's too bad that the JVM bothers to
        // initialize this with nulls.
        @SuppressWarnings("unchecked")
        A[] newItems = tClass == null ? (A[]) new Object[insertedItems.length + origItems.length] :
                       (A[]) Array.newInstance(tClass, insertedItems.length + origItems.length);

        // If we aren't inserting at the first item, array-copy the items before the insert
        // point.
        if (idx > 0) {
            //               src,  srcPos, dest,destPos,length
            System.arraycopy(origItems, 0, newItems, 0, idx);
        }

        // Insert the new items
        //               src,      srcPos,     dest, destPos, length
        System.arraycopy(insertedItems, 0, newItems, idx, insertedItems.length);

        // If we aren't inserting at the last item, array-copy the items after the insert
        // point.
        if (idx < origItems.length) {
            System.arraycopy(origItems, idx, newItems, idx + insertedItems.length,
                             origItems.length - idx);
        }
        return newItems;
    }
public static <A> A[] spliceIntoArrayAt(A[] insertedItems, A[] origItems, int idx,
                                            Class<A> tClass) {
        // Make an array that big enough.  It's too bad that the JVM bothers to
        // initialize this with nulls.
        @SuppressWarnings("unchecked")
        A[] newItems = tClass == null ? (A[]) new Object[insertedItems.length + origItems.length] :
                       (A[]) Array.newInstance(tClass, insertedItems.length + origItems.length);

        // If we aren't inserting at the first item, array-copy the items before the insert
        // point.
        if (idx > 0) {
            //               src,  srcPos, dest,destPos,length
            System.arraycopy(origItems, 0, newItems, 0, idx);
        }

        // Insert the new items
        //               src,      srcPos,     dest, destPos, length
        System.arraycopy(insertedItems, 0, newItems, idx, insertedItems.length);

        // If we aren't inserting at the last item, array-copy the items after the insert
        // point.
        if (idx < origItems.length) {
            System.arraycopy(origItems, idx, newItems, idx + insertedItems.length,
                             origItems.length - idx);
        }
        return newItems;
    }
public static < A > A [ ] spliceIntoArrayAt ( A [ ] insertedItems , A [ ] origItems , int idx , Class < A > tClass ) { // Make an array that big enough.  It's too bad that the JVM bothers to // initialize this with nulls. @ SuppressWarnings ( "unchecked" ) A [ ] newItems = tClass == null ? ( A [ ] ) new Object [ insertedItems . length + origItems . length ] : ( A [ ] ) Array . newInstance ( tClass , insertedItems . length + origItems . length ) ; // If we aren't inserting at the first item, array-copy the items before the insert // point. if ( idx > 0 ) { //               src,  srcPos, dest,destPos,length System . arraycopy ( origItems , 0 , newItems , 0 , idx ) ; } // Insert the new items //               src,      srcPos,     dest, destPos, length System . arraycopy ( insertedItems , 0 , newItems , idx , insertedItems . length ) ; // If we aren't inserting at the last item, array-copy the items after the insert // point. if ( idx < origItems . length ) { System . arraycopy ( origItems , idx , newItems , idx + insertedItems . length , origItems . length - idx ) ; } return newItems ; }
Called splice, but handles precat (idx = 0) and concat (idx = origItems.length).
@param insertedItems the items to insert
@param origItems the original items.
@param idx the index to insert new items at
@param tClass the class of the resulting new array
@return a new array with the new items inserted at the proper position of the old array.
Called splice but handles precat ( idx = 0 ) and concat ( idx = origItems . length ) .