Decamelizer.java revision e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7
1/*
2 * Copyright (c) 2007 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5
6package org.mockito.internal.util;
7
8import java.util.regex.Matcher;
9import java.util.regex.Pattern;
10
11public class Decamelizer {
12
13private static final Pattern CAPS = Pattern.compile("([A-Z\\d][^A-Z\\d]*)");
14
15    public static String decamelizeMatcher(String className) {
16        if (className.length() == 0) {
17            return "<custom argument matcher>";
18        }
19
20        String decamelized = decamelizeClassName(className);
21
22        if (decamelized.length() == 0) {
23            return "<" + className + ">";
24        }
25
26        return "<" + decamelized + ">";
27    }
28
29    private static String decamelizeClassName(String className) {
30        Matcher match = CAPS.matcher(className);
31        StringBuilder deCameled = new StringBuilder();
32        while(match.find()) {
33            if (deCameled.length() == 0) {
34                deCameled.append(match.group());
35            } else {
36                deCameled.append(" ");
37                deCameled.append(match.group().toLowerCase());
38            }
39        }
40        return deCameled.toString();
41    }
42}
43