All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.pressgang.ccms.utils.common.CollectionUtilities Maven / Gradle / Ivy

The newest version!
/*
  Copyright 2011-2014 Red Hat, Inc

  This file is part of PressGang CCMS.

  PressGang CCMS is free software: you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  PressGang CCMS 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 Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with PressGang CCMS.  If not, see .
*/

package org.jboss.pressgang.ccms.utils.common;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * A collection of static methods to create and manipulate collections
 *
 * @author Matthew Casperson
 */
public class CollectionUtilities {
    public static > List sortAndReturn(final List list) {
        Collections.sort(list);
        return list;
    }

    /**
     * @param array A collection of elements to be included in the returned ArrayList
     * @return An ArrayList that includes all the elements passed in via the array parameter
     */
    public static  ArrayList toArrayList(final T... array) {
        final ArrayList retValue = new ArrayList();
        for (final T item : array)
            retValue.add(item);
        return retValue;
    }

    /**
     * @param items The Collection of items to be converted to an ArrayList
     * @return An ArrayList containing the elements in the set
     */
    public static  ArrayList toArrayList(final Collection items) {
        final ArrayList retValue = new ArrayList();
        for (final T item : items)
            retValue.add(item);
        return retValue;
    }

    /**
     * @param array The elements to be included in the returned ArrayList
     * @return An ArrayList containing the String representation of the elements passed in via the array parameter
     */
    public static ArrayList toStringArrayList(final Object... array) {
        final ArrayList retValue = new ArrayList();
        for (final Object item : array)
            retValue.add(item.toString());
        return retValue;
    }

    /**
     * @param input       The original collection of Strings
     * @param originalRE  The regular expression to match
     * @param replacement The String to be replace any instance of originalRE with
     * @return A collection of strings where originalRE has been replaced with replacement
     */
    public static List replaceStrings(final List input, final String originalRE, final String replacement) {
        final List retValue = new ArrayList();
        for (final String element : input) {
            retValue.add(element.replaceAll(originalRE, replacement));
        }
        return retValue;
    }

    /**
     * Merge arrays
     *
     * @param array The source arrays
     * @return A collection that contains the contents of all the arrays passed to array
     */
    public static  ArrayList mergeLists(final T... array) {
        final ArrayList retValue = new ArrayList();
        Collections.addAll(retValue, array);
        return retValue;
    }

    public static  int addAll(final T[] source, final Collection destination) {
        int count = 0;
        for (final T sourceItem : source) {
            destination.add(sourceItem);
            ++count;
        }

        return count;
    }

    public static  int addAllThatDontExist(final Collection source, final Collection destination) {
        int count = 0;
        for (final T sourceItem : source) {
            if (!destination.contains(sourceItem)) {
                destination.add(sourceItem);
                ++count;
            }
        }

        return count;
    }

    public static  String toSeperatedString(final Collection list) {
        final StringBuffer stringBuffer = new StringBuffer();
        for (final T element : list) {
            if (stringBuffer.length() != 0) stringBuffer.append(",");
            stringBuffer.append(element.toString());
        }
        return stringBuffer.toString();
    }

    public static  String toSeperatedString(final Collection list, final String separator) {
        final StringBuffer stringBuffer = new StringBuffer();
        for (final T element : list) {
            if (stringBuffer.length() != 0) stringBuffer.append(separator);
            stringBuffer.append(element.toString());
        }
        return stringBuffer.toString();
    }

    /**
     * Merges two arrays together
     *
     * @param first  The first source array
     * @param second The second source array
     * @return An array that combines the two arrays
     */
    public static  T[] concat(final T[] first, final T[] second) {
        /* deal with null inputs */
        if (first == null && second == null) return null;
        if (first == null) return second;
        if (second == null) return first;

        final T[] result = Arrays.copyOf(first, first.length + second.length);
        System.arraycopy(second, 0, result, first.length, second.length);
        return result;
    }

    /**
     * Provides an easy way to compare two possibly null comparable objects
     *
     * @param first  The first object to compare
     * @param second The second object to compare
     * @return < 0 if the first object is less than the second object, 0 if they are equal, and > 0 otherwise
     */
    public static > Integer getSortOrder(final T first, final T second) {
        if (first == null && second == null) return null;

        if (first == null && second != null) return -1;

        if (first != null && second == null) return 1;

        return first.compareTo(second);
    }

    /**
     * Provides an easy way to see if two possibly null objects are equal
     *
     * @param first  The first object to test
     * @param second The second to test
     * @return true if both objects are equal, false otherwise
     */
    public static  boolean isEqual(final T first, final T second) {
        /* test to see if they are both null, or both reference the same object */
        if (first == second) return true;

        if (first == null && second != null) return false;

        return first.equals(second);
    }

    public static List toAbsIntegerList(final Collection array) {
        final ArrayList retValue = new ArrayList();
        for (final Integer item : array)
            retValue.add(Math.abs(item));
        return retValue;
    }

    /**
     * Trims an array of Strings to remove the whitespace. If the string is empty then its removed from the array.
     *
     * @param input The array of strings to be trimmed
     * @return The same array of strings but all elements have been trimmed of whitespace
     */
    public static String[] trimStringArray(final String[] input) {
        final ArrayList output = new ArrayList();
        for (int i = 0; i < input.length; i++) {
            String s = input[i].trim();
            if (!s.equals("")) output.add(s);
        }
        return output.toArray(new String[0]);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy