Debian Codegraph @

chunkio-processor/src/com/android/tools/chunkio/codegen/ParameterDef.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.lang.reflect.Type;
21 import java.util.EnumSet;
22 import java.util.Set;
23
24 import javax.lang.model.element.Modifier;
25
26 public final class ParameterDef {
27 private final String name;
28 private final TypeDef type;
29 private final Set<Modifier> modifiers;
30
31 private ParameterDef(Builder builder) {
32 name = builder.name;
33 type = builder.type;
34 modifiers = Utils.immutableCopy(builder.modifiers);
35 }
36
37 public static Builder builder(Type type, String name, EnumSet<Modifier> modifiers) {
38 return new Builder(type, name).modifiers(modifiers);
39 }
40
41 public static Builder builder(TypeDef type, String name, EnumSet<Modifier> modifiers) {
42 return new Builder(type, name).modifiers(modifiers);
43 }
44
45 void emit(CodeGenerator generator) throws IOException {
46 generator.emitModifiers(modifiers);
47 generator.emit("$T $L", type, name);
48 }
49
50 public static final class Builder {
51 private final String name;
52 private TypeDef type;
53 private Set<Modifier> modifiers;
54
55 private Builder(Type type, String name) {
56 this.name = name;
57 this.type = TypeDef.of(type);
58 }
59
60 private Builder(TypeDef type, String name) {
61 this.type = type;
62 this.name = name;
63 }
64
65 private Builder modifiers(EnumSet<Modifier> modifiers) {
66 this.modifiers = EnumSet.copyOf(modifiers);
67 return this;
68 }
69
70 ParameterDef build() {
71 return new ParameterDef(this);
72 }
73 }
74 }