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

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

There is a newer version: 4.2
Show newest version
// Metawidget
//
// This library 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 2.1 of the License, or (at your option) any later version.
//
// This 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

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 final  ArrayList newArrayList() {

		return new ArrayList();
	}

	/**
	 * Type-safe initializer.
	 */

	public static final  ArrayList newArrayList( Collection collection ) {

		return new ArrayList( collection );
	}

	/**
	 * Type-safe initializer.
	 */

	public static final  ArrayList newArrayList( int capacity ) {

		return new ArrayList( capacity );
	}

	/**
	 * Type-safe initializer.
	 */

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

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

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

	/**
	 * Type-safe initializer.
	 */

	public static final  HashSet newHashSet() {

		return new HashSet();
	}

	/**
	 * Type-safe initializer.
	 */

	public static final  HashSet newHashSet( Collection set ) {

		return new HashSet( set );
	}

	/**
	 * Type-safe initializer.
	 */

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

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

	/**
	 * Type-safe initializer.
	 */

	public static final  Stack newStack() {

		return new Stack();
	}

	/**
	 * Type-safe initializer.
	 */

	public static final  HashMap newHashMap() {

		return new HashMap();
	}

	/**
	 * Type-safe initializer.
	 */

	public static final  HashMap newHashMap( Map map ) {

		return new HashMap( map );
	}

	/**
	 * Type-safe initializer.
	 */

	public static final  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 final  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 final WeakHashMap newWeakHashMap() { return new WeakHashMap(); } /** * Type-safe initializer. */ public static final LinkedHashMap newLinkedHashMap() { return new LinkedHashMap(); } /** * Type-safe initializer. */ public static final LinkedHashMap newLinkedHashMap( Map map ) { return new LinkedHashMap( map ); } /** * Type-safe initializer. */ public static final TreeMap newTreeMap() { return new TreeMap(); } public static final 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