Here you can find the source of limitLengthOfPrintedList(Collection> list)
Parameter | Description |
---|---|
list | The list to print. |
public static String limitLengthOfPrintedList(Collection<?> list)
//package com.java2s; //License from project: Open Source License import java.util.Collection; import java.util.Iterator; import java.util.Map; public class Main { /**/*from www . jav a2 s . com*/ * Converts a list to a string that shows at most 100 elements. * * @param list * The list to print. * @return A string representing the first 100 elements of the given list, * or null if the given list is null. * @see Utils#limitLengthOfPrintedList(List, int) */ public static String limitLengthOfPrintedList(Collection<?> list) { return limitLengthOfPrintedList(list, 100); } /** * Converts a collection to a string that shows at most maxSize elements. * * @param collection The collection to print. * @param maxItems How many elements to print, at most. * @return A string representing at most the first maxSize elements of the given * collection, or null if the given collection is null. */ public static String limitLengthOfPrintedList(Collection<?> collection, int maxItems) { if (collection == null) { return null; } int size = getSizeSafely(collection); if (size <= maxItems) { return collection.toString(); } Iterator<?> iter = collection.iterator(); String result = "[" + iter.next(); if (size > 1) { for (int i = 1; i < maxItems; i++) { result += ", " + iter.next(); } } return result + ", ..., plus " + comma(size - maxItems) + " more items]"; } /** * Converts a map to a string that shows at most maxSize elements. * * @param map The map to print. * @param maxItems How many set elements to print, at most. * @return A string representing the first maxSize elements of the given * map, or null if the given map is null. */ public static String limitLengthOfPrintedList(Map<?, ?> map, int maxItems) { if (map == null) { return null; } return limitLengthOfPrintedList(map.entrySet(), maxItems); } public static String limitLengthOfPrintedList(Map<?, ?> map) { return limitLengthOfPrintedList(map, 100); } /** * "Safely" returns the size of a collection. * * @param collection * A collection. * @return The size of the given collection, or zero if the collection is null. */ public static int getSizeSafely(Collection<?> collection) { if (collection == null) { return 0; } return collection.size(); } public static int getSizeSafely(Map<?, ?> map) { if (map == null) { return 0; } return map.size(); } public static int getSizeSafely(String str) { if (str == null) { return 0; } return str.length(); } public static int getSizeSafely(Iterator<?> collection) { if (collection == null) { return 0; } int counter = 0; while (collection.hasNext()) { collection.next(); counter++; } return counter; } public static int getSizeSafely(Integer integer) { // This version helps if we lookup in a <Object,Integer> map; i.e., default to 0 if not there. if (integer == null) { return 0; } return integer; } public static <T> String toString(Collection<T> collection, String divider) { StringBuilder sb = new StringBuilder(); boolean first = true; for (T object : collection) { if (first == false) { sb.append(divider); } first = false; sb.append(toString(object, divider)); } return sb.toString(); } public static <T, S> String toString(Map<T, S> map, String divider) { StringBuilder sb = new StringBuilder(); boolean first = true; for (Map.Entry<T, S> entry : map.entrySet()) { if (first == false) { sb.append(divider); } first = false; sb.append(toString(entry.getKey(), divider)).append(" => ").append(toString(entry.getValue(), divider)); } return sb.toString(); } public static String toString(Object object, String divider) { if (object == null) { return null; } else if (object instanceof Collection) { Collection collection = (Collection) object; return toString(collection, divider); } else if (object instanceof Map) { Map map = (Map) object; return toString(map, divider); } else { return object.toString(); } } public static String comma(int value) { // Always use separators (e.g., "100,000"). return String.format("%,d", value); } public static String comma(long value) { // Always use separators (e.g., "100,000"). return String.format("%,d", value); } public static String comma(double value) { // Always use separators (e.g., "100,000"). return String.format("%,f", value); } public static String comma(Collection<?> collection) { return comma(getSizeSafely(collection)); } public static String comma(Map<?, ?> map) { return comma(getSizeSafely(map)); } }