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 ValueOrException<E extends Exception> extends ValueOrUntypedException { |
| 20 | |
| 21 | /** Gets the stored value. Throws an exception if one was thrown when computing this value. */ |
| 22 | @Nullable |
| 23 | public abstract SkyValue get() throws E; |
shreyax | c10ad74 | 2018-04-10 12:44:40 -0700 | [diff] [blame] | 24 | |
| 25 | static <E extends Exception> ValueOrException<E> fromUntypedException( |
| 26 | ValueOrUntypedException voe, Class<E> exceptionClass) { |
| 27 | SkyValue value = voe.getValue(); |
| 28 | if (value != null) { |
| 29 | return ofValue(value); |
| 30 | } |
| 31 | Exception e = voe.getException(); |
| 32 | if (e != null) { |
| 33 | if (exceptionClass.isInstance(e)) { |
| 34 | return ofExn1(exceptionClass.cast(e)); |
| 35 | } |
| 36 | } |
| 37 | return ofNullValue(); |
| 38 | } |
| 39 | |
| 40 | private static <E extends Exception> ValueOrException<E> ofExn1(E e) { |
| 41 | return new ValueOrExceptionExnImpl<>(e); |
| 42 | } |
| 43 | |
| 44 | private static <E extends Exception> ValueOrException<E> ofNullValue() { |
| 45 | return ValueOrExceptionValueImpl.ofNullValue(); |
| 46 | } |
| 47 | |
| 48 | private static <E extends Exception> ValueOrException<E> ofValue(SkyValue value) { |
| 49 | return new ValueOrExceptionValueImpl<>(value); |
| 50 | } |
| 51 | |
| 52 | private static class ValueOrExceptionValueImpl<E extends Exception> extends ValueOrException<E> { |
| 53 | private static final ValueOrExceptionValueImpl<Exception> NULL = |
| 54 | new ValueOrExceptionValueImpl<>(null); |
| 55 | |
| 56 | @Nullable private final SkyValue value; |
| 57 | |
| 58 | private ValueOrExceptionValueImpl(@Nullable SkyValue value) { |
| 59 | this.value = value; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | @Nullable |
| 64 | public SkyValue get() { |
| 65 | return value; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | @Nullable |
| 70 | public SkyValue getValue() { |
| 71 | return value; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | @Nullable |
| 76 | public Exception getException() { |
| 77 | return null; |
| 78 | } |
| 79 | |
janakr | 02be4c5 | 2019-03-17 20:33:11 -0700 | [diff] [blame] | 80 | @Override |
| 81 | public String toString() { |
| 82 | return "ValueOrExceptionValueImpl:" + value; |
| 83 | } |
| 84 | |
shreyax | c10ad74 | 2018-04-10 12:44:40 -0700 | [diff] [blame] | 85 | @SuppressWarnings("unchecked") |
| 86 | static <E extends Exception> ValueOrExceptionValueImpl<E> ofNullValue() { |
| 87 | return (ValueOrExceptionValueImpl<E>) NULL; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | private static class ValueOrExceptionExnImpl<E extends Exception> extends ValueOrException<E> { |
| 92 | private final E e; |
| 93 | |
| 94 | private ValueOrExceptionExnImpl(E e) { |
| 95 | this.e = e; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public SkyValue get() throws E { |
| 100 | throw e; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | @Nullable |
| 105 | public SkyValue getValue() { |
| 106 | return null; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public Exception getException() { |
| 111 | return e; |
| 112 | } |
| 113 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 114 | } |