Jake Wharton | 3d656f7 | 2019-12-12 23:11:15 -0500 | [diff] [blame] | 1 | // Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file |
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | package backport; |
| 6 | |
| 7 | import java.util.NoSuchElementException; |
| 8 | import java.util.OptionalInt; |
| 9 | |
| 10 | public final class OptionalIntBackportJava10Main { |
| 11 | |
| 12 | public static void main(String[] args) { |
| 13 | testOrElseThrow(); |
| 14 | } |
| 15 | |
| 16 | private static void testOrElseThrow() { |
| 17 | OptionalInt present = OptionalInt.of(2); |
| 18 | assertEquals(2, present.orElseThrow()); |
| 19 | |
| 20 | OptionalInt absent = OptionalInt.empty(); |
| 21 | try { |
| 22 | throw new AssertionError(absent.orElseThrow()); |
| 23 | } catch (NoSuchElementException expected) { |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | private static void assertEquals(Object expected, Object actual) { |
| 28 | if (expected != actual && !expected.equals(actual)) { |
| 29 | throw new AssertionError("Expected <" + expected + "> but was <" + actual + ">"); |
| 30 | } |
| 31 | } |
| 32 | } |