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

com.github.leeonky.util.function.IfFactory Maven / Gradle / Ivy

The newest version!
package com.github.leeonky.util.function;


import java.util.Optional;
import java.util.function.Predicate;

import static java.util.Optional.of;

public interface IfFactory {

    boolean matches(CONDITION condition);

    default  OptionalFactory then(VALUE value) {
        return condition -> matches(condition) ? of(value) : Optional.empty();
    }

    interface ElseIfFactory {
        boolean matches(CONDITION condition);

        Optional previousIf(CONDITION condition);

        default OptionalFactory then(VALUE value) {
            return condition -> {
                Optional previous = previousIf(condition);
                return previous.isPresent() ? previous : matches(condition) ? of(value) : Optional.empty();
            };
        }
    }

    interface OptionalFactory {
        Optional get(CONDITION condition);

        default Factory orElse(VALUE value) {
            return condition -> get(condition).orElse(value);
        }

        default ElseIfFactory when(Predicate predicate) {
            return new ElseIfFactory() {
                @Override
                public boolean matches(CONDITION condition) {
                    return predicate.test(condition);
                }

                @Override
                public Optional previousIf(CONDITION condition) {
                    return get(condition);
                }
            };
        }
    }

    interface Factory {
        VALUE get(CONDITION condition);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy