From f4c556327f46f224fc20f1a743614b12720b0b57 Mon Sep 17 00:00:00 2001 From: ribeaudc <ribeaudc> Date: Fri, 7 Sep 2007 08:41:51 +0000 Subject: [PATCH] add: - CollectionStyle change: - CollectionUtils uses CollectionStyle SVN: 1632 --- .../common/utilities/CollectionStyle.java | 43 +++++++++ .../common/utilities/CollectionUtils.java | 93 ++++++++----------- .../common/utilities/CollectionUtilsTest.java | 6 +- 3 files changed, 88 insertions(+), 54 deletions(-) create mode 100644 common/source/java/ch/systemsx/cisd/common/utilities/CollectionStyle.java diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/CollectionStyle.java b/common/source/java/ch/systemsx/cisd/common/utilities/CollectionStyle.java new file mode 100644 index 00000000000..2dfebc54c5c --- /dev/null +++ b/common/source/java/ch/systemsx/cisd/common/utilities/CollectionStyle.java @@ -0,0 +1,43 @@ +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 diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/CollectionUtils.java b/common/source/java/ch/systemsx/cisd/common/utilities/CollectionUtils.java index a9617a1ee97..376d96d599e 100644 --- a/common/source/java/ch/systemsx/cisd/common/utilities/CollectionUtils.java +++ b/common/source/java/ch/systemsx/cisd/common/utilities/CollectionUtils.java @@ -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[] { "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)]" * </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 diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/CollectionUtilsTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/CollectionUtilsTest.java index f24736f4f83..ace91873426 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/CollectionUtilsTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/CollectionUtilsTest.java @@ -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)); -- GitLab