Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 /*
  * Copyright (C) 2014 the original author or authors.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.codehaus.gmavenplus.util;
Utility methods ported from later JDK versions for use with JDK 5.

Author(s):
Keegan Witt
Since:
1.2
public class JDK5Utils {

    
Private constructor that should never be called since this is a static utility class.
    private JDK5Utils() { }

    
Copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length. For all indices that are valid in both the original array and the copy, the two arrays will contain identical values. For any indices that are valid in the copy but not the original, the copy will contain null. Such indices will exist if and only if the specified length is greater than that of the original array. The resulting array is of exactly the same class as the original array.

Parameters:
original the array to be copied
newLength the length of the copy to be returned
Returns:
a copy of the original array, truncated or padded with nulls to obtain the specified length
Throws:
java.lang.NegativeArraySizeException if newLength is negative
java.lang.NullPointerException if original is null
    @SuppressWarnings("unchecked")
    public static <T> T[] Arrays_copyOf(final T[] originalfinal int newLength) {
        return (T[]) Arrays_copyOf(originalnewLengthoriginal.getClass());
    }

    
Copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length. For all indices that are valid in both the original array and the copy, the two arrays will contain identical values. For any indices that are valid in the copy but not the original, the copy will contain null. Such indices will exist if and only if the specified length is greater than that of the original array. The resulting array is of the class newType.

Parameters:
original the array to be copied
newLength the length of the copy to be returned
newType the class of the copy to be returned
Returns:
a copy of the original array, truncated or padded with nulls to obtain the specified length
Throws:
java.lang.NegativeArraySizeException if newLength is negative
java.lang.NullPointerException if original is null
java.lang.ArrayStoreException if an element copied from original is not of a runtime type that can be stored in an array of class newType
    @SuppressWarnings("unchecked")
    public static <T, U> T[] Arrays_copyOf(final U[] originalfinal int newLengthfinal Class<? extends T[]> newType) {
        T[] copy = ((ObjectnewType == (ObjectObject[].class) ? (T[]) new Object[newLength] : (T[]) Array.newInstance(newType.getComponentType(), newLength);
        System.arraycopy(original, 0, copy, 0, Math.min(original.lengthnewLength));
        return copy;
    }
New to GrepCode? Check out our FAQ X