<commit_before>/*
 * Copyright 2016 <NAME> <<EMAIL>>.
 *
 * This file is part of business-flows.
 *
 * 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 io.github.theangrydev.businessflows;

import java.util.Optional;
import java.util.function.Function;

public class HappyPath<Sad, Happy> extends BusinessFlow<Sad, Happy, Happy> {

    HappyPath(BusinessCase<Sad, Happy> businessCase) {
        super(businessCase);
    }

    public static <Sad, Happy> HappyPath<Sad, Happy> happyAttempt(HappyAttempt<Happy> happyAttempt) {
        return happyAttempt(happyAttempt.andThen(HappyPath::happyPath), HappyPath::technicalFailure);
    }

    public static <Result> Result happyAttempt(HappyAttempt<Result> happyAttempt, Function<Exception, Result> failureHandler) {
        try {
            return happyAttempt.happy();
        } catch (Exception technicalFailure) {
            return failureHandler.apply(technicalFailure);
        }
    }

    public static <Sad, Happy> HappyPath<Sad, Happy> happyPath(Happy happy) {
        return new HappyPath<>(new HappyCase<>(happy));
    }

    public <NewHappy> HappyPath<Sad, NewHappy> then(Mapping<Happy, BusinessFlow<Sad, NewHappy, ?>> action) {
        return join(HappyPath::sadPath, happy -> {
            try {
                return action.map(happy).ifHappy();
            } catch (Exception technicalFailure) {
                return HappyPath.technicalFailure(technicalFailure);
            }
        }, HappyPath::technicalFailure);
    }

    public <NewHappy> HappyPath<Sad, NewHappy> map(Mapping<Happy, NewHappy> mapping) {
        return then(mapping.andThen(HappyPath::happyPath));
    }

    public HappyPath<Sad, Happy> attempt(ActionThatMightFail<Sad, Happy> actionThatMightFail) {
        return then(happy -> actionThatMightFail.attempt(happy).map(HappyPath::<Sad, Happy>sadPath).orElse(HappyPath.happyPath(happy)));
    }

    public HappyPath<Sad, Happy> peek(Peek<Happy> peek) {
        return then(happy -> {
            peek.peek(happy);
            return this;
        });
    }

    private static <Sad, Happy> HappyPath<Sad, Happy> sadPath(Sad sad) {
        return new HappyPath<>(new SadCase<>(sad));
    }

    private static <Sad, Happy> HappyPath<Sad, Happy> technicalFailure(Exception technicalFailure) {
        return new HappyPath<>(new TechnicalFailureCase<>(technicalFailure));
    }

    @Override
    Function<BusinessCase<Sad, Happy>, Optional<Happy>> bias() {
        return BusinessCase::happyOptional;
    }
}<commit_msg>Refactoring<commit_after>/*
 * Copyright 2016 <NAME> <<EMAIL>>.
 *
 * This file is part of business-flows.
 *
 * 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 io.github.theangrydev.businessflows;

import java.util.Optional;
import java.util.function.Function;

public class HappyPath<Sad, Happy> extends BusinessFlow<Sad, Happy, Happy> {

    HappyPath(BusinessCase<Sad, Happy> businessCase) {
        super(businessCase);
    }

    public static <Sad, Happy> HappyPath<Sad, Happy> happyAttempt(HappyAttempt<Happy> happyAttempt) {
        return happyAttempt(happyAttempt.andThen(HappyPath::happyPath), HappyPath::technicalFailure);
    }

    public static <Result> Result happyAttempt(HappyAttempt<Result> happyAttempt, Function<Exception, Result> failureHandler) {
        try {
            return happyAttempt.happy();
        } catch (Exception technicalFailure) {
            return failureHandler.apply(technicalFailure);
        }
    }

    public static <Sad, Happy> HappyPath<Sad, Happy> happyPath(Happy happy) {
        return new HappyPath<>(new HappyCase<>(happy));
    }

    public <NewHappy> HappyPath<Sad, NewHappy> then(Mapping<Happy, BusinessFlow<Sad, NewHappy, ?>> action) {
        return join(HappyPath::sadPath, happy -> {
            try {
                return action.map(happy).ifHappy();
            } catch (Exception technicalFailure) {
                return HappyPath.technicalFailure(technicalFailure);
            }
        }, HappyPath::technicalFailure);
    }

    public <NewHappy> HappyPath<Sad, NewHappy> map(Mapping<Happy, NewHappy> mapping) {
        return then(mapping.andThen(HappyPath::happyPath));
    }

    public HappyPath<Sad, Happy> attempt(ActionThatMightFail<Sad, Happy> actionThatMightFail) {
        return then(happy -> actionThatMightFail.attempt(happy).map(HappyPath::<Sad, Happy>sadPath).orElse(HappyPath.happyPath(happy)));
    }

    public HappyPath<Sad, Happy> peek(Peek<Happy> peek) {
        return then(happy -> {
            peek.peek(happy);
            return this;
        });
    }

    private static <Sad, Happy> HappyPath<Sad, Happy> sadPath(Sad sad) {
        return new HappyPath<>(new SadCase<>(sad));
    }

    private static <Sad, Happy> HappyPath<Sad, Happy> technicalFailure(Exception technicalFailure) {
        return new HappyPath<>(new TechnicalFailureCase<>(technicalFailure));
    }

    @Override
    protected Function<BusinessCase<Sad, Happy>, Optional<Happy>> bias() {
        return BusinessCase::happyOptional;
    }
}
{'fraction_non_alphanumeric': 0.10806345009914078, 'fraction_numerical': 0.0026437541308658294, 'mean_word_length': 3.4441997063142438, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2369514', 'n_tokens_mistral': 1788, 'n_tokens_neox': 1616, 'n_words': 455}