• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 Google, Inc.
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 package dagger.internal.codegen.writer;
17 
18 import dagger.internal.codegen.writer.Writable.Context;
19 import java.io.IOException;
20 import java.util.Iterator;
21 import java.util.Set;
22 
23 final class Writables {
24 
25   /**
26    * Joins the writables by the given delimiter, writing out the
27    * prefix & suffix if there's at least one element.
28    */
join(String delimiter, Iterable<? extends Writable> writables, String prefix, String suffix, Appendable appendable, Context context)29   static void join(String delimiter, Iterable<? extends Writable> writables,
30       String prefix, String suffix,
31       Appendable appendable, Context context) throws IOException {
32     Iterator<? extends Writable> iter = writables.iterator();
33     if (iter.hasNext()) {
34       appendable.append(prefix);
35       iter.next().write(appendable, context);
36       while (iter.hasNext()) {
37         appendable.append(delimiter);
38         iter.next().write(appendable, context);
39       }
40       appendable.append(suffix);
41     }
42   }
43 
44   /** Joins the writables by the given delimiter. */
join(String delimiter, Iterable<? extends Writable> writables, Appendable appendable, Context context)45   static void join(String delimiter, Iterable<? extends Writable> writables,
46       Appendable appendable, Context context) throws IOException {
47     join(delimiter, writables, "", "", appendable, context);
48   }
49 
toStringWritable(final Object object)50   static Writable toStringWritable(final Object object) {
51     return new Writable() {
52       @Override
53       public Appendable write(Appendable appendable, Context context) throws IOException {
54         return appendable.append(object.toString());
55       }
56     };
57   }
58 
59   private static final Context DEFAULT_CONTEXT = new Context() {
60     @Override
61     public String sourceReferenceForClassName(ClassName className) {
62       return className.canonicalName();
63     }
64 
65     @Override
66     public Context createSubcontext(Set<ClassName> newTypes) {
67       throw new UnsupportedOperationException();
68     }
69   };
70 
71   static String writeToString(Writable writable) {
72     StringBuilder builder = new StringBuilder();
73     try {
74       writable.write(builder, DEFAULT_CONTEXT);
75     } catch (IOException e) {
76       throw new AssertionError("StringBuilder doesn't throw IOException" + e);
77     }
78     return builder.toString();
79   }
80 
81   private Writables() {
82   }
83 }
84