• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockito.internal.util;
6 
7 import static java.util.Arrays.asList;
8 
9 import java.util.Collection;
10 import java.util.regex.Matcher;
11 import java.util.regex.Pattern;
12 
13 public class StringUtil {
14 
15     private static final Pattern CAPS = Pattern.compile("([A-Z\\d][^A-Z\\d]*)");
16 
StringUtil()17     private StringUtil() {}
18     /**
19      * @param text
20      *            to have the first line removed
21      * @return less first line
22      */
removeFirstLine(String text)23     public static String removeFirstLine(String text) {
24         return text.replaceFirst(".*?\n", "");
25     }
26 
27     /**
28      * Joins Strings with line break character. It adds line break in front, too.
29      * This makes it something like 'format' no really 'join'.
30      */
join(Object .... linesToBreak)31     public static String join(Object ... linesToBreak) {
32         return join("\n", asList(linesToBreak));
33     }
34 
35     /**
36      * Joins Strings with EOL character
37      *
38      * @param start the starting String
39      * @param lines collection to join
40      */
join(String start, Collection<?> lines)41     public static String join(String start, Collection<?> lines) {
42         return join(start, "", lines);
43     }
44 
45     /**
46      * Joins Strings with EOL character
47      *
48      * @param start the starting String
49      * @param linePrefix the prefix for each line to be joined
50      * @param lines collection to join
51      */
join(String start, String linePrefix, Collection<?> lines)52     public static String join(String start, String linePrefix, Collection<?> lines) {
53         if (lines.isEmpty()) {
54             return "";
55         }
56         StringBuilder out = new StringBuilder(start);
57         for (Object line : lines) {
58             out.append(linePrefix).append(line).append("\n");
59         }
60         return out.substring(0, out.length() - 1); //lose last EOL
61     }
62 
decamelizeMatcher(String className)63     public static String decamelizeMatcher(String className) {
64         if (className.length() == 0) {
65             return "<custom argument matcher>";
66         }
67 
68         String decamelized = decamelizeClassName(className);
69 
70         if (decamelized.length() == 0) {
71             return "<" + className + ">";
72         }
73 
74         return "<" + decamelized + ">";
75     }
76 
decamelizeClassName(String className)77     private static String decamelizeClassName(String className) {
78         Matcher match = CAPS.matcher(className);
79         StringBuilder deCameled = new StringBuilder();
80         while (match.find()) {
81             if (deCameled.length() == 0) {
82                 deCameled.append(match.group());
83             } else {
84                 deCameled.append(" ");
85                 deCameled.append(match.group().toLowerCase());
86             }
87         }
88         return deCameled.toString();
89     }
90 }
91