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 */ 16package dagger.internal.codegen.writer; 17 18import com.google.common.collect.Iterables; 19import com.google.common.collect.Lists; 20import dagger.internal.codegen.writer.Writable.Context; 21import java.io.IOException; 22import java.lang.annotation.Annotation; 23import java.util.EnumSet; 24import java.util.List; 25import java.util.Set; 26import javax.lang.model.element.Modifier; 27 28public abstract class Modifiable { 29 final Set<Modifier> modifiers; 30 final List<AnnotationWriter> annotations; 31 32 Modifiable() { 33 this.modifiers = EnumSet.noneOf(Modifier.class); 34 this.annotations = Lists.newArrayList(); 35 } 36 37 public void addModifiers(Modifier first, Modifier... rest) { 38 addModifiers(Lists.asList(first, rest)); 39 } 40 41 public void addModifiers(Iterable<Modifier> modifiers) { 42 Iterables.addAll(this.modifiers, modifiers); 43 } 44 45 public AnnotationWriter annotate(ClassName annotation) { 46 AnnotationWriter annotationWriter = new AnnotationWriter(annotation); 47 this.annotations.add(annotationWriter); 48 return annotationWriter; 49 } 50 51 public AnnotationWriter annotate(Class<? extends Annotation> annotation) { 52 return annotate(ClassName.fromClass(annotation)); 53 } 54 55 Appendable writeModifiers(Appendable appendable) throws IOException { 56 for (Modifier modifier : modifiers) { 57 appendable.append(modifier.toString()).append(' '); 58 } 59 return appendable; 60 } 61 62 Appendable writeAnnotations(Appendable appendable, Context context) throws IOException { 63 for (AnnotationWriter annotationWriter : annotations) { 64 annotationWriter.write(appendable, context).append('\n'); 65 } 66 return appendable; 67 } 68} 69