Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2017, Numdata BV, The Netherlands.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Numdata nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NUMDATA BV BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.numdata.oss;
import java.lang.reflect.*;
import java.math.*;
import java.util.*;
import com.numdata.oss.ensemble.*;
import org.jetbrains.annotations.*;
/**
* This class contains utility methods for working with collections.
*
* @author Peter S. Heijnen
*/
public class CollectionTools
{
/**
* Utility class is not supposed to be instantiated.
*/
private CollectionTools()
{
}
/**
* Add element to grouped collections.
*
* @param result Resulting groups mapped by key.
* @param groupKey Element group.
* @param element Element to add.
* @param groupClass Type of newly created groups.
* @param Group key type.
* @param Element type.
* @param Group type.
*
* @return Group to which the element was added.
*/
@NotNull
public static > G addToGroupedCollection( @NotNull final Map result, final K groupKey, final E element, @SuppressWarnings( "rawtypes" ) @NotNull final Class extends Collection> groupClass )
{
G group = result.get( groupKey );
if ( group == null )
{
try
{
group = (G)groupClass.getConstructor().newInstance();
}
catch ( final InstantiationException e )
{
throw new IllegalArgumentException( "Bad group class: " + groupClass, e );
}
catch ( final IllegalAccessException e )
{
throw new IllegalArgumentException( "Bad group class: " + groupClass, e );
}
catch ( final NoSuchMethodException e )
{
throw new IllegalArgumentException( "Bad group class: " + groupClass, e );
}
catch ( final InvocationTargetException e )
{
throw new IllegalArgumentException( "Bad group class: " + groupClass, e );
}
result.put( groupKey, group );
}
group.add( element );
return group;
}
/**
* Add element to grouped list.
*
* @param result Resulting list mapped by key.
* @param groupKey Element group.
* @param element Element to add.
* @param Group key type.
* @param Element type.
*
* @return List to which the element was added.
*/
@NotNull
public static List addToGroupedList( @NotNull final Map> result, final K groupKey, final E element )
{
return addToGroupedCollection( result, groupKey, element, ArrayList.class );
}
/**
* Add element to grouped set.
*
* @param result Resulting set mapped by key.
* @param groupKey Element group.
* @param element Element to add.
* @param Group key type.
* @param Element type.
*
* @return Set to which the element was added.
*/
@NotNull
public static Set addToGroupedSet( @NotNull final Map> result, final K groupKey, final E element )
{
return addToGroupedCollection( result, groupKey, element, HashSet.class );
}
/**
* Adds the given value to the map's current value of the given key. If the
* map contains no entry for the given key, the implicit value of the key is
* zero.
*
* @param map Map to be modified.
* @param key Key to change the value of.
* @param value Amount to be added to the current value of the key.
*
* @return The value that is stored in the map.
*/
public static int addToValue( final Map map, final K key, final int value )
{
final Integer oldValue = map.get( key );
final int result = ( oldValue != null ) ? value + oldValue : value;
map.put( key, result );
return result;
}
/**
* Adds the given value to the map's current value of the given key. If the
* map contains no entry for the given key, the implicit value of the key is
* zero.
*
* @param map Map to be modified.
* @param key Key to change the value of.
* @param value Amount to be added to the current value of the key.
*
* @return The value that is stored in the map.
*/
public static float addToValue( final Map map, final K key, final float value )
{
final Float oldValue = map.get( key );
final float result = ( oldValue != null ) ? value + oldValue : value;
map.put( key, result );
return result;
}
/**
* Adds the given value to the map's current value of the given key. If the
* map contains no entry for the given key, the implicit value of the key is
* zero.
*
* @param map Map to be modified.
* @param key Key to change the value of.
* @param value Amount to be added to the current value of the key.
*
* @return The value that is stored in the map.
*/
public static BigDecimal addToValue( final Map map, final K key, final BigDecimal value )
{
final BigDecimal oldValue = map.get( key );
final BigDecimal result = ( oldValue != null ) ? oldValue.add( value ) : value;
map.put( key, result );
return result;
}
/**
* Adds the given value to the map's current value of the given key. If the
* map contains no entry for the given key, the implicit value of the key is
* zero.
*
* @param map Map to be modified.
* @param key Key to change the value of.
* @param value Amount to be added to the current value of the key.
*
* @return The value that is stored in the map.
*/
public static double addToValue( final Map map, final K key, final double value )
{
final Double oldValue = map.get( key );
final double result = ( oldValue != null ) ? value + oldValue : value;
map.put( key, result );
return result;
}
/**
* Get values of the specified field from a collection.
*
* @param collection Collection of objects to get values from.
* @param fieldName Name of field whose values to get.
* @param fieldType Field type.
*
* @return List of values from collection.
*
* @throws IllegalArgumentException if an argument or element is {@code
* null}.
*/
public static List getFieldValues( @NotNull final Collection> collection, @NotNull final String fieldName, @NotNull final Class fieldType )
{
return getFieldValues( new ArrayList( collection.size() ), collection, fieldName, fieldType );
}
/**
* Get values of the specified field from a collection into another
* collection.
*
* @param result Collection to store result in.
* @param collection Collection of objects to get values from.
* @param fieldName Name of field whose values to get.
* @param fieldType Field type.
*
* @return {@code result} argument value.
*
* @throws IllegalArgumentException if an argument or element is {@code
* null}.
*/
public static > C getFieldValues( @NotNull final C result, @NotNull final Iterable> collection, @NotNull final String fieldName, @NotNull final Class fieldType )
{
try
{
Class> lastClass = null;
Field field = null;
for ( final Object element : collection )
{
if ( element == null )
{
throw new IllegalArgumentException( "null element" );
}
final Class> elementClass = element.getClass();
//noinspection ObjectEquality
if ( elementClass != lastClass )
{
field = elementClass.getField( fieldName );
lastClass = elementClass;
}
if ( field != null )
{
result.add( fieldType.cast( field.get( element ) ) );
}
}
return result;
}
catch ( final NoSuchFieldException e )
{
throw new IllegalArgumentException( e.getMessage(), e );
}
catch ( final IllegalAccessException e )
{
throw new IllegalArgumentException( e.getMessage(), e );
}
}
/**
* Get values of the specified bean property from a collection.
*
* @param collection Collection of objects to get values from.
* @param propertyName Name of property whose values to get.
* @param propertyType Property type.
*
* @return New collection with property values.
*
* @throws IllegalArgumentException if an argument or element is {@code
* null}.
*/
public static List getPropertyValues( @NotNull final Collection> collection, @NotNull final String propertyName, @NotNull final Class propertyType )
{
return getPropertyValues( new ArrayList( collection.size() ), collection, propertyName, propertyType );
}
/**
* Get values of the specified bean property from a collection into another
* collection.
*
* @param result Collection to store result in.
* @param collection Collection of objects to get values from.
* @param propertyName Name of property whose values to get.
* @param propertyType Property type.
*
* @return {@code result} argument value.
*
* @throws IllegalArgumentException if an argument or element is {@code
* null}.
*/
public static > C getPropertyValues( @NotNull final C result, @NotNull final Iterable> collection, @NotNull final String propertyName, @NotNull final Class propertyType )
{
//noinspection ObjectEquality
final String methodName = ( ( propertyType == Boolean.class ) ? "is" : "get" ) + Character.toUpperCase( propertyName.charAt( 0 ) ) + propertyName.substring( 1 );
try
{
Class> lastClass = null;
Method getter = null;
for ( final Object element : collection )
{
if ( element == null )
{
throw new IllegalArgumentException( "null element" );
}
final Class> elementClass = element.getClass();
//noinspection ObjectEquality
if ( elementClass != lastClass )
{
lastClass = elementClass;
getter = elementClass.getMethod( methodName );
}
if ( getter != null )
{
result.add( propertyType.cast( getter.invoke( element ) ) );
}
}
return result;
}
catch ( final InvocationTargetException e )
{
throw new IllegalArgumentException( e.getMessage(), e );
}
catch ( final IllegalAccessException e )
{
throw new IllegalArgumentException( e.getMessage(), e );
}
catch ( final NoSuchMethodException e )
{
throw new IllegalArgumentException( e.getMessage(), e );
}
}
/**
* Get integer values of the specified field from a collection.
*
* @param collection Collection of objects to get values from.
* @param fieldName Name of field whose values to get.
*
* @return Arrays with integer values from collection.
*
* @throws IllegalArgumentException if an argument or element is {@code
* null}.
*/
public static int[] getIntFieldValues( @NotNull final Collection> collection, @NotNull final String fieldName )
{
try
{
final int[] result = new int[ collection.size() ];
Class> lastClass = null;
Field field = null;
int resultIndex = 0;
for ( final Object element : collection )
{
if ( element == null )
{
throw new IllegalArgumentException( "element" );
}
final Class> elementClass = element.getClass();
//noinspection ObjectEquality
if ( ( field == null ) || ( elementClass != lastClass ) )
{
field = elementClass.getField( fieldName );
lastClass = elementClass;
}
result[ resultIndex++ ] = ( (Number)field.get( element ) ).intValue();
}
return result;
}
catch ( final NoSuchFieldException e )
{
throw new IllegalArgumentException( e.getMessage(), e );
}
catch ( final IllegalAccessException e )
{
throw new IllegalArgumentException( e.getMessage(), e );
}
}
/**
* Get {@link Collection} class for the specified element types.
*
* @param Element type.
*
* @return {@link Class} for {@link Collection}
*/
public static Class> getCollectionClass()
{
final Class> collectionClass = Collection.class;
return (Class>)collectionClass;
}
/**
* Get first element from {@link Iterable}.
*
* @param iterable Iterable to get first element from.
*
* @return First element by iterator of {@code iterable}.
*
* @throws NoSuchElementException if the iterable has no elements.
*/
public static T getFirst( @NotNull final Iterable extends T> iterable )
{
final Iterator extends T> itereator = iterable.iterator();
return itereator.next();
}
/**
* Get {@link List} class for the specified element types.
*
* @param Element type.
*
* @return {@link Class} for {@link List}
*/
public static Class> getListClass()
{
final Class> listClass = List.class;
return (Class>)listClass;
}
/**
* Get {@link Map} class for the specified key and value types.
*
* @param Key type.
* @param Value type.
*
* @return {@link Class} for {@link Map}
*/
public static Class