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