/* * This file is part of WebLookAndFeel library. * * WebLookAndFeel library 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 3 of the License, or * (at your option) any later version. * * WebLookAndFeel library 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 WebLookAndFeel library. If not, see . */ package com.alee.utils; import com.alee.utils.compare.Filter; import com.alee.utils.text.TextProvider; import java.util.*; /** * This class provides a set of utilities to work with collections. * * @author Mikle Garin */ public final class CollectionUtils { /** * Adds all objects into specified list. * * @param collection list to fill * @param objects objects * @param objects type * @return true if list changed as the result of this operation, false otherwise */ public static boolean addAll ( final Collection collection, final T... objects ) { boolean result = false; for ( final T object : objects ) { result |= collection.add ( object ); } return result; } /** * Returns copy of the specified list. * Note that this method will copy same list values into the new list. * * @param collection list to copy * @param list type * @return copy of the specified list */ public static List copy ( final Collection collection ) { if ( collection == null ) { return null; } return new ArrayList ( collection ); } /** * Returns clone of the specified list. * Note that this method will clone all values into new list. * * @param collection list to clone * @param list type * @return clone of the specified list */ public static List clone ( final Collection collection ) { if ( collection == null ) { return null; } final List cloned = new ArrayList ( collection.size () ); for ( final T value : collection ) { cloned.add ( ReflectUtils.cloneSafely ( value ) ); } return cloned; } /** * Returns data converted into list. * * @param data data * @param data type * @return data list */ public static List copy ( final T... data ) { final List list = new ArrayList ( data.length ); Collections.addAll ( list, data ); return list; } /** * Removes all null elements from list. * * @param list list to refactor * @param list type * @return refactored list */ public static List removeNulls ( final List list ) { if ( list == null ) { return null; } for ( int i = list.size () - 1; i >= 0; i-- ) { if ( list.get ( i ) == null ) { list.remove ( i ); } } return list; } /** * Returns whether lists are equal or not. * * @param list1 first list * @param list2 second list * @return true if lists are equal, false otherwise */ public static boolean areEqual ( final List list1, final List list2 ) { if ( list1 == null && list2 == null ) { return true; } else if ( ( list1 == null || list2 == null ) && list1 != list2 ) { return false; } else { if ( list1.size () != list2.size () ) { return false; } else { for ( final Object object : list1 ) { if ( !list2.contains ( object ) ) { return false; } } return true; } } } /** * Returns list of strings extracted from the specified elements list. * * @param list elements list * @param textProvider text provider * @param elements type * @return list of strings extracted from the specified elements list */ public static List toStringList ( final List list, final TextProvider textProvider ) { final List stringList = new ArrayList ( list.size () ); for ( final T element : list ) { stringList.add ( textProvider.provide ( element ) ); } return stringList; } /** * Returns an int array created using Integer list. * * @param list Integer list * @return int array */ public static int[] toArray ( final List list ) { final int[] array = new int[ list.size () ]; for ( int i = 0; i < list.size (); i++ ) { final Integer integer = list.get ( i ); array[ i ] = integer != null ? integer : 0; } return array; } /** * Returns a list of objects converted from array. * * @param array data array * @param data type * @return data list */ public static List toList ( final T[] array ) { final List list = new ArrayList ( array.length ); Collections.addAll ( list, array ); return list; } /** * Returns a list of objects converted from deque. * * @param deque data deque * @param data type * @return data list */ public static List toList ( final Deque deque ) { return new ArrayList ( deque ); } /** * Returns list of elements filtered from collection. * * @param collection collecton to filter * @param filter filter to process * @param elements type * @return list of elements filtered from collection */ public static List filter ( final Collection collection, final Filter filter ) { final List filtered = new ArrayList ( collection.size () ); for ( final T element : collection ) { if ( filter.accept ( element ) ) { filtered.add ( element ); } } return filtered; } /** * Returns map keys list. * * @param map map to process * @param key object type * @param value object type * @return map keys list */ public static List keysList ( final Map map ) { return new ArrayList ( map.keySet () ); } /** * Returns map values list. * * @param map map to process * @param key object type * @param value object type * @return map values list */ public static List valuesList ( final Map map ) { return new ArrayList ( map.values () ); } /** * Returns map values summary list with unique elements only. * * @param map map to process * @param key object type * @param value object type * @return map values summary list with unique elements only */ public static List valuesSummaryList ( final Map> map ) { final ArrayList summary = new ArrayList ( 0 ); for ( final Map.Entry> entry : map.entrySet () ) { final List list = entry.getValue (); summary.ensureCapacity ( summary.size () + list.size () ); for ( final V value : list ) { if ( !summary.contains ( value ) ) { summary.add ( value ); } } } return summary; } }