/*
 * JBoss, Home of Professional Open Source
 * Copyright 2008, Red Hat, Inc., and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * 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.jboss.weld.util.collections;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author pmuir
 */
public class Arrays2 {

    private Arrays2() {
    }

    public static final boolean containsAll(Object[] array, Object... values) {
        return Arrays.asList(array).containsAll(Arrays.asList(values));
    }

    public static final boolean unorderedEquals(Object[] array, Object... values) {
        return containsAll(array, values) && array.length == values.length;
    }

    public static <T> Set<T> asSet(T... array) {
        Set<T> result = new HashSet<T>();
        for (T a : array) {
            result.add(a);
        }
        return result;
    }

    // Cloning

    /**
     * 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 <tt>null</tt>.
     * 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.
     *
     * @param original  the array to be copied
     * @param newLength the length of the copy to be returned
     * @return a copy of the original array, truncated or padded with nulls
     *         to obtain the specified length
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
     * @throws NullPointerException       if <tt>original</tt> is null
     * @since 1.6
     */
    @SuppressWarnings("unchecked")
    public static <T> T[] copyOf(T[] original, int newLength) {
        return (T[]) copyOf(original, newLength, original.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 <tt>null</tt>.
     * 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 <tt>newType</tt>.
     *
     * @param original  the array to be copied
     * @param newLength the length of the copy to be returned
     * @param newType   the class of the copy to be returned
     * @return a copy of the original array, truncated or padded with nulls
     *         to obtain the specified length
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
     * @throws NullPointerException       if <tt>original</tt> is null
     * @throws ArrayStoreException        if an element copied from
     *                                    <tt>original</tt> is not of a runtime type that can be stored in
     *                                    an array of class <tt>newType</tt>
     * @since 1.6
     */
    public static <T, U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
        @SuppressWarnings("unchecked")
        T[] copy = ((Object) newType == (Object) Object[].class)
                ? (T[]) new Object[newLength]
                : (T[]) Array.newInstance(newType.getComponentType(), newLength);
        System.arraycopy(original, 0, copy, 0,
                Math.min(original.length, newLength));
        return copy;
    }

}

@@ -1411,19 +1411,31 @@
 hSet%3CT%3E(
+array.length
 );%0A
-
         
@@ -1536,17 +1536,16 @@
 Cloning%0A
-%0A
     /**%0A
@@ -1540,33 +1540,32 @@
 ing%0A    /**%0A    
- 
 * Copies the spe
@@ -1618,33 +1618,32 @@
  (if necessary)%0A
- 
     * so the cop
@@ -1692,33 +1692,32 @@
 es that are%0A    
- 
 * valid in both 
@@ -1761,33 +1761,32 @@
 two arrays will%0A
- 
     * contain id
@@ -1836,33 +1836,32 @@
 alid in the%0A    
- 
 * copy but not t
@@ -1902,33 +1902,32 @@
  %3Ctt%3Enull%3C/tt%3E.%0A
- 
     * Such indic
@@ -1972,33 +1972,32 @@
 fied length%0A    
- 
 * is greater tha
@@ -2018,33 +2018,32 @@
 original array.%0A
- 
     * The result
@@ -2104,28 +2104,26 @@
  array.%0A    
- 
 *%0A
- 
     * @param
@@ -2124,33 +2124,32 @@
 @param original 
- 
 the array to be 
@@ -2147,33 +2147,32 @@
 ay to be copied%0A
- 
     * @param new
@@ -2212,33 +2212,32 @@
 be returned%0A    
- 
 * @return a copy
@@ -2291,35 +2291,30 @@
 h nulls%0A    
- *  
+*
      
-  
 to obtain th
@@ -2324,33 +2324,32 @@
 pecified length%0A
- 
     * @throws Ne
@@ -2403,33 +2403,32 @@
 is negative%0A    
- 
 * @throws NullPo
@@ -2434,38 +2434,32 @@
 ointerException 
-      
 if %3Ctt%3Eoriginal%3C
@@ -2471,25 +2471,24 @@
 is null%0A    
- 
 * @since 1.6
@@ -2480,33 +2480,32 @@
    * @since 1.6%0A
- 
     */%0A    @Supp
@@ -2498,17 +2498,16 @@
   */%0A   
- 
 @Suppres
@@ -2529,17 +2529,16 @@
 ecked%22)%0A
- 
    publi
@@ -2595,25 +2595,24 @@
 h) %7B%0A       
- 
 return (T%5B%5D)
@@ -2658,25 +2658,24 @@
 etClass());%0A
- 
    %7D%0A%0A    /*

improve mem usage Conflicts: