Coverage Report - joptsimple.internal.Strings
 
Classes in this File Line Coverage Branch Coverage Complexity
Strings
100%
16/16
100%
10/10
1.833
 
 1  
 /*
 2  
  The MIT License
 3  
 
 4  
  Copyright (c) 2004-2015 Paul R. Holser, Jr.
 5  
 
 6  
  Permission is hereby granted, free of charge, to any person obtaining
 7  
  a copy of this software and associated documentation files (the
 8  
  "Software"), to deal in the Software without restriction, including
 9  
  without limitation the rights to use, copy, modify, merge, publish,
 10  
  distribute, sublicense, and/or sell copies of the Software, and to
 11  
  permit persons to whom the Software is furnished to do so, subject to
 12  
  the following conditions:
 13  
 
 14  
  The above copyright notice and this permission notice shall be
 15  
  included in all copies or substantial portions of the Software.
 16  
 
 17  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 18  
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 19  
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 20  
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 21  
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 22  
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 23  
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 24  
 */
 25  
 
 26  
 package joptsimple.internal;
 27  
 
 28  
 import java.util.Iterator;
 29  
 import java.util.List;
 30  
 
 31  
 import static java.lang.System.*;
 32  
 import static java.util.Arrays.*;
 33  
 
 34  
 /**
 35  
  * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
 36  
  */
 37  
 public final class Strings {
 38  
     public static final String EMPTY = "";
 39  1
     public static final String LINE_SEPARATOR = getProperty( "line.separator" );
 40  
 
 41  1
     private Strings() {
 42  1
         throw new UnsupportedOperationException();
 43  
     }
 44  
 
 45  
     /**
 46  
      * Gives a string consisting of the given character repeated the given number of times.
 47  
      *
 48  
      * @param ch the character to repeat
 49  
      * @param count how many times to repeat the character
 50  
      * @return the resultant string
 51  
      */
 52  
     public static String repeat( char ch, int count ) {
 53  897
         StringBuilder buffer = new StringBuilder();
 54  
 
 55  6911
         for ( int i = 0; i < count; ++i )
 56  6014
             buffer.append( ch );
 57  
 
 58  897
         return buffer.toString();
 59  
     }
 60  
 
 61  
     /**
 62  
      * Tells whether the given string is either {@code} or consists solely of whitespace characters.
 63  
      *
 64  
      * @param target string to check
 65  
      * @return {@code true} if the target string is null or empty
 66  
      */
 67  
     public static boolean isNullOrEmpty( String target ) {
 68  737
         return target == null || EMPTY.equals( target );
 69  
     }
 70  
 
 71  
 
 72  
     /**
 73  
      * Gives a string consisting of a given string prepended and appended with surrounding characters.
 74  
      *
 75  
      * @param target a string
 76  
      * @param begin character to prepend
 77  
      * @param end character to append
 78  
      * @return the surrounded string
 79  
      */
 80  
     public static String surround( String target, char begin, char end ) {
 81  8
         return begin + target + end;
 82  
     }
 83  
 
 84  
     /**
 85  
      * Gives a string consisting of the elements of a given array of strings, each separated by a given separator
 86  
      * string.
 87  
      *
 88  
      * @param pieces the strings to join
 89  
      * @param separator the separator
 90  
      * @return the joined string
 91  
      */
 92  
     public static String join( String[] pieces, String separator ) {
 93  65
         return join( asList( pieces ), separator );
 94  
     }
 95  
 
 96  
     /**
 97  
      * Gives a string consisting of the string representations of the elements of a given array of objects,
 98  
      * each separated by a given separator string.
 99  
      *
 100  
      * @param pieces the elements whose string representations are to be joined
 101  
      * @param separator the separator
 102  
      * @return the joined string
 103  
      */
 104  
     public static String join( List<String> pieces, String separator ) {
 105  114
         StringBuilder buffer = new StringBuilder();
 106  
 
 107  114
         for ( Iterator<String> iter = pieces.iterator(); iter.hasNext(); ) {
 108  427
             buffer.append( iter.next() );
 109  
 
 110  427
             if ( iter.hasNext() )
 111  313
                 buffer.append( separator );
 112  
         }
 113  
 
 114  114
         return buffer.toString();
 115  
     }
 116  
 }