summaryrefslogtreecommitdiff
path: root/java/com/google/devtools/build/android/desugar/IndexedInputs.java
blob: 33c613202038899ab082e35f44689b4b546f6eb1 (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 2017 The Bazel Authors. All rights reserved.
//
// 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 com.google.devtools.build.android.desugar;

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

import com.google.common.collect.ImmutableMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable;

/**
 * Opens the given list of input files and compute an index of all classes in them, to avoid
 * scanning all inputs over and over for each class to load. An indexed inputs can have a parent
 * that is firstly used when a file name is searched.
 */
class IndexedInputs {

  private final ImmutableMap<String, InputFileProvider> inputFiles;

  /**
   * Parent {@link IndexedInputs} to use before to search a file name into this {@link
   * IndexedInputs}.
   */
  @Nullable
  private final IndexedInputs parent;

  /** Index a list of input files without a parent {@link IndexedInputs}. */
  public IndexedInputs(List<InputFileProvider> inputProviders) {
    this.parent = null;
    this.inputFiles = indexInputs(inputProviders);
  }

  /**
   * Create a new {@link IndexedInputs} with input files previously indexed and with a parent {@link
   * IndexedInputs}.
   */
  private IndexedInputs(
      ImmutableMap<String, InputFileProvider> inputFiles, IndexedInputs parentIndexedInputs) {
    this.parent = parentIndexedInputs;
    this.inputFiles = inputFiles;
  }

  /**
   * Create a new {@link IndexedInputs} with input files already indexed and with a parent {@link
   * IndexedInputs}.
   */
  @CheckReturnValue
  public IndexedInputs withParent(IndexedInputs parent) {
    checkState(this.parent == null);
    return new IndexedInputs(this.inputFiles, parent);
  }

  @Nullable
  public InputFileProvider getInputFileProvider(String filename) {
    checkArgument(filename.endsWith(".class"));

    if (parent != null) {
      InputFileProvider inputFileProvider = parent.getInputFileProvider(filename);
      if (inputFileProvider != null) {
        return inputFileProvider;
      }
    }

    return inputFiles.get(filename);
  }

  private ImmutableMap<String, InputFileProvider> indexInputs(
      List<InputFileProvider> inputProviders) {
    Map<String, InputFileProvider> indexedInputs = new HashMap<>();
    for (InputFileProvider inputProvider : inputProviders) {
      for (String relativePath : inputProvider) {
        if (relativePath.endsWith(".class") && !indexedInputs.containsKey(relativePath)) {
          indexedInputs.put(relativePath, inputProvider);
        }
      }
    }
    return ImmutableMap.copyOf(indexedInputs);
  }
}