Coverage Report - org.eclipse.swtbot.swt.finder.utils.StringUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
StringUtils
88%
31/35
76%
20/26
2.1
 
 1  
 /*******************************************************************************
 2  
  * Copyright (c) 2008 Ketan Padegaonkar and others.
 3  
  * All rights reserved. This program and the accompanying materials
 4  
  * are made available under the terms of the Eclipse Public License v1.0
 5  
  * which accompanies this distribution, and is available at
 6  
  * http://www.eclipse.org/legal/epl-v10.html
 7  
  * 
 8  
  * Contributors:
 9  
  *     Ketan Padegaonkar - initial API and implementation
 10  
  *******************************************************************************/
 11  
 package org.eclipse.swtbot.swt.finder.utils;
 12  
 
 13  
 import java.util.ArrayList;
 14  
 import java.util.Collection;
 15  
 
 16  
 import org.eclipse.swtbot.swt.finder.utils.internal.Assert;
 17  
 
 18  
 /**
 19  
  * A set of utilities for string manipulation.
 20  
  * 
 21  
  * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 22  
  * @version $Id$
 23  
  * @since 1.0
 24  
  */
 25  0
 public class StringUtils {
 26  
 
 27  
         /**
 28  
          * Joins an array of objects using the given delimiter as spacing.
 29  
          * 
 30  
          * @param toJoin the objects to join into a string.
 31  
          * @param delimiter the delimiter used to join the objects.
 32  
          * @return the result of joining the <code>toJoin</code> with <code>delimiter</code>.
 33  
          */
 34  
         public static String join(Object[] toJoin, String delimiter) {
 35  29
                 if ((toJoin == null) || (toJoin.length == 0))
 36  0
                         return ""; //$NON-NLS-1$
 37  29
                 Assert.isTrue(!isEmptyOrNull(delimiter));
 38  29
                 StringBuffer result = new StringBuffer();
 39  
 
 40  80
                 for (Object object : toJoin) {
 41  51
                         result.append(object);
 42  51
                         result.append(delimiter);
 43  
                 }
 44  
 
 45  29
                 result.lastIndexOf(delimiter);
 46  29
                 result.replace(result.length() - delimiter.length(), result.length(), ""); //$NON-NLS-1$
 47  29
                 return result.toString();
 48  
         }
 49  
 
 50  
         /**
 51  
          * Joins an array of objects using the given delimiter as spacing.
 52  
          * 
 53  
          * @param toJoin the objects to join into a string.
 54  
          * @param delimiter the delimiter used to join the objects.
 55  
          * @param converter the converter that can convert objects in the collection into strings.
 56  
          * @return the result of joining the <code>toJoin</code> with <code>delimiter</code>.
 57  
          */
 58  
         public static String join(Object[] toJoin, String delimiter, StringConverter converter) {
 59  1
                 if ((toJoin == null) || (toJoin.length == 0))
 60  0
                         return ""; //$NON-NLS-1$
 61  1
                 Assert.isTrue(!isEmptyOrNull(delimiter));
 62  
 
 63  1
                 ArrayList<Object> convertedObjects = new ArrayList<Object>();
 64  4
                 for (Object object : toJoin) {
 65  3
                         convertedObjects.add(converter.toString(object));
 66  
                 }
 67  
 
 68  1
                 return join(convertedObjects, delimiter);
 69  
         }
 70  
 
 71  
         /**
 72  
          * Joins a collection of objects using the given delimiter as spacing.
 73  
          * 
 74  
          * @param toJoin the objects to join into a string.
 75  
          * @param delimiter the delimiter used to join the objects.
 76  
          * @return the result of joining the <code>toJoin</code> with <code>delimiter</code>.
 77  
          */
 78  
         public static String join(Collection<?> toJoin, String delimiter) {
 79  1
                 return join(toJoin.toArray(), delimiter);
 80  
         }
 81  
 
 82  
         /**
 83  
          * Joins a collection of objects using the given delimiter as spacing.
 84  
          * 
 85  
          * @param toJoin the objects to join into a string.
 86  
          * @param delimiter the delimiter used to join the objects.
 87  
          * @param converter the converter that can convert objects in the collection into strings.
 88  
          * @return the result of joining the <code>toJoin</code> with <code>delimiter</code>.
 89  
          */
 90  
         public static String join(Collection<?> toJoin, String delimiter, StringConverter converter) {
 91  0
                 return join(toJoin.toArray(), delimiter, converter);
 92  
         }
 93  
 
 94  
         /**
 95  
          * Checks if the given string is <code>null</code> or empty.
 96  
          * 
 97  
          * @param string the string.
 98  
          * @return <code>true</code> if string is null, blank or whitespaces. <code>false</code> otherwise.
 99  
          */
 100  
         public static boolean isEmptyOrNull(String string) {
 101  38
                 return isNull(string) || isEmpty(string);
 102  
         }
 103  
 
 104  
         /**
 105  
          * Joins the given integer array with the given delimiter.
 106  
          * 
 107  
          * @param toJoin the integers to join into a string.
 108  
          * @param delimiter the delimiter.
 109  
          * @return the result of joining the <code>toJoin</code> with <code>delimiter</code>.
 110  
          */
 111  
         public static String join(int[] toJoin, String delimiter) {
 112  15
                 Integer[] ints = new Integer[toJoin.length];
 113  38
                 for (int i = 0; i < toJoin.length; i++)
 114  23
                         ints[i] = new Integer(toJoin[i]);
 115  15
                 return join(ints, delimiter);
 116  
         }
 117  
 
 118  
         /**
 119  
          * @param text the text.
 120  
          * @return <code>true</code> if the text is empty, <code>false</code> otherwise.
 121  
          * @since 1.3
 122  
          */
 123  
         public static boolean isEmpty(String text) {
 124  38
                 return text.trim().equals(""); //$NON-NLS-1$
 125  
         }
 126  
 
 127  
         /**
 128  
          * @param text the text.
 129  
          * @return <code>true</code> if the text is null, <code>false</code> otherwise.
 130  
          * @since 1.3
 131  
          */
 132  
         public static boolean isNull(String text) {
 133  38
                 return text == null;
 134  
         }
 135  
 
 136  
         /**
 137  
          * Converts the string to camelcase. Strings are of the format: THIS_IS_A_STRING, and the result of camel casing
 138  
          * would be thisIsAString.
 139  
          * 
 140  
          * @param string the string to be camelcased.
 141  
          * @return the camel cased string.
 142  
          * @since 2.0
 143  
          */
 144  
         public static String toCamelCase(String string) {
 145  9
                 StringBuffer result = new StringBuffer(string);
 146  19
                 while (result.indexOf("_") != -1) { //$NON-NLS-1$
 147  1
                         int indexOf = result.indexOf("_"); //$NON-NLS-1$
 148  1
                         result.replace(indexOf, indexOf + 2, "" + Character.toUpperCase(result.charAt(indexOf + 1))); //$NON-NLS-1$
 149  
                 }
 150  9
                 return result.toString();
 151  
         }
 152  
 
 153  
         /**
 154  
          * Converts the string to capitalized. Strings are of the format: THIS_IS_A_STRING, and the result of capitalization
 155  
          * would be ThisIsAString.
 156  
          * 
 157  
          * @param string the string to capitalize.
 158  
          * @return the capitalized string.
 159  
          * @since 2.0
 160  
          */
 161  
         public static String capitalize(String string) {
 162  2
                 StringBuffer result = new StringBuffer(toCamelCase(string));
 163  2
                 result.replace(0, 1, "" + Character.toUpperCase(result.charAt(0))); //$NON-NLS-1$
 164  2
                 return result.toString();
 165  
         }
 166  
 
 167  
 }