Debian Codegraph @

chunkio-processor/src/com/android/tools/chunkio/codegen/ClassDef.java

raw
1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.android.tools.chunkio.codegen;
18
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.EnumSet;
22 import java.util.List;
23 import java.util.Set;
24
25 import javax.lang.model.element.Modifier;
26
27 public final class ClassDef {
28 private final String name;
29 private final List<MethodDef> methods;
30 private final Set<Modifier> modifiers;
31
32 private ClassDef(Builder builder) {
33 name = builder.name;
34 methods = Utils.immutableCopy(builder.methods);
35 modifiers = Utils.immutableCopy(builder.modifiers);
36 }
37
38 public static Builder builder(String name) {
39 return new Builder(name);
40 }
41
42 void emit(CodeGenerator generator) throws IOException {
43 generator.emitModifiers(modifiers);
44 generator.emit("class $L", name);
45 generator.emit(" {\n");
46 generator.indent();
47
48 boolean needNewLine = false;
49 for (MethodDef method : methods) {
50 if (needNewLine) generator.emit("\n");
51 method.emit(generator);
52 needNewLine = true;
53 }
54
55 generator.unindent();
56 generator.emit("}\n");
57 }
58
59 public static final class Builder {
60 private final String name;
61 private final List<MethodDef> methods = new ArrayList<>();
62 private Set<Modifier> modifiers;
63
64 private Builder(String name) {
65 this.name = name;
66 }
67
68 public Builder addMethod(MethodDef method) {
69 methods.add(method);
70 return this;
71 }
72
73 public Builder modifiers(EnumSet<Modifier> modifiers) {
74 this.modifiers = EnumSet.copyOf(modifiers);
75 return this;
76 }
77
78 public ClassDef build() {
79 return new ClassDef(this);
80 }
81 }
82 }