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

com.github.lespaul361.commons.commonroutines.utilities.DefaultComparators Maven / Gradle / Ivy

There is a newer version: 1.6.0
Show newest version
/*
 * Copyright (C) 2019 Charles Hamilton
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
package com.github.lespaul361.commons.commonroutines.utilities;

import java.util.Comparator;

/**
 * 
 * @author Charles Hamilton
 */
public class DefaultComparators {

    /**
     * Gets a String Comparator
     * 
     * @param isCaseSensative
     *            if case is used to determine sorting
     * @return a String Comparator
     */
    public static Comparator getDefaultStringComparator(boolean isCaseSensative) {
        class c implements Comparator {

            private final boolean checkCase;

            public c(boolean caseSensative) {
                checkCase = caseSensative;
            }

            @Override
            public int compare(String obj1, String obj2) {
                if (obj1 == null) {
                    return -1;
                }
                if (obj2 == null) {
                    return 1;
                }
                if (!checkCase) {
                    return obj1.compareTo(obj2);
                }
                return obj1.compareToIgnoreCase(obj2);
            }

        }
        return new c(isCaseSensative);
    }

    /**
     * Gets a double Comparator
     * 
     * @return a double Comparator
     */
    public static Comparator getDefaultDoubleComparator() {
        class DoubleComparator implements Comparator {

            @Override
            public int compare(Double d1, Double d2) {
                return Double.compare(d1, d2);
            }

        }
        return new DoubleComparator();
    }

    /**
     * Gets a int Comparator
     * 
     * @return a int Comparator
     */
    public static Comparator getDefaultIntegerComparator() {
        class IntegerComparator implements Comparator {

            @Override
            public int compare(Integer i1, Integer i2) {
                return Integer.compare(i1, i2);
            }

        }
        return new IntegerComparator();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy