Debian Codegraph @

chunkio-processor/src/com/android/tools/chunkio/codegen/CodeSnippet.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 javax.lang.model.type.TypeMirror;
20 import java.lang.reflect.Type;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Iterator;
24 import java.util.List;
25
26 final class CodeSnippet {
27 private final List<String> parts;
28 private final List<Object> values;
29
30 private CodeSnippet(Builder builder) {
31 parts = Utils.immutableCopy(builder.parts);
32 values = Utils.immutableCopy(builder.values);
33 }
34
35 public static Builder builder() {
36 return new Builder();
37 }
38
39 List<String> getParts() {
40 return parts;
41 }
42
43 List<Object> getValues() {
44 return values;
45 }
46
47 public static final class Builder {
48 private final List<String> parts = new ArrayList<>();
49 private final List<Object> values = new ArrayList<>();
50
51 private Builder() {
52 }
53
54 CodeSnippet build() {
55 return new CodeSnippet(this);
56 }
57
58 public Builder add(CodeSnippet snippet) {
59 parts.addAll(snippet.parts);
60 values.addAll(snippet.values);
61 return this;
62 }
63
64 public Builder add(String format, Object... args) {
65 Iterator<Object> values = Arrays.asList(args).iterator();
66
67 int index = 0;
68 while (index < format.length()) {
69 int prevIndex = index;
70 if (format.charAt(index) == '$') {
71 if (index + 1 >= format.length()) {
72 throw new IllegalArgumentException("Malformed $ expression in format " +
73 "string: " + format);
74 }
75
76 switch (format.charAt(index + 1)) {
77 case 'L':
78 this.values.add(asLiteral(values.next()));
79 break;
80 case 'N':
81 this.values.add(asName(values.next()));
82 break;
83 case 'S':
84 this.values.add(asString(values.next()));
85 break;
86 case 'T':
87 this.values.add(asType(values.next()));
88 break;
89 case '$':
90 case '<':
91 case '>':
92 case '[':
93 case ']':
94 break;
95 default:
96 throw new IllegalArgumentException("Invalid escape sequence $" +
97 format.charAt(index + 1) + " in format string: " + format);
98 }
99
100 index += 2;
101 parts.add(format.substring(prevIndex, index));
102 } else {
103 index = format.indexOf('$', index + 1);
104 if (index == -1) index = format.length();
105 parts.add(format.substring(prevIndex, index));
106 }
107 }
108
109 return this;
110 }
111
112 Builder addStatement(String format, Object... args) {
113 add("$[");
114 add(format, args);
115 add(";\n$]");
116 return this;
117 }
118
119 Builder beginControlStatement(String format, Object... args) {
120 add(format, args);
121 add(" {\n");
122 indent();
123 return this;
124 }
125
126 Builder continueControlStatement(String format, Object... args) {
127 unindent();
128 add("} ");
129 add(format, args);
130 add(" {\n");
131 indent();
132 return this;
133 }
134
135 Builder endControlStatement() {
136 return endBlock();
137 }
138
139 private Builder indent() {
140 parts.add("$>");
141 return this;
142 }
143
144 private Builder unindent() {
145 parts.add("$<");
146 return this;
147 }
148
149 Builder beginBlock() {
150 add("{\n");
151 indent();
152 return this;
153 }
154
155 Builder endBlock() {
156 unindent();
157 add("}\n");
158 return this;
159 }
160
161 private static Object asType(Object value) {
162 if (value instanceof TypeDef) return value;
163 if (value instanceof Type) return TypeDef.of((Type) value);
164 if (value instanceof TypeMirror) return TypeDef.of((TypeMirror) value);
165 throw new IllegalArgumentException("Invalid type: " + value.getClass());
166 }
167
168 private static Object asName(Object value) {
169 if (value instanceof CharSequence) {
170 return String.valueOf(value);
171 }
172 if (value instanceof MethodDef) {
173 return ((MethodDef) value).getName();
174 }
175 throw new IllegalArgumentException("Name expected in format string, got " +
176 (value == null ? "null" : value.getClass()));
177 }
178
179 private static Object asLiteral(Object value) {
180 return value;
181 }
182
183 private static Object asString(Object value) {
184 return value == null ? null : value.toString();
185 }
186 }
187 }