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

com.nls.util.DateTimes Maven / Gradle / Ivy

There is a newer version: 1.0.14
Show newest version
package com.nls.util;

import org.joda.time.DateTime;

import java.util.Comparator;
import java.util.function.Function;

public final class DateTimes {
    private DateTimes() {

    }

    public static DateTime max(DateTime left, DateTime right) {
        if (left == null) {
            return right;
        }
        if (right == null) {
            return left;
        }
        return left.isAfter(right) ? left : right;
    }

    public static DateTime min(DateTime left, DateTime right) {
        if (left == null) {
            return left;
        }
        if (right == null) {
            return right;
        }
        return left.isBefore(right) ? left : right;
    }

    public static Comparator comparator(boolean nullsFirst) {
        return comparator(Function.identity(), nullsFirst);
    }

    public static  Comparator comparator(Function mapper, boolean nullsFirst) {
        return (Comparator) (e1, e2) -> {
            DateTime o1 = mapper.apply(e1);
            DateTime o2 = mapper.apply(e2);
            if (o1 == null && o2 == null) {
                return 0;
            }
            if (o1 == null) {
                return nullsFirst ? -1 : 1;
            }
            if (o2 == null) {
                return nullsFirst ? 1 : -1;
            }
            return o1.compareTo(o2);
        };
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy