1/*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24import java.util.Collection;
25import java.util.stream.Collectors;
26
27public enum  ClassType {
28    CLASS("class"),
29    INTERFACE("interface") {
30        @Override
31        public String methodToString(TestCase.TestMethodInfo method) {
32            String modifiers = method.mods.stream()
33                    .collect(Collectors.joining(" "));
34            boolean hasBody = modifiers.contains("static") || modifiers.contains("default");
35            String parameters = method.parameters.stream()
36                    .map(TestCase.TestMemberInfo::generateSource)
37                    .collect(Collectors.joining(", "));
38            return String.format("%s %s %s(%s) %s",
39                    method.indention() + modifiers,
40                    "int",
41                    method.getName(),
42                    parameters,
43                    hasBody ? "{return 0;}" : ";");
44        }
45    },
46    ANNOTATION("@interface") {
47        @Override
48        public String methodToString(TestCase.TestMethodInfo method) {
49            String modifiers = method.mods.stream()
50                    .collect(Collectors.joining(" "));
51            return String.format("%s %s %s() %s",
52                    method.indention() + modifiers,
53                    "int",
54                    method.getName(),
55                    ";");
56        }
57    },
58    ENUM("enum") {
59        @Override
60        public String fieldToString(TestCase.TestFieldInfo field) {
61            return field.indention() + field.name;
62        }
63
64        @Override
65        public String collectFields(Collection<TestCase.TestFieldInfo> fields) {
66            return fields.stream()
67                    .map(TestCase.TestMemberInfo::generateSource)
68                    .collect(Collectors.joining(",\n")) + ";\n";
69        }
70    };
71
72    private final String classType;
73
74    ClassType(String classType) {
75        this.classType = classType;
76    }
77
78    private String collectSrc(Collection<? extends TestCase.TestMemberInfo> members) {
79        String src = members.stream()
80                .map(TestCase.TestMemberInfo::generateSource)
81                .collect(Collectors.joining("\n"));
82        return src.trim().isEmpty() ? "" : src + "\n\n";
83    }
84
85    public String collectInnerClasses(Collection<TestCase.TestClassInfo> innerClasses) {
86        return collectSrc(innerClasses);
87    }
88
89    public String collectFields(Collection<TestCase.TestFieldInfo> fields) {
90        return collectSrc(fields);
91    }
92
93    public String collectMethods(Collection<TestCase.TestMethodInfo> methods) {
94        return collectSrc(methods);
95    }
96
97    public String methodToString(TestCase.TestMethodInfo method) {
98        String modifiers = method.mods.stream()
99                .collect(Collectors.joining(" "));
100        String parameters = method.parameters.stream()
101                .map(TestCase.TestMemberInfo::generateSource)
102                .collect(Collectors.joining(", "));
103        String localClasses = collectInnerClasses(method.localClasses.values());
104        String methodBody = modifiers.contains("abstract") ? ";" :
105                String.format("{%n%s%s%n%s}",
106                        localClasses,
107                        method.isConstructor
108                                ? ""
109                                : method.indention() + "return false;",
110                        method.indention());
111        return String.format("%s %s %s(%s) %s",
112                method.indention() + modifiers,
113                method.isConstructor ? "" : "boolean",
114                method.getName(),
115                parameters,
116                methodBody);
117    }
118
119    public String fieldToString(TestCase.TestFieldInfo field) {
120        String modifiers = field.mods.stream()
121                .collect(Collectors.joining(" "));
122        return String.format("%s int %s = 0;",
123                field.indention() + modifiers,
124                field.name);
125    }
126
127    public String getDescription() {
128        return classType;
129    }
130}
131