1/*
2 * Copyright (c) 2007 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5package org.mockito.internal.util;
6
7import org.mockito.mock.MockName;
8
9import java.io.Serializable;
10
11public class MockNameImpl implements MockName, Serializable {
12
13    private static final long serialVersionUID = 8014974700844306925L;
14    private final String mockName;
15    private boolean defaultName;
16
17    @SuppressWarnings("unchecked")
18    public MockNameImpl(String mockName, Class classToMock) {
19        if (mockName == null) {
20            this.mockName = toInstanceName(classToMock);
21            this.defaultName = true;
22        } else {
23            this.mockName = mockName;
24        }
25    }
26
27    public MockNameImpl(String mockName) {
28        this.mockName = mockName;
29    }
30
31    private static String toInstanceName(Class<?> clazz) {
32        String className = clazz.getSimpleName();
33        if (className.length() == 0) {
34            //it's an anonymous class, let's get name from the parent
35            className = clazz.getSuperclass().getSimpleName();
36        }
37        //lower case first letter
38        return className.substring(0, 1).toLowerCase() + className.substring(1);
39    }
40
41    public boolean isDefault() {
42        return defaultName;
43    }
44
45    @Override
46    public String toString() {
47        return mockName;
48    }
49}
50