/*
 * Copyright 2006-2009 The MZmine 2 Development Team
 * 
 * This file is part of MZmine 2.
 * 
 * MZmine 2 is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * MZmine 2 is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * MZmine 2; if not, write to the Free Software Foundation, Inc., 51 Franklin
 * St, Fifth Floor, Boston, MA 02110-1301 USA
 */

package net.sf.mzmine.util;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Iterator;

/**
 * Collection API related utilities
 */
public class CollectionUtils {

    /**
     * Returns an array of ints consisting of the elements of the specified
     * collection.
     * 
     * @param collection Collection of Integers
     * @return Array of ints
     */
    public static int[] toIntArray(Collection<Integer> collection) {
        int array[] = new int[collection.size()];
        int index = 0;
        Iterator<Integer> it = collection.iterator();
        while (it.hasNext()) {
            array[index++] = it.next();
        }
        return array;
    }

    /**
     * Change the type of array of Objects to an array of objects of type
     * newClass. The code of the method is a bit tricky, but the purpose is to
     * avoid warnings about unchecked casts.
     * 
     */
    public static <T> T[] changeArrayType(Object[] array, Class<T> newClass) {

        Object newArrayObject = Array.newInstance(newClass, array.length);

        Class<T[]> newArrayClass = T[].class;

        T newArray[] = newArrayClass.cast(newArrayObject);

        for (int i = 0; i < array.length; i++) {
            newArray[i] = newClass.cast(array[i]);
        }

        return newArray;
    }

    /**
     * Returns an array of doubles consisting of the elements of the specified
     * collection.
     * 
     * @param collection Collection of Doubles
     * @return Array of doubles
     */
    public static float[] toFloatArray(Collection<Float> collection) {
        float array[] = new float[collection.size()];
        int index = 0;
        Iterator<Float> it = collection.iterator();
        while (it.hasNext()) {
            array[index++] = it.next();
        }
        return array;
    }

    /**
     * Returns an array of doubles consisting of the elements of the specified
     * collection.
     * 
     * @param collection Collection of Doubles
     * @return Array of doubles
     */
    public static double[] toDoubleArray(Collection<Double> collection) {
        double array[] = new double[collection.size()];
        int index = 0;
        Iterator<Double> it = collection.iterator();
        while (it.hasNext()) {
            array[index++] = it.next();
        }
        return array;
    }

    /**
     * Checks if the haystack array contains all elements of needles array
     * 
     * @param haystack array of ints
     * @param needles array of ints
     * @return true if haystack contains all elements of needles
     */
    public static boolean isSubset(int haystack[], int needles[]) {
        needleTraversal: for (int i = 0; i < needles.length; i++) {
            for (int j = 0; j < haystack.length; j++) {
                if (needles[i] == haystack[j])
                    continue needleTraversal;
            }
            return false;
        }
        return true;
    }

    /**
     * Checks if the haystack array contains a specified element
     * 
     * @param haystack array of objects
     * @param needle object
     * @return true if haystack contains needle
     */
    public static <T> boolean arrayContains(T haystack[], T needle) {
        for (T test : haystack) {
            if (needle.equals(test))
                return true;
        }
        return false;
    }

}

@@ -1592,131 +1592,59 @@
 ass.
- The code of the method is a bit tricky, but the purpose is to%0A     * avoid warnings about unchecked casts.%0A     * %0A     */
+%0A     * %0A     */%0A    @SuppressWarnings(%22unchecked%22)
 %0A   
@@ -1728,22 +1728,17 @@
         
-Object
+T
  newArra
@@ -1742,16 +1742,18 @@
 rray
-Object =
+%5B%5D = (T%5B%5D)
  Arr
@@ -1797,115 +1797,8 @@
 );%0A%0A
-        Class%3CT%5B%5D%3E newArrayClass = T%5B%5D.class;%0A%0A        T newArray%5B%5D = newArrayClass.cast(newArrayObject);%0A%0A
     

Fix Java 6 compatibility