Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2 | // |
| 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. |
| 14 | package com.google.devtools.build.lib.skyframe; |
| 15 | |
Kristina Chodorow | 73fa203 | 2015-08-28 17:57:46 +0000 | [diff] [blame] | 16 | import com.google.devtools.build.lib.cmdline.PackageIdentifier; |
Mark Schaller | 6df8179 | 2015-12-10 18:47:47 +0000 | [diff] [blame] | 17 | import com.google.devtools.build.lib.util.Preconditions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 18 | import com.google.devtools.build.lib.vfs.Path; |
janakr | bfdad90 | 2017-05-03 21:38:28 +0200 | [diff] [blame] | 19 | import com.google.devtools.build.skyframe.LegacySkyKey; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 20 | import com.google.devtools.build.skyframe.SkyKey; |
| 21 | import 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 | */ |
| 28 | public abstract class ContainingPackageLookupValue implements SkyValue { |
Michajlo Matijkiw | 1118459 | 2015-10-16 19:00:45 +0000 | [diff] [blame] | 29 | |
| 30 | public static final NoContainingPackage NONE = new NoContainingPackage(); |
| 31 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 32 | /** Returns whether there is a containing package. */ |
| 33 | public abstract boolean hasContainingPackage(); |
| 34 | |
| 35 | /** If there is a containing package, returns its name. */ |
Janak Ramakrishnan | 29c5ab4 | 2015-05-14 19:38:12 +0000 | [diff] [blame] | 36 | public abstract PackageIdentifier getContainingPackageName(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 37 | |
| 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 Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 43 | Preconditions.checkArgument(!id.getRepository().isDefault(), id); |
janakr | bfdad90 | 2017-05-03 21:38:28 +0200 | [diff] [blame] | 44 | return LegacySkyKey.create(SkyFunctions.CONTAINING_PACKAGE_LOOKUP, id); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 45 | } |
| 46 | |
Michajlo Matijkiw | 0b97b91 | 2015-10-21 16:24:04 +0000 | [diff] [blame] | 47 | public static ContainingPackage withContainingPackage(PackageIdentifier pkgId, Path root) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 48 | return new ContainingPackage(pkgId, root); |
| 49 | } |
| 50 | |
Michajlo Matijkiw | 1118459 | 2015-10-16 19:00:45 +0000 | [diff] [blame] | 51 | /** Value indicating there is no containing package. */ |
| 52 | public static class NoContainingPackage extends ContainingPackageLookupValue { |
| 53 | |
| 54 | private NoContainingPackage() {} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 55 | |
| 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 Matijkiw | 1118459 | 2015-10-16 19:00:45 +0000 | [diff] [blame] | 72 | /** A successful lookup value. */ |
| 73 | public static class ContainingPackage extends ContainingPackageLookupValue { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 74 | 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 | } |