aboutsummaryrefslogtreecommitdiff
path: root/javatests/dagger/internal/codegen/JavaFileBuilder.java
blob: caaa28ac4c19d8cf215eaeedbd4535d15b929b85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * Copyright (C) 2017 The Dagger Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package dagger.internal.codegen;

import static com.google.common.base.Preconditions.checkArgument;

import androidx.room.compiler.processing.util.Source;
import com.google.common.collect.ImmutableList;
import dagger.testing.compile.CompilerTests;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
 * A fluent API to build a {@link Source} appropriate for a current set of settings, such as
 * compiler mode.
 *
 * <p>After creating a builder, you can add lines to the file. Call {@link #addLines(String...)} to
 * add lines irrespective of the settings. If you want to add different lines for different possible
 * settings, call {@link #addLinesIf(Object, String...)} to add those lines only if the given
 * setting has been added via {@link #withSetting(Object)} or {@link #withSettings(Object...)}.
 */
final class JavaFileBuilder {
  private final String qualifiedName;
  private final Set<Object> settings = new HashSet<>();

  private final ImmutableList.Builder<String> sourceLines = ImmutableList.builder();

  /** Creates a builder for a file whose top level type has a given qualified name. */
  JavaFileBuilder(String qualifiedName) {
    checkArgument(!qualifiedName.isEmpty());
    this.qualifiedName = qualifiedName;
  }

  // TODO(cgdecker): Get rid of the special constructor/method for CompilerMode

  /** Creates a builder for a file whose top level type has a given qualified name. */
  JavaFileBuilder(CompilerMode compilerMode, String qualifiedName) {
    this(qualifiedName);
    settings.add(compilerMode);
  }

  /** Adds the given setting as one that code should be generated for. */
  JavaFileBuilder withSetting(Object setting) {
    this.settings.add(setting);
    return this;
  }

  /** Adds the given settings as one that code should be generated for. */
  JavaFileBuilder withSettings(Object s1, Object s2, Object... more) {
    settings.add(s1);
    settings.add(s2);
    Collections.addAll(settings, more);
    return this;
  }

  /** Adds lines no matter what the {@link CompilerMode} is. */
  JavaFileBuilder addLines(String... lines) {
    sourceLines.add(lines);
    return this;
  }

  /** Adds lines if in the given mode. */
  JavaFileBuilder addLinesIn(CompilerMode mode, String... lines) {
    return addLinesIf(mode, lines);
  }

  /** Adds lines if in the given setting is set. */
  JavaFileBuilder addLinesIf(Object setting, String... lines) {
    if (settings.contains(setting)) {
      sourceLines.add(lines);
    }
    return this;
  }

  /** Builds the {@link Source}. */
  Source buildSource() {
    return CompilerTests.javaSource(qualifiedName, sourceLines.build().toArray(new String[] {}));
  }
}