Skip to content
Snippets Groups Projects
Commit f4c55632 authored by ribeaudc's avatar ribeaudc
Browse files

add:

- CollectionStyle
change:
- CollectionUtils uses CollectionStyle

SVN: 1632
parent af9c2710
No related branches found
No related tags found
No related merge requests found
package ch.systemsx.cisd.common.utilities;
/**
* Controls <code>Collection</code> string representation for {@link CollectionUtils}.
*
* @author Christian Ribeaud
*/
public enum CollectionStyle
{
/** Default <code>CollectionStyle</code>. */
DEFAULT_COLLECTION_STYLE("[", "]", ", ");
private final String collectionStart;
private final String collectionEnd;
private final String collectionSeparator;
private CollectionStyle(String collectionStart, String collectionEnd, String collectionSeparator)
{
this.collectionStart = collectionStart;
this.collectionEnd = collectionEnd;
this.collectionSeparator = collectionSeparator;
}
/** Returns the token that terminates a collection string representation. */
public final String getCollectionEnd()
{
return collectionEnd;
}
/** Returns the token that separates the different items of a given collection. */
public final String getCollectionSeparator()
{
return collectionSeparator;
}
/** Returns the token that starts a collection string representation. */
public final String getCollectionStart()
{
return collectionStart;
}
}
\ No newline at end of file
......@@ -27,98 +27,84 @@ import java.util.Iterator;
*/
public final class CollectionUtils
{
public final static String DEFAULT_COLLECTION_START = "[";
public final static String DEFAULT_COLLECTION_END = "]";
public final static String DEFAULT_COLLECTION_SEPARATOR = ", ";
private static String collectionStart = DEFAULT_COLLECTION_START;
private static String collectionEnd = DEFAULT_COLLECTION_END;
private static String collectionSeparator = DEFAULT_COLLECTION_SEPARATOR;
private CollectionUtils()
{
// Can not be instantiated
}
public static final void setCollectionEnd(String collectionEnd)
{
CollectionUtils.collectionEnd = collectionEnd;
}
public static final void setCollectionSeparator(String collectionSeparator)
{
CollectionUtils.collectionSeparator = collectionSeparator;
}
public static final void setCollectionStart(String collectionStart)
{
CollectionUtils.collectionStart = collectionStart;
}
public static final String getCollectionEnd()
{
return collectionEnd;
}
public static final String getCollectionSeparator()
/**
* Abbreviates a given array of <code>Object</code> using ellipses.
* <p>
* By default it shows the number of items left and {@link CollectionStyle#DEFAULT_COLLECTION_STYLE} is used.
* </p>
*
* @param maxLength the maximum number of items that should be shown. If <code>-1</code> then all items will be
* displayed.
*/
public final static String abbreviate(Object[] objects, int maxLength)
{
return collectionSeparator;
return abbreviate(objects, maxLength, true);
}
public static final String getCollectionStart()
/**
* Abbreviates a given <code>Collection</code> using ellipses.
* <p>
* By default it shows the number of items left and {@link CollectionStyle#DEFAULT_COLLECTION_STYLE} is used.
* </p>
*
* @param maxLength the maximum number of items that should be shown. If <code>-1</code> then all items will be
* displayed.
*/
public final static String abbreviate(Collection<?> collection, int maxLength)
{
return collectionStart;
return abbreviate(collection, maxLength, true);
}
/**
* Abbreviates a given array of <code>Object</code> using ellipses.
* <p>
* By default it shows the number of items left.
* By default {@link CollectionStyle#DEFAULT_COLLECTION_STYLE} is used.
* </p>
*
* @param maxLength the maximum number of items that should be shown. If <code>-1</code> then all items will be
* displayed.
*/
public final static String abbreviate(Object[] objects, int maxLength)
public final static String abbreviate(Object[] objects, int maxLength, boolean showLeft)
{
return abbreviate(objects, maxLength, true);
return abbreviate(objects, maxLength, showLeft, CollectionStyle.DEFAULT_COLLECTION_STYLE);
}
/**
* Abbreviates a given <code>Collection</code> using ellipses.
* <p>
* By default it shows the number of items left.
* By default {@link CollectionStyle#DEFAULT_COLLECTION_STYLE} is used.
* </p>
*
* @param maxLength the maximum number of items that should be shown. If <code>-1</code> then all items will be
* displayed.
*/
public final static String abbreviate(Collection<?> collection, int maxLength)
public final static String abbreviate(Collection<?> collection, int maxLength, boolean showLeft)
{
return abbreviate(collection, maxLength, true);
return abbreviate(collection, maxLength, showLeft, CollectionStyle.DEFAULT_COLLECTION_STYLE);
}
/**
* Abbreviates a given array of <code>Object</code> using ellipses.
*
* <pre>
* CollectionUtils.abbreviate(new String[] { "1", "2", "3", "4", "5" }, 3, false) = "[1, 2, 3, ...]"
* CollectionUtils.abbreviate(new String[] { "1", "2", "3", "4", "5" }, 3, true) = "[1, 2, 3, ... (2 left)]"
* CollectionUtils.abbreviate(new String[] { &quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot; }, 3, false) = &quot;[1, 2, 3, ...]&quot;
* CollectionUtils.abbreviate(new String[] { &quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot; }, 3, true) = &quot;[1, 2, 3, ... (2 left)]&quot;
* </pre>
*
* @param maxLength the maximum number of items that should be shown. If <code>-1</code> then all items will be
* displayed.
* @param showLeft whether the number of items left should be displayed at the end of the output. This is only
* relevant if you limit the number of items displayed.
* @param style the style that should be applied to the output.
*/
public final static String abbreviate(Object[] objects, int maxLength, boolean showLeft)
public final static String abbreviate(Object[] objects, int maxLength, boolean showLeft, CollectionStyle style)
{
return abbreviate(Arrays.asList(objects), maxLength, showLeft);
return abbreviate(Arrays.asList(objects), maxLength, showLeft, style);
}
/**
......@@ -128,31 +114,34 @@ public final class CollectionUtils
* displayed.
* @param showLeft whether the number of items left should be displayed at the end of the output. This is only
* relevant if you limit the number of items displayed.
* @param style the style that should be applied to the output.
*/
public final static String abbreviate(Collection<?> collection, int maxLength, boolean showLeft)
public final static String abbreviate(Collection<?> collection, int maxLength, boolean showLeft,
CollectionStyle style)
{
assert collection != null;
StringBuilder builder = new StringBuilder(collectionStart);
StringBuilder builder = new StringBuilder(style.getCollectionStart());
Iterator<?> iterator = collection.iterator();
for (int i = 0; iterator.hasNext() && (i < maxLength || maxLength < 0); i++)
{
if (i > 0)
{
builder.append(collectionSeparator);
builder.append(style.getCollectionSeparator());
}
builder.append(String.valueOf(iterator.next()));
}
int size = collection.size();
if (maxLength > 0 && maxLength < size)
{
builder.append(collectionSeparator);
builder.append(style.getCollectionSeparator());
builder.append("...");
if (showLeft)
{
builder.append(" (").append(size - maxLength).append(" left)");
}
}
builder.append(collectionEnd);
builder.append(style.getCollectionEnd());
return builder.toString();
}
}
\ No newline at end of file
......@@ -43,9 +43,11 @@ public final class CollectionUtilsTest
// Nothing to do here
}
String[] s = StringUtilities.getStrings(5);
CollectionStyle collectionStyle = CollectionStyle.DEFAULT_COLLECTION_STYLE;
String string =
CollectionUtils.getCollectionStart() + StringUtils.join(s, CollectionUtils.getCollectionSeparator())
+ CollectionUtils.getCollectionEnd();
collectionStyle.getCollectionStart()
+ StringUtils.join(s, collectionStyle.getCollectionSeparator())
+ collectionStyle.getCollectionEnd();
assertEquals(string, CollectionUtils.abbreviate(s, -1, false));
assertEquals(string, CollectionUtils.abbreviate(s, -10, false));
assertEquals(string, CollectionUtils.abbreviate(s, 5, false));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment