View Javadoc
1   package org.apache.maven.surefire.util.internal;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.StringTokenizer;
23  
24  import static java.lang.System.lineSeparator;
25  
26  /**
27   * <p>
28   * Common {@link String java.lang.String} manipulation routines.
29   * </p>
30   * <br>
31   * <p>
32   * Originally from <a href="http://jakarta.apache.org/turbine/">Turbine</a> and the GenerationJavaCore library.
33   * </p>
34   * <br>
35   * NOTE: This class is not part of any api and is public purely for technical reasons !
36   *
37   * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
38   * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
39   * @author <a href="mailto:gcoladonato@yahoo.com">Greg Coladonato</a>
40   * @author <a href="mailto:bayard@generationjava.com">Henri Yandell</a>
41   * @author <a href="mailto:ed@codehaus.org">Ed Korthof</a>
42   * @author <a href="mailto:rand_mcneely@yahoo.com">Rand McNeely</a>
43   * @author Stephen Colebourne
44   * @author <a href="mailto:fredrik@westermarck.com">Fredrik Westermarck</a>
45   * @author Holger Krauth
46   * @author <a href="mailto:alex@purpletech.com">Alexander Day Chaffee</a>
47   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
48   * @version $Id: StringUtils.java 8001 2009-01-03 13:17:09Z vsiveton $
49   * @since 1.0
50   */
51  public final class StringUtils
52  {
53      public static final String NL = lineSeparator();
54  
55      private StringUtils()
56      {
57          throw new IllegalStateException( "no instantiable constructor" );
58      }
59  
60      public static String[] split( String text, String separator )
61      {
62          final StringTokenizer tok;
63          if ( separator == null )
64          {
65              // Null separator means we're using StringTokenizer's default
66              // delimiter, which comprises all whitespace characters.
67              tok = new StringTokenizer( text );
68          }
69          else
70          {
71              tok = new StringTokenizer( text, separator );
72          }
73  
74          String[] list = new String[tok.countTokens()];
75          for ( int i = 0; tok.hasMoreTokens(); i++ )
76          {
77              list[i] = tok.nextToken();
78          }
79          return list;
80      }
81  
82      /**
83       * <p>
84       * Checks if a (trimmed) String is {@code null} or blank.
85       * </p>
86       *
87       * @param str the String to check
88       * @return {@code true} if the String is {@code null}, or length zero once trimmed
89       */
90      public static boolean isBlank( String str )
91      {
92          return str == null || str.trim().isEmpty();
93      }
94  
95      /**
96       * <p>
97       * Checks if a (trimmed) String is not {@code null} and not blank.
98       * </p>
99       *
100      * @param str the String to check
101      * @return {@code true} if the String is not {@code null} and length of trimmed {@code str} is not zero.
102      */
103     public static boolean isNotBlank( String str )
104     {
105         return !isBlank( str );
106     }
107 
108     /**
109      * Determines if {@code buffer} starts with specific literal(s).
110      *
111      * @param buffer     Examined StringBuffer
112      * @param pattern    a pattern which should start in {@code buffer}
113      * @return    {@code true} if buffer's literal starts with given {@code pattern}, or both are empty.
114      */
115     public static boolean startsWith( StringBuffer buffer, String pattern )
116     {
117         if ( buffer.length() < pattern.length() )
118         {
119             return false;
120         }
121         else
122         {
123             for ( int i = 0, len = pattern.length(); i < len; i++ )
124             {
125                 if ( buffer.charAt( i ) != pattern.charAt( i ) )
126                 {
127                     return false;
128                 }
129             }
130             return true;
131         }
132     }
133 }