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

io.github.pellse.util.ObjectUtils Maven / Gradle / Ivy

Go to download

Small library allowing to efficiently assemble entities from querying/merging external datasources or aggregating microservices

There is a newer version: 0.7.9
Show newest version
/*
 * Copyright 2024 Sebastien Pelletier
 *
 * 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.pellse.util;

import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

import static java.util.Optional.ofNullable;

public interface ObjectUtils {

    static  boolean isSafeEqual(T t1, T t2, Function propertyExtractor) {
        return isSafeEqual(t1, propertyExtractor, t2, propertyExtractor);
    }

    static  boolean isSafeEqual(T1 t1,
                                           Function propertyExtractor1,
                                           T2 t2,
                                           Function propertyExtractor2) {

        return ofNullable(t1)
                .map(propertyExtractor1)
                .equals(ofNullable(t2)
                        .map(propertyExtractor2));
    }

    static  void ifNotNull(T value, Consumer codeBlock) {
        runIf(value, Objects::nonNull, codeBlock);
    }

    @SafeVarargs
    static  T also(T value, Consumer... codeBlocks) {

        if (value == null) {
            return null;
        }

        for (var codeBlock : codeBlocks) {
            codeBlock.accept(value);
        }
        return value;
    }

    @SafeVarargs
    static  void run(T value, Consumer... codeBlocks) {
        also(value, codeBlocks);
    }

    static  void runIf(T value, Predicate predicate, Consumer codeBlock) {

        if (value != null && predicate.test(value)) {
            codeBlock.accept(value);
        }
    }

    static  Consumer doNothing() {
        return __ -> {
        };
    }

    static  Consumer run(Runnable runnable) {
        return __ -> runnable.run();
    }

    static  Function get(R value) {
        return __ -> value;
    }

    static  Function get(Supplier supplier) {
        return __ -> supplier.get();
    }

    static  R then(T value, Function mappingFunction) {
        return mappingFunction.apply(value);
    }

    static  Predicate or(Predicate predicate1, Predicate predicate2) {
        return nonNull(predicate1).or(nonNull(predicate2));
    }

    static  Predicate nonNull(Predicate predicate) {
        return predicate != null ? predicate : t -> true;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy