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

org.metawidget.util.CollectionUtils Maven / Gradle / Ivy

The newest version!
// Metawidget
//
// This file is dual licensed under both the LGPL
// (http://www.gnu.org/licenses/lgpl-2.1.html) and the EPL
// (http://www.eclipse.org/org/documents/epl-v10.php). As a
// recipient of Metawidget, you may choose to receive it under either
// the LGPL or the EPL.
//
// Commercial licenses are also available. See http://metawidget.org
// for details.

package org.metawidget.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import java.util.TreeMap;
import java.util.WeakHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.metawidget.util.simple.StringUtils;
import org.metawidget.widgetbuilder.iface.WidgetBuilderException;

/**
 * Utilities for working with Java Collections.
 *
 * @author Richard Kennard
 */

public final class CollectionUtils {

	//
	// Public statics
	//

	/**
	 * Type-safe initializer.
	 */

	public static  ArrayList newArrayList() {

		return new ArrayList();
	}

	/**
	 * Type-safe initializer.
	 */

	public static  ArrayList newArrayList( Collection collection ) {

		return new ArrayList( collection );
	}

	/**
	 * Type-safe initializer.
	 */

	public static  ArrayList newArrayList( int capacity ) {

		return new ArrayList( capacity );
	}

	/**
	 * Type-safe initializer.
	 */

	public static  ArrayList newArrayList( T... array ) {

		if ( array == null ) {
			return new ArrayList();
		}

		return new ArrayList( Arrays.asList( array ) );
	}

	/**
	 * Type-safe initializer.
	 */

	public static  HashSet newHashSet() {

		return new HashSet();
	}

	/**
	 * Type-safe initializer.
	 */

	public static  HashSet newHashSet( Collection set ) {

		return new HashSet( set );
	}

	/**
	 * Type-safe initializer.
	 */

	public static  HashSet newHashSet( K... array ) {

		return new HashSet( Arrays.asList( array ) );
	}

	/**
	 * Type-safe initializer.
	 */

	public static  Stack newStack() {

		return new Stack();
	}

	/**
	 * Type-safe initializer.
	 */

	public static  HashMap newHashMap() {

		return new HashMap();
	}

	/**
	 * Type-safe initializer.
	 */

	public static  HashMap newHashMap( Map map ) {

		return new HashMap( map );
	}

	/**
	 * Type-safe initializer.
	 */

	public static  HashMap newHashMap( int size ) {

		return new HashMap( size );
	}

	/**
	 * Returns a Map initialized using the first given List as keys, and the second given List as
	 * values.
	 */

	public static  Map newHashMap( List keys, List values ) {

		if ( keys.size() != values.size() ) {
			throw WidgetBuilderException.newException( "Keys list must be same size as values list" );
		}

		Map map = CollectionUtils.newHashMap();

		for ( int loop = 0, length = keys.size(); loop < length; loop++ ) {
			map.put( keys.get( loop ), values.get( loop ) );
		}

		return map;
	}

	/**
	 * Type-safe initializer.
	 * 

* WeakHashMap is preferrable if K is of type Class. */ public static WeakHashMap newWeakHashMap() { return new WeakHashMap(); } /** * Type-safe initializer. */ public static LinkedHashMap newLinkedHashMap() { return new LinkedHashMap(); } /** * Type-safe initializer. */ public static LinkedHashMap newLinkedHashMap( Map map ) { return new LinkedHashMap( map ); } /** * Type-safe initializer. */ public static TreeMap newTreeMap() { return new TreeMap(); } public static TreeMap newTreeMap( Comparator comparator ) { return new TreeMap( comparator ); } public static List unmodifiableList( T... array ) { return Collections.unmodifiableList( Arrays.asList( array ) ); } public static String toString( Collection collection ) { return toString( collection, StringUtils.SEPARATOR_COMMA ); } public static String toString( Collection collection, String separator ) { return toString( collection, separator, false, false ); } public static String toString( Collection collection, String separator, boolean leadingSeparator, boolean trailingSeparator ) { // Nothing to do? if ( collection == null ) { return ""; } // If Collection is a Set, sort it for consistency in unit tests. Never // sort the original Collection, as users wouldn't expect toString() to do that! Collection consistentlyOrderedCollection = collection; if ( consistentlyOrderedCollection instanceof Set ) { consistentlyOrderedCollection = newArrayList( collection ); Collections.sort( (List) consistentlyOrderedCollection, null ); } // Output as a String Pattern patternSeparator = Pattern.compile( separator, Pattern.LITERAL ); String replacement = "\\\\" + separator; StringBuilder builder = new StringBuilder(); for ( T t : consistentlyOrderedCollection ) { String value = String.valueOf( t ); // Concatenate the separator if ( builder.length() > 0 || leadingSeparator ) { builder.append( separator ); } // Escape the separator value = patternSeparator.matcher( value ).replaceAll( replacement ); // Build the string builder.append( value ); } if ( trailingSeparator && builder.length() > 0 ) { builder.append( separator ); } return builder.toString(); } /** * Split the given String by comma. Commas within Strings may be escaped using the backslash (\) * character. Starting and ending whitespace around each String will be trimmed. */ public static List fromString( final String collection ) { return fromString( collection, ',' ); } /** * Split the given String by the given separator. Separators within Strings may be escaped using * the backslash (\) character. Starting and ending whitespace around each sub-String will be * trimmed. */ public static List fromString( final String collection, char separator ) { if ( collection == null || collection.length() == 0 ) { return Collections.emptyList(); } List split = CollectionUtils.newArrayList(); Pattern patternSplit = Pattern.compile( "((\\\\" + separator + "|[^" + separator + "])*)(" + separator + "|$)" ); Pattern patternSeparator = Pattern.compile( "\\" + separator, Pattern.LITERAL ); String replacement = String.valueOf( separator ); Matcher matcher = patternSplit.matcher( collection ); while ( matcher.find() ) { String match = matcher.group( 1 ).trim(); match = patternSeparator.matcher( match ).replaceAll( replacement ); split.add( match ); if ( matcher.end( 1 ) == collection.length() ) { break; } } return split; } // // Private constructor // private CollectionUtils() { // Can never be called } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy