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

de.quantummaid.mapmaid.builder.detection.DetectionResult Maven / Gradle / Ivy

There is a newer version: 0.10.19
Show newest version
/*
 * Copyright (c) 2020 Richard Hauswald - https://quantummaid.de/.
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 de.quantummaid.mapmaid.builder.detection;

import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;

import static de.quantummaid.mapmaid.collections.Collection.smallList;
import static de.quantummaid.mapmaid.shared.validators.NotNullValidator.validateNotNull;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.joining;

@ToString
@EqualsAndHashCode
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public final class DetectionResult {
    private final T result;
    private final List reasonsForFailure;

    public static  DetectionResult combine(final DetectionResult a,
                                                       final DetectionResult b,
                                                       final BiFunction combinator) {
        if (!a.isFailure() && !b.isFailure()) {
            final C combination = combinator.apply(a.result, b.result);
            return success(combination);
        }
        final List combinedReasons = smallList();
        combinedReasons.addAll(a.reasonsForFailure);
        combinedReasons.addAll(b.reasonsForFailure);
        return failure(combinedReasons);
    }

    public static  DetectionResult success(final T result) {
        validateNotNull(result, "result");
        return new DetectionResult<>(result, emptyList());
    }

    public static  DetectionResult failure(final String reasonForFailure) {
        validateNotNull(reasonForFailure, "reasonForFailure");
        return failure(singletonList(reasonForFailure));
    }

    public static  DetectionResult failure(final List reasonsForFailure) {
        validateNotNull(reasonsForFailure, "reasonsForFailure");
        return new DetectionResult<>(null, reasonsForFailure);
    }

    public static  DetectionResult followUpFailure(final DetectionResult... detectionResults) {
        final List combinedReasons = smallList();
        for (final DetectionResult result : detectionResults) {
            if (!result.isFailure()) {
                throw new IllegalArgumentException("Can only follow up on failures");
            }
            combinedReasons.addAll(result.reasonsForFailure);
        }
        return failure(combinedReasons);
    }

    public void ifSuccess(final Consumer consumer) {
        if (isSuccess()) {
            consumer.accept(this.result);
        }
    }

    public boolean isFailure() {
        return !this.reasonsForFailure.isEmpty();
    }

    public boolean isSuccess() {
        return !isFailure();
    }

    public String reasonForFailure() {
        return this.reasonsForFailure.stream()
                .collect(joining("\n", "[", "]"));
    }

    public T result() {
        return this.result;
    }

    @SuppressWarnings("unchecked")
    public  DetectionResult map(final Function mapper) {
        if (isFailure()) {
            return (DetectionResult) this;
        }
        final X mapped = mapper.apply(this.result);
        return success(mapped);
    }

    @SuppressWarnings("unchecked")
    public  DetectionResult flatMap(final Function> mapper) {
        if (isFailure()) {
            return (DetectionResult) this;
        }
        return mapper.apply(this.result);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy