com.palantir.util.OptionalResolver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of atlasdb-commons Show documentation
Show all versions of atlasdb-commons Show documentation
Palantir open source project
/*
* (c) Copyright 2018 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.palantir.util;
import com.palantir.logsafe.UnsafeArg;
import com.palantir.logsafe.exceptions.SafeIllegalArgumentException;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public final class OptionalResolver {
private OptionalResolver() {
// Utility class
}
/**
* Returns a single value corresponding to the value that is present in one or more of the Optionals provided.
* This method throws if no Optionals provided contain values, or if the Optionals provided contain multiple
* values that are not equal.
* Null Optionals are considered not-present.
*/
@SuppressWarnings("OptionalUsedAsFieldOrParameterType") // Used to process existing configuration files.
public static T resolve(Optional optional1, Optional optional2) {
Set values = Stream.of(optional1, optional2)
.filter(Objects::nonNull)
.flatMap(Optional::stream)
.collect(Collectors.toSet());
if (values.size() == 1) {
return values.iterator().next();
} else if (values.size() > 1) {
throw new SafeIllegalArgumentException(
"Contradictory values found, expected a single common value", UnsafeArg.of("values", values));
} else {
throw new SafeIllegalArgumentException("All Optionals provided were empty, couldn't determine a value.");
}
}
}