Java Collection Convert toToolTip(final Collection collection)

Here you can find the source of toToolTip(final Collection collection)

Description

to Tool Tip

License

Open Source License

Parameter

Parameter Description
collection a parameter

Return

String

Declaration

public static String toToolTip(final Collection<?> collection) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Manchester Centre for Integrative Systems Biology
 * University of Manchester/*w  ww . j a v a2  s . c  om*/
 * Manchester M1 7ND
 * United Kingdom
 * 
 * Copyright (C) 2007 University of Manchester
 * 
 * This program is released under the Academic Free License ("AFL") v3.0.
 * (http://www.opensource.org/licenses/academic.php)
 *******************************************************************************/

import java.util.*;

public class Main {
    /**
     * 
     */
    public static final String ELEMENT_SEPARATOR = ",(\\s)*";
    /**
     * 
     */
    private static final String EMPTY_STRING = "";
    /**
     * 
     */
    private static final String ARRAY_SEPARATOR = ";";
    /**
     * 
     */
    private static final String ARRAY_OPEN_TAG = "[";
    /**
     * 
     */
    private static final String ARRAY_CLOSE_TAG = "]";

    /**
     * 
     * @param collection
     * @return String
     */
    public static String toToolTip(final Collection<?> collection) {
        if (collection == null || collection.size() == 0) {
            return null;
        }

        final String START_SEPARATOR = "<p>"; //$NON-NLS-1$
        final String END_SEPARATOR = "</p>"; //$NON-NLS-1$
        final StringBuffer buffer = new StringBuffer("<html>"); //$NON-NLS-1$

        for (Iterator<?> iterator = collection.iterator(); iterator.hasNext();) {
            buffer.append(START_SEPARATOR);
            buffer.append(iterator.next());
            buffer.append(END_SEPARATOR);
        }

        buffer.append("</html>"); //$NON-NLS-1$

        return buffer.toString();
    }

    /**
     * 
     * @param array
     * @return String
     */
    public static String toString(double[][] array) {
        final StringBuffer buffer = new StringBuffer(ARRAY_OPEN_TAG);

        for (int i = 0; i < array.length; i++) {
            final StringBuffer tempBuffer = new StringBuffer(Arrays.toString(array[i]));
            buffer.append(tempBuffer.substring(1, tempBuffer.length() - 1));
            buffer.append(ARRAY_SEPARATOR);
        }

        buffer.append(ARRAY_CLOSE_TAG);
        return buffer.toString();
    }

    /**
     * 
     * @param collection
     * @return String
     */
    public static String toString(final Collection<?> collection) {
        return toString(collection.toArray());
    }

    /**
     * 
     * @param array
     * @return String
     */
    public static String toString(final Object[] array) {
        return Arrays.toString(array).replace(ARRAY_OPEN_TAG, EMPTY_STRING).replace(ARRAY_CLOSE_TAG, EMPTY_STRING);
    }

    /**
     * 
     * @param value
     * @return Object[]
     */
    public static Object[] toArray(final String value) {
        if (value == null) {
            return new Object[] {};
        }

        final int BRACKET_LENGTH = 1;
        final String strippedValue = value.substring(BRACKET_LENGTH, value.length() - BRACKET_LENGTH);
        final StringTokenizer tokenizer = new StringTokenizer(strippedValue, ELEMENT_SEPARATOR);
        final Collection<Object> collection = new ArrayList<>();

        while (tokenizer.hasMoreTokens()) {
            collection.add(tokenizer.nextToken().trim());
        }

        return collection.toArray();
    }
}

Related

  1. toSQLIn(Collection values)
  2. toStringBuilder( Collection collection, String delimiter)
  3. toStringColl(Collection set)
  4. toSV(Collection collection, String separator)
  5. toTestParameters(Collection rawParams)
  6. toTreeSet(Collection collection, Comparator comparator)
  7. toUnmodifiableCollection( AnyCollection collection)
  8. trimCollection(Collection collectionToBeConverted)