aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/analysis/constraints/EnvironmentCollection.java
blob: 5bf0570cc3c1991c260b34c0878260ac80a7eddf (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2015 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.lib.analysis.constraints;

import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.packages.EnvironmentGroup;

import java.util.Map;

/**
 * Contains a set of {@link Environment} labels and their associated groups.
 */
@Immutable
public class EnvironmentCollection {
  private final ImmutableMultimap<EnvironmentGroup, Label> map;

  private EnvironmentCollection(ImmutableMultimap<EnvironmentGroup, Label> map) {
    this.map = map;
  }

  /**
   * Stores an environment's build label along with the group it belongs to.
   */
  static class EnvironmentWithGroup {
    private final Label environment;
    private final EnvironmentGroup group;
    EnvironmentWithGroup(Label environment, EnvironmentGroup group) {
      this.environment = environment;
      this.group = group;
    }
    Label environment() { return environment; }
    EnvironmentGroup group() { return group; }
  }

  /**
   * Returns the build labels of each environment in this collection, ordered by
   * their insertion order in {@link Builder}.
   */
  ImmutableCollection<Label> getEnvironments() {
    return map.values();
  }

  /**
   * Returns the set of groups the environments in this collection belong to, ordered by
   * their insertion order in {@link Builder}
   */
  ImmutableSet<EnvironmentGroup> getGroups() {
    return map.keySet();
  }

  /**
   * Returns the build labels of each environment in this collection paired with the
   * group each environment belongs to, ordered by their insertion order in {@link Builder}.
   */
  ImmutableCollection<EnvironmentWithGroup> getGroupedEnvironments() {
    ImmutableSet.Builder<EnvironmentWithGroup> builder = ImmutableSet.builder();
    for (Map.Entry<EnvironmentGroup, Label> entry : map.entries()) {
      builder.add(new EnvironmentWithGroup(entry.getValue(), entry.getKey()));
    }
    return builder.build();
  }

  /**
   * Returns the environments in this collection that belong to the given group, ordered by
   * their insertion order in {@link Builder}. If no environments belong to the given group,
   * returns an empty collection.
   */
  ImmutableCollection<Label> getEnvironments(EnvironmentGroup group) {
    return map.get(group);
  }

  /**
   * An empty collection.
   */
  static final EnvironmentCollection EMPTY =
      new EnvironmentCollection(ImmutableMultimap.<EnvironmentGroup, Label>of());

  /**
   * Builder for {@link EnvironmentCollection}.
   */
  public static class Builder {
    private final ImmutableMultimap.Builder<EnvironmentGroup, Label> mapBuilder =
        ImmutableMultimap.builder();

    /**
     * Inserts the given environment / owning group pair.
     */
    public Builder put(EnvironmentGroup group, Label environment) {
      mapBuilder.put(group, environment);
      return this;
    }

    /**
     * Inserts the given set of environments, all belonging to the specified group.
     */
    public Builder putAll(EnvironmentGroup group, Iterable<Label> environments) {
      mapBuilder.putAll(group, environments);
      return this;
    }

    /**
     * Inserts the contents of another {@link EnvironmentCollection} into this one.
     */
    public Builder putAll(EnvironmentCollection other) {
      mapBuilder.putAll(other.map);
      return this;
    }

    public EnvironmentCollection build() {
      return new EnvironmentCollection(mapBuilder.build());
    }
  }
}