View Javadoc
1   package org.apache.maven.surefire.api.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       * Determines if {@code buffer} starts with specific literal(s).
84       *
85       * @param buffer     Examined StringBuffer
86       * @param pattern    a pattern which should start in {@code buffer}
87       * @return    {@code true} if buffer's literal starts with given {@code pattern}, or both are empty.
88       */
89      public static boolean startsWith( StringBuffer buffer, String pattern )
90      {
91          if ( buffer.length() < pattern.length() )
92          {
93              return false;
94          }
95          else
96          {
97              for ( int i = 0, len = pattern.length(); i < len; i++ )
98              {
99                  if ( buffer.charAt( i ) != pattern.charAt( i ) )
100                 {
101                     return false;
102                 }
103             }
104             return true;
105         }
106     }
107 }