All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.salesforce.datacloud.jdbc.util.Result Maven / Gradle / Ivy

There is a newer version: 0.22.0
Show newest version
/*
 * Copyright (c) 2024, Salesforce, Inc.
 *
 * 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.salesforce.datacloud.jdbc.util;

import java.util.Optional;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NonNull;

public abstract class Result {
    private Result() {}

    public static  Result of(@NonNull ThrowingSupplier supplier) {
        try {
            return new Success<>(supplier.get());
        } catch (Throwable t) {
            return new Failure<>(t);
        }
    }

    abstract Optional get();

    abstract Optional getError();

    @Getter
    @AllArgsConstructor
    public static class Success extends Result {
        private final T value;

        @Override
        Optional get() {
            return Optional.ofNullable(value);
        }

        @Override
        Optional getError() {
            return Optional.empty();
        }
    }

    @Getter
    @AllArgsConstructor
    public static class Failure extends Result {
        private final Throwable error;

        @Override
        Optional get() {
            return Optional.empty();
        }

        @Override
        Optional getError() {
            return Optional.ofNullable(error);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy