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.skyframe; |
| 15 | |
| 16 | import javax.annotation.Nullable; |
| 17 | |
| 18 | /** Wrapper for a value or the typed exception thrown when trying to compute it. */ |
| 19 | public abstract class ValueOrException3<E1 extends Exception, E2 extends Exception, |
| 20 | E3 extends Exception> extends ValueOrUntypedException { |
| 21 | |
| 22 | /** Gets the stored value. Throws an exception if one was thrown when computing this value. */ |
| 23 | @Nullable |
| 24 | public abstract SkyValue get() throws E1, E2, E3; |
shreyax | c10ad74 | 2018-04-10 12:44:40 -0700 | [diff] [blame] | 25 | |
| 26 | static <E1 extends Exception, E2 extends Exception, E3 extends Exception> |
| 27 | ValueOrException3<E1, E2, E3> fromUntypedException( |
| 28 | ValueOrUntypedException voe, |
| 29 | Class<E1> exceptionClass1, |
| 30 | Class<E2> exceptionClass2, |
| 31 | Class<E3> exceptionClass3) { |
| 32 | SkyValue value = voe.getValue(); |
| 33 | if (value != null) { |
| 34 | return ofValue(value); |
| 35 | } |
| 36 | Exception e = voe.getException(); |
| 37 | if (e != null) { |
| 38 | if (exceptionClass1.isInstance(e)) { |
| 39 | return ofExn1(exceptionClass1.cast(e)); |
| 40 | } |
| 41 | if (exceptionClass2.isInstance(e)) { |
| 42 | return ofExn2(exceptionClass2.cast(e)); |
| 43 | } |
| 44 | if (exceptionClass3.isInstance(e)) { |
| 45 | return ofExn3(exceptionClass3.cast(e)); |
| 46 | } |
| 47 | } |
| 48 | return ofNullValue(); |
| 49 | } |
| 50 | |
| 51 | private static <E1 extends Exception, E2 extends Exception, E3 extends Exception> |
| 52 | ValueOrException3<E1, E2, E3> ofNullValue() { |
| 53 | return ValueOrException3ValueImpl.ofNullValue(); |
| 54 | } |
| 55 | |
| 56 | private static <E1 extends Exception, E2 extends Exception, E3 extends Exception> |
| 57 | ValueOrException3<E1, E2, E3> ofValue(SkyValue value) { |
| 58 | return new ValueOrException3ValueImpl<>(value); |
| 59 | } |
| 60 | |
| 61 | private static <E1 extends Exception, E2 extends Exception, E3 extends Exception> |
| 62 | ValueOrException3<E1, E2, E3> ofExn1(E1 e) { |
| 63 | return new ValueOrException3Exn1Impl<>(e); |
| 64 | } |
| 65 | |
| 66 | private static <E1 extends Exception, E2 extends Exception, E3 extends Exception> |
| 67 | ValueOrException3<E1, E2, E3> ofExn2(E2 e) { |
| 68 | return new ValueOrException3Exn2Impl<>(e); |
| 69 | } |
| 70 | |
| 71 | private static <E1 extends Exception, E2 extends Exception, E3 extends Exception> |
| 72 | ValueOrException3<E1, E2, E3> ofExn3(E3 e) { |
| 73 | return new ValueOrException3Exn3Impl<>(e); |
| 74 | } |
| 75 | |
| 76 | private static class ValueOrException3ValueImpl< |
| 77 | E1 extends Exception, E2 extends Exception, E3 extends Exception> |
| 78 | extends ValueOrException3<E1, E2, E3> { |
| 79 | private static final ValueOrException3ValueImpl<Exception, Exception, Exception> NULL = |
| 80 | new ValueOrException3ValueImpl<>(null); |
| 81 | |
| 82 | @Nullable private final SkyValue value; |
| 83 | |
| 84 | ValueOrException3ValueImpl(@Nullable SkyValue value) { |
| 85 | this.value = value; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | @Nullable |
| 90 | public SkyValue get() { |
| 91 | return value; |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | @Nullable |
| 96 | public Exception getException() { |
| 97 | return null; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | @Nullable |
| 102 | public SkyValue getValue() { |
| 103 | return value; |
| 104 | } |
| 105 | |
| 106 | @SuppressWarnings("unchecked") |
| 107 | static <E1 extends Exception, E2 extends Exception, E3 extends Exception> |
| 108 | ValueOrException3ValueImpl<E1, E2, E3> ofNullValue() { |
| 109 | return (ValueOrException3ValueImpl<E1, E2, E3>) NULL; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | private static class ValueOrException3Exn1Impl< |
| 114 | E1 extends Exception, E2 extends Exception, E3 extends Exception> |
| 115 | extends ValueOrException3<E1, E2, E3> { |
| 116 | private final E1 e; |
| 117 | |
| 118 | private ValueOrException3Exn1Impl(E1 e) { |
| 119 | this.e = e; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public SkyValue get() throws E1 { |
| 124 | throw e; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public Exception getException() { |
| 129 | return e; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | @Nullable |
| 134 | public SkyValue getValue() { |
| 135 | return null; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | private static class ValueOrException3Exn2Impl< |
| 140 | E1 extends Exception, E2 extends Exception, E3 extends Exception> |
| 141 | extends ValueOrException3<E1, E2, E3> { |
| 142 | private final E2 e; |
| 143 | |
| 144 | private ValueOrException3Exn2Impl(E2 e) { |
| 145 | this.e = e; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public SkyValue get() throws E2 { |
| 150 | throw e; |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public Exception getException() { |
| 155 | return e; |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | @Nullable |
| 160 | public SkyValue getValue() { |
| 161 | return null; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | private static class ValueOrException3Exn3Impl< |
| 166 | E1 extends Exception, E2 extends Exception, E3 extends Exception> |
| 167 | extends ValueOrException3<E1, E2, E3> { |
| 168 | private final E3 e; |
| 169 | |
| 170 | private ValueOrException3Exn3Impl(E3 e) { |
| 171 | this.e = e; |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public SkyValue get() throws E3 { |
| 176 | throw e; |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public Exception getException() { |
| 181 | return e; |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | @Nullable |
| 186 | public SkyValue getValue() { |
| 187 | return null; |
| 188 | } |
| 189 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 190 | } |