@Test public void throwsOnTwoEmptyOptionals() { assertThatThrownBy(() -> OptionalResolver.resolve(EMPTY_OPTIONAL, EMPTY_OPTIONAL)) .isInstanceOf(IllegalArgumentException.class); }
@SuppressWarnings("OptionalUsedAsFieldOrParameterType") public static <T> T resolve(Optional<T> optional1, Optional<T> optional2) { Set<T> values = Stream.of(optional1, optional2) .filter(Objects::nonNull) .filter(Optional::isPresent) .map(Optional::get) .collect(Collectors.toSet()); com.palantir.logsafe.Preconditions.checkArgument(values.size() >= 1, "All Optionals provided were empty, couldn't determine a value."); Preconditions.checkArgument(values.size() <= 1, "Contradictory values %s found, expected a single common value", values); return Iterables.getOnlyElement(values); }
OptionalResolver { @SuppressWarnings("OptionalUsedAsFieldOrParameterType") public static <T> T resolve(Optional<T> optional1, Optional<T> optional2) { Set<T> values = Stream.of(optional1, optional2) .filter(Objects::nonNull) .filter(Optional::isPresent) .map(Optional::get) .collect(Collectors.toSet()); com.palantir.logsafe.Preconditions.checkArgument(values.size() >= 1, "All Optionals provided were empty, couldn't determine a value."); Preconditions.checkArgument(values.size() <= 1, "Contradictory values %s found, expected a single common value", values); return Iterables.getOnlyElement(values); } }
OptionalResolver { @SuppressWarnings("OptionalUsedAsFieldOrParameterType") public static <T> T resolve(Optional<T> optional1, Optional<T> optional2) { Set<T> values = Stream.of(optional1, optional2) .filter(Objects::nonNull) .filter(Optional::isPresent) .map(Optional::get) .collect(Collectors.toSet()); com.palantir.logsafe.Preconditions.checkArgument(values.size() >= 1, "All Optionals provided were empty, couldn't determine a value."); Preconditions.checkArgument(values.size() <= 1, "Contradictory values %s found, expected a single common value", values); return Iterables.getOnlyElement(values); } private  OptionalResolver(); }
OptionalResolver { @SuppressWarnings("OptionalUsedAsFieldOrParameterType") public static <T> T resolve(Optional<T> optional1, Optional<T> optional2) { Set<T> values = Stream.of(optional1, optional2) .filter(Objects::nonNull) .filter(Optional::isPresent) .map(Optional::get) .collect(Collectors.toSet()); com.palantir.logsafe.Preconditions.checkArgument(values.size() >= 1, "All Optionals provided were empty, couldn't determine a value."); Preconditions.checkArgument(values.size() <= 1, "Contradictory values %s found, expected a single common value", values); return Iterables.getOnlyElement(values); } private  OptionalResolver(); @SuppressWarnings("OptionalUsedAsFieldOrParameterType") // Used to process existing configuration files. static T resolve(Optional<T> optional1, Optional<T> optional2); }
OptionalResolver { @SuppressWarnings("OptionalUsedAsFieldOrParameterType") public static <T> T resolve(Optional<T> optional1, Optional<T> optional2) { Set<T> values = Stream.of(optional1, optional2) .filter(Objects::nonNull) .filter(Optional::isPresent) .map(Optional::get) .collect(Collectors.toSet()); com.palantir.logsafe.Preconditions.checkArgument(values.size() >= 1, "All Optionals provided were empty, couldn't determine a value."); Preconditions.checkArgument(values.size() <= 1, "Contradictory values %s found, expected a single common value", values); return Iterables.getOnlyElement(values); } private  OptionalResolver(); @SuppressWarnings("OptionalUsedAsFieldOrParameterType") // Used to process existing configuration files. static T resolve(Optional<T> optional1, Optional<T> optional2);  }