| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Strings |
|
| 1.6666666666666667;1.667 |
| 1 | /* | |
| 2 | Copyright 2004-2008 Paul R. Holser, Jr. All rights reserved. | |
| 3 | Licensed under the Academic Free License version 3.0 | |
| 4 | */ | |
| 5 | ||
| 6 | package joptsimple.internal; | |
| 7 | ||
| 8 | import java.util.Arrays; | |
| 9 | import java.util.Iterator; | |
| 10 | import java.util.List; | |
| 11 | ||
| 12 | /** | |
| 13 | * @since 2.1 | |
| 14 | * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a> | |
| 15 | * @version $Id: Strings.java,v 1.3 2008/05/22 14:22:33 pholser Exp $ | |
| 16 | */ | |
| 17 | public final class Strings { | |
| 18 | public static final String EMPTY = ""; | |
| 19 | public static final String SINGLE_QUOTE = "'"; | |
| 20 | 2 | public static final String LINE_SEPARATOR = System.getProperty( "line.separator" ); |
| 21 | ||
| 22 | /** | |
| 23 | * <p>Do not instantiate -- statics only.</p> | |
| 24 | */ | |
| 25 | 2 | protected Strings() { |
| 26 | 2 | throw new UnsupportedOperationException(); |
| 27 | } | |
| 28 | ||
| 29 | /** | |
| 30 | * <p>Gives a string consisting of the given character repeated the given number of | |
| 31 | * times.</p> | |
| 32 | * | |
| 33 | * @param ch the character to repeat | |
| 34 | * @param count how many times to repeat the character | |
| 35 | * @return the resultant string | |
| 36 | */ | |
| 37 | public static String repeat( char ch, int count ) { | |
| 38 | 464 | StringBuffer buffer = new StringBuffer(); |
| 39 | ||
| 40 | 10716 | for ( int i = 0; i < count; ++i ) |
| 41 | 10252 | buffer.append( ch ); |
| 42 | ||
| 43 | 464 | return buffer.toString(); |
| 44 | } | |
| 45 | ||
| 46 | /** | |
| 47 | * <p>Tells whether the given string is either <code>null</code> or consists solely | |
| 48 | * of whitespace characters.</p> | |
| 49 | * | |
| 50 | * @param target string to check | |
| 51 | * @return <code>true</code> if the target string is null or empty | |
| 52 | */ | |
| 53 | public static boolean isNullOrEmpty( String target ) { | |
| 54 | 274 | return target == null || EMPTY.equals( target ); |
| 55 | } | |
| 56 | ||
| 57 | ||
| 58 | /** | |
| 59 | * <p>Returns a string consisting of a given string prepended and appended with | |
| 60 | * surrounding characters.</p> | |
| 61 | * | |
| 62 | * @param target a string | |
| 63 | * @param begin character to prepend | |
| 64 | * @param end character to append | |
| 65 | * @return the surrounded string | |
| 66 | */ | |
| 67 | public static String surround( String target, char begin, char end ) { | |
| 68 | 20 | return begin + target + end; |
| 69 | } | |
| 70 | ||
| 71 | /** | |
| 72 | * Returns a string consisting of the elements of a given array of strings, each | |
| 73 | * separated by a given separator string. | |
| 74 | * | |
| 75 | * @param pieces the strings to join | |
| 76 | * @param separator the separator | |
| 77 | * @return the joined string | |
| 78 | */ | |
| 79 | public static String join( String[] pieces, String separator ) { | |
| 80 | 36 | return join( Arrays.asList( pieces ), separator ); |
| 81 | } | |
| 82 | ||
| 83 | /** | |
| 84 | * Returns a string consisting of the string representations of the elements of a | |
| 85 | * given array of objects, each separated by a given separator string. | |
| 86 | * | |
| 87 | * @param pieces the elements whose string representations are to be joined | |
| 88 | * @param separator the separator | |
| 89 | * @return the joined string | |
| 90 | */ | |
| 91 | public static String join( List pieces, String separator ) { | |
| 92 | 36 | StringBuffer buffer = new StringBuffer(); |
| 93 | ||
| 94 | 36 | for ( Iterator iter = pieces.iterator(); iter.hasNext(); ) { |
| 95 | 232 | buffer.append( iter.next() ); |
| 96 | ||
| 97 | 232 | if ( iter.hasNext() ) |
| 98 | 196 | buffer.append( separator ); |
| 99 | } | |
| 100 | ||
| 101 | 36 | return buffer.toString(); |
| 102 | } | |
| 103 | } |