blob: b952a97575a689a3e6a61ecd0414077be3a18460 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package com.google.devtools.build.lib.skyframe;
15
Kristina Chodorow73fa2032015-08-28 17:57:46 +000016import com.google.devtools.build.lib.cmdline.PackageIdentifier;
Mark Schaller6df81792015-12-10 18:47:47 +000017import com.google.devtools.build.lib.util.Preconditions;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010018import com.google.devtools.build.lib.vfs.Path;
janakrbfdad902017-05-03 21:38:28 +020019import com.google.devtools.build.skyframe.LegacySkyKey;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010020import com.google.devtools.build.skyframe.SkyKey;
21import com.google.devtools.build.skyframe.SkyValue;
22
23/**
24 * A value that represents the result of looking for the existence of a package that owns a
25 * specific directory path. Compare with {@link PackageLookupValue}, which deals with existence of
26 * a specific package.
27 */
28public abstract class ContainingPackageLookupValue implements SkyValue {
Michajlo Matijkiw11184592015-10-16 19:00:45 +000029
30 public static final NoContainingPackage NONE = new NoContainingPackage();
31
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010032 /** Returns whether there is a containing package. */
33 public abstract boolean hasContainingPackage();
34
35 /** If there is a containing package, returns its name. */
Janak Ramakrishnan29c5ab42015-05-14 19:38:12 +000036 public abstract PackageIdentifier getContainingPackageName();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010037
38 /** If there is a containing package, returns its package root */
39 public abstract Path getContainingPackageRoot();
40
41 public static SkyKey key(PackageIdentifier id) {
42 Preconditions.checkArgument(!id.getPackageFragment().isAbsolute(), id);
Brian Silvermand7d6d622016-03-17 09:53:39 +000043 Preconditions.checkArgument(!id.getRepository().isDefault(), id);
janakrbfdad902017-05-03 21:38:28 +020044 return LegacySkyKey.create(SkyFunctions.CONTAINING_PACKAGE_LOOKUP, id);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010045 }
46
Michajlo Matijkiw0b97b912015-10-21 16:24:04 +000047 public static ContainingPackage withContainingPackage(PackageIdentifier pkgId, Path root) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010048 return new ContainingPackage(pkgId, root);
49 }
50
Michajlo Matijkiw11184592015-10-16 19:00:45 +000051 /** Value indicating there is no containing package. */
52 public static class NoContainingPackage extends ContainingPackageLookupValue {
53
54 private NoContainingPackage() {}
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010055
56 @Override
57 public boolean hasContainingPackage() {
58 return false;
59 }
60
61 @Override
62 public PackageIdentifier getContainingPackageName() {
63 throw new IllegalStateException();
64 }
65
66 @Override
67 public Path getContainingPackageRoot() {
68 throw new IllegalStateException();
69 }
70 }
71
Michajlo Matijkiw11184592015-10-16 19:00:45 +000072 /** A successful lookup value. */
73 public static class ContainingPackage extends ContainingPackageLookupValue {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010074 private final PackageIdentifier containingPackage;
75 private final Path containingPackageRoot;
76
77 private ContainingPackage(PackageIdentifier pkgId, Path containingPackageRoot) {
78 this.containingPackage = pkgId;
79 this.containingPackageRoot = containingPackageRoot;
80 }
81
82 @Override
83 public boolean hasContainingPackage() {
84 return true;
85 }
86
87 @Override
88 public PackageIdentifier getContainingPackageName() {
89 return containingPackage;
90 }
91
92 @Override
93 public Path getContainingPackageRoot() {
94 return containingPackageRoot;
95 }
96
97 @Override
98 public boolean equals(Object obj) {
99 if (this == obj) {
100 return true;
101 }
102 if (!(obj instanceof ContainingPackage)) {
103 return false;
104 }
105 ContainingPackage other = (ContainingPackage) obj;
106 return containingPackage.equals(other.containingPackage)
107 && containingPackageRoot.equals(other.containingPackageRoot);
108 }
109
110 @Override
111 public int hashCode() {
112 return containingPackage.hashCode();
113 }
114 }
115}