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

org.jace.util.DelimitedCollection Maven / Gradle / Ivy

There is a newer version: 1.2.22
Show newest version
package org.jace.util;

import java.util.Collection;
import java.util.Iterator;

/**
 * A utility class that returns the String representation of a collection.
 *
 * @param  the list type
 * @author Toby Reyelts
 */
public class DelimitedCollection
{
	private final Collection collection;

	/**
	 * Returns the String representation of an Object.
	 *
	 * @param  the object type
	 * @author Toby Reyelts
	 */
	@SuppressWarnings("PublicInnerClass")
	public interface Stringifier
	{
		/**
		 * Returns the String representation of the object.
		 *
		 * @param t the object
		 * @return the String representation
		 */
		public String toString(T t);
	}
	/**
	 * Returns Object.toString().
	 *
	 * @author Toby Reyelts
	 */
	private static Stringifier defaultStringifier = new Stringifier()
	{
		@Override
		public String toString(Object obj)
		{
			return obj.toString();
		}
	};

	/**
	 * Creates a new DelimitedCollection.
	 *
	 * @param collection the collection
	 */
	public DelimitedCollection(Collection collection)
	{
		this.collection = collection;
	}

	/**
	 * Returns the String representation of the collection.
	 *
	 * @param separator the separator string
	 * @return the String representation of the collection
	 */
	public String toString(String separator)
	{
		@SuppressWarnings("unchecked")
		Stringifier stringifier = (Stringifier) defaultStringifier;
		return toString(stringifier, separator);
	}

	/**
	 * Returns the String representation of the collection.
	 *
	 * @param stringifier generates the String representation of the collection elements
	 * @param separator the separator string
	 * @return the String representation of the collection
	 */
	public String toString(Stringifier stringifier, String separator)
	{
		@SuppressWarnings("StringBufferWithoutInitialCapacity")
		StringBuilder result = new StringBuilder();
		for (Iterator it = collection.iterator(); it.hasNext();)
		{
			result.append(stringifier.toString(it.next()));
			if (!it.hasNext())
				return result.toString();
			result.append(separator);
		}
		return result.toString();
	}
}