1 /* 2 * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* 27 * This file is available under and governed by the GNU General Public 28 * License version 2 only, as published by the Free Software Foundation. 29 * However, the following notice accompanied the original version of this 30 * file: 31 * 32 * The MIT License 33 * 34 * Copyright (c) 2004-2015 Paul R. Holser, Jr. 35 * 36 * Permission is hereby granted, free of charge, to any person obtaining 37 * a copy of this software and associated documentation files (the 38 * "Software"), to deal in the Software without restriction, including 39 * without limitation the rights to use, copy, modify, merge, publish, 40 * distribute, sublicense, and/or sell copies of the Software, and to 41 * permit persons to whom the Software is furnished to do so, subject to 42 * the following conditions: 43 * 44 * The above copyright notice and this permission notice shall be 45 * included in all copies or substantial portions of the Software. 46 * 47 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 48 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 49 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 50 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 51 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 52 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 53 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 54 */ 55 56 package jdk.internal.joptsimple.internal; 57 58 import java.util.Iterator; 59 60 import static java.lang.System.*; 61 import static java.util.Arrays.*; 62 63 /** 64 * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a> 65 */ 66 public final class Strings { 67 public static final String EMPTY = ""; 68 public static final String LINE_SEPARATOR = getProperty( "line.separator" ); 69 Strings()70 private Strings() { 71 throw new UnsupportedOperationException(); 72 } 73 74 /** 75 * Gives a string consisting of the given character repeated the given number of times. 76 * 77 * @param ch the character to repeat 78 * @param count how many times to repeat the character 79 * @return the resultant string 80 */ repeat( char ch, int count )81 public static String repeat( char ch, int count ) { 82 StringBuilder buffer = new StringBuilder(); 83 84 for ( int i = 0; i < count; ++i ) 85 buffer.append( ch ); 86 87 return buffer.toString(); 88 } 89 90 /** 91 * Tells whether the given string is either {@code} or consists solely of whitespace characters. 92 * 93 * @param target string to check 94 * @return {@code true} if the target string is null or empty 95 */ isNullOrEmpty( String target )96 public static boolean isNullOrEmpty( String target ) { 97 return target == null || target.isEmpty(); 98 } 99 100 101 /** 102 * Gives a string consisting of a given string prepended and appended with surrounding characters. 103 * 104 * @param target a string 105 * @param begin character to prepend 106 * @param end character to append 107 * @return the surrounded string 108 */ surround( String target, char begin, char end )109 public static String surround( String target, char begin, char end ) { 110 return begin + target + end; 111 } 112 113 /** 114 * Gives a string consisting of the elements of a given array of strings, each separated by a given separator 115 * string. 116 * 117 * @param pieces the strings to join 118 * @param separator the separator 119 * @return the joined string 120 */ join( String[] pieces, String separator )121 public static String join( String[] pieces, String separator ) { 122 return join( asList( pieces ), separator ); 123 } 124 125 /** 126 * Gives a string consisting of the string representations of the elements of a given array of objects, 127 * each separated by a given separator string. 128 * 129 * @param pieces the elements whose string representations are to be joined 130 * @param separator the separator 131 * @return the joined string 132 */ join( Iterable<String> pieces, String separator )133 public static String join( Iterable<String> pieces, String separator ) { 134 StringBuilder buffer = new StringBuilder(); 135 136 for ( Iterator<String> iter = pieces.iterator(); iter.hasNext(); ) { 137 buffer.append( iter.next() ); 138 139 if ( iter.hasNext() ) 140 buffer.append( separator ); 141 } 142 143 return buffer.toString(); 144 } 145 } 146