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

com.landawn.abacus.util.Comparators Maven / Gradle / Ivy

Go to download

A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.

There is a newer version: 2.1.12
Show newest version
/*
 * Copyright (c) 2017, Haiyang Li.
 *
 * 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 com.landawn.abacus.util;

import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map;

import com.landawn.abacus.util.function.Function;
import com.landawn.abacus.util.function.ToDoubleFunction;
import com.landawn.abacus.util.function.ToIntFunction;
import com.landawn.abacus.util.function.ToLongFunction;

/**
 *
 * Factory utility class for Comparator.
 *
 * @author haiyangl
 *
 */
public abstract class Comparators {

    /** The Constant NULL_FIRST_COMPARATOR. */
    @SuppressWarnings("rawtypes")
    static final Comparator NULL_FIRST_COMPARATOR = new Comparator() {
        @Override
        public int compare(final Comparable a, final Comparable b) {
            return a == null ? (b == null ? 0 : -1) : (b == null ? 1 : a.compareTo(b));
        }
    };

    /** The Constant NULL_LAST_COMPARATOR. */
    @SuppressWarnings("rawtypes")
    static final Comparator NULL_LAST_COMPARATOR = new Comparator() {
        @Override
        public int compare(final Comparable a, final Comparable b) {
            return a == null ? (b == null ? 0 : 1) : (b == null ? -1 : a.compareTo(b));
        }
    };

    /** The Constant NATURAL_ORDER. */
    @SuppressWarnings("rawtypes")
    static final Comparator NATURAL_ORDER = NULL_FIRST_COMPARATOR;

    /** The Constant REVERSED_ORDER. */
    @SuppressWarnings("rawtypes")
    static final Comparator REVERSED_ORDER = Collections.reverseOrder(NATURAL_ORDER);

    /** The Constant COMPARING_IGNORE_CASE. */
    static final Comparator COMPARING_IGNORE_CASE = new Comparator() {
        @Override
        public int compare(String a, String b) {
            return N.compareIgnoreCase(a, b);
        }
    };

    /** The Constant COMPARING_BY_LENGTH. */
    static final Comparator COMPARING_BY_LENGTH = new Comparator() {
        @Override
        public int compare(CharSequence a, CharSequence b) {
            return (a == null ? 0 : a.length()) - (b == null ? 0 : b.length());
        }
    };

    /** The Constant COMPARING_BY_SIZE. */
    @SuppressWarnings("rawtypes")
    static final Comparator COMPARING_BY_SIZE = new Comparator() {
        @Override
        public int compare(Collection a, Collection b) {
            return (a == null ? 0 : a.size()) - (b == null ? 0 : b.size());
        }
    };

    /** The Constant COMPARING_BY_SIZEE. */
    @SuppressWarnings("rawtypes")
    static final Comparator COMPARING_BY_SIZEE = new Comparator() {
        @Override
        public int compare(Map a, Map b) {
            return (a == null ? 0 : a.size()) - (b == null ? 0 : b.size());
        }
    };

    /**
     * Instantiates a new comparators.
     */
    Comparators() {
        // Singleton
    }

    /**
     *
     * @param 
     * @return
     */
    @SuppressWarnings("unchecked")
    public static  Comparator naturalOrder() {
        return NATURAL_ORDER;
    }

    /**
     *
     * @param 
     * @return
     */
    public static  Comparator reversedOrder() {
        return REVERSED_ORDER;
    }

    /**
     *
     * @param 
     * @param cmp
     * @return
     */
    public static  Comparator reversedOrder(final Comparator cmp) {
        if (cmp == null || cmp == NATURAL_ORDER) {
            return REVERSED_ORDER;
        } else if (cmp == REVERSED_ORDER) {
            return NATURAL_ORDER;
        }

        return Collections.reverseOrder(cmp);
    }

    /**
     *
     * @param 
     * @return
     */
    public static  Comparator nullsFirst() {
        return NULL_FIRST_COMPARATOR;
    }

    /**
     *
     * @param 
     * @param cmp
     * @return
     */
    public static  Comparator nullsFirst(final Comparator cmp) {
        if (cmp == null || cmp == NULL_FIRST_COMPARATOR) {
            return NULL_FIRST_COMPARATOR;
        }

        return new Comparator() {
            @Override
            public int compare(T a, T b) {
                return a == null ? (b == null ? 0 : -1) : (b == null ? 1 : cmp.compare(a, b));
            }
        };
    }

    /**
     *
     * @param 
     * @return
     */
    public static  Comparator nullsLast() {
        return NULL_LAST_COMPARATOR;
    }

    /**
     *
     * @param 
     * @param cmp
     * @return
     */
    public static  Comparator nullsLast(final Comparator cmp) {
        if (cmp == null || cmp == NULL_LAST_COMPARATOR) {
            return NULL_LAST_COMPARATOR;
        }

        return new Comparator() {
            @Override
            public int compare(T a, T b) {
                return a == null ? (b == null ? 0 : 1) : (b == null ? -1 : cmp.compare(a, b));
            }
        };
    }

    /**
     *
     * @param 
     * @param 
     * @param keyMapper
     * @return
     */
    public static > Comparator comparingBy(final Function keyMapper) {
        N.checkArgNotNull(keyMapper);

        return new Comparator() {
            @Override
            public int compare(T a, T b) {
                return N.compare(keyMapper.apply(a), keyMapper.apply(b));
            }
        };
    }

    /**
     * Reversed comparing by.
     *
     * @param 
     * @param 
     * @param keyMapper
     * @return
     */
    public static > Comparator reversedComparingBy(final Function keyMapper) {
        N.checkArgNotNull(keyMapper);

        return new Comparator() {
            @Override
            public int compare(T a, T b) {
                return N.compare(keyMapper.apply(b), keyMapper.apply(a));
            }
        };
    }

    /**
     *
     * @param 
     * @param 
     * @param keyMapper
     * @param keyComparator
     * @return
     */
    public static  Comparator comparingBy(final Function keyMapper, final Comparator keyComparator) {
        N.checkArgNotNull(keyMapper);
        N.checkArgNotNull(keyComparator);

        return new Comparator() {
            @Override
            public int compare(T a, T b) {
                return keyComparator.compare(keyMapper.apply(a), keyMapper.apply(b));
            }
        };
    }

    /**
     *
     * @param 
     * @param keyMapper
     * @return
     */
    public static  Comparator comparingInt(final ToIntFunction keyMapper) {
        N.checkArgNotNull(keyMapper);

        return new Comparator() {
            @Override
            public int compare(T a, T b) {
                return Integer.compare(keyMapper.applyAsInt(a), keyMapper.applyAsInt(b));
            }
        };
    }

    /**
     *
     * @param 
     * @param keyMapper
     * @return
     */
    public static  Comparator comparingLong(final ToLongFunction keyMapper) {
        N.checkArgNotNull(keyMapper);

        return new Comparator() {
            @Override
            public int compare(T a, T b) {
                return Long.compare(keyMapper.applyAsLong(a), keyMapper.applyAsLong(b));
            }
        };
    }

    /**
     *
     * @param 
     * @param keyMapper
     * @return
     */
    public static  Comparator comparingDouble(final ToDoubleFunction keyMapper) {
        N.checkArgNotNull(keyMapper);

        return new Comparator() {
            @Override
            public int compare(T a, T b) {
                return Double.compare(keyMapper.applyAsDouble(a), keyMapper.applyAsDouble(b));
            }
        };
    }

    /**
     * Comparing ignore case.
     *
     * @return
     */
    public static Comparator comparingIgnoreCase() {
        return COMPARING_IGNORE_CASE;
    }

    /**
     * Comparing ignore case.
     *
     * @param 
     * @param keyMapper
     * @return
     */
    public static  Comparator comparingIgnoreCase(final Function keyMapper) {
        N.checkArgNotNull(keyMapper);

        return new Comparator() {
            @Override
            public int compare(T a, T b) {
                return N.compareIgnoreCase(keyMapper.apply(a), keyMapper.apply(b));
            }
        };
    }

    /**
     * Comparing by key.
     *
     * @param  the key type
     * @param  the value type
     * @return
     */
    public static , V> Comparator> comparingByKey() {
        return new Comparator>() {
            @Override
            public int compare(Map.Entry a, Map.Entry b) {
                return N.compare(a.getKey(), b.getKey());
            }
        };
    }

    /**
     * Reversed comparing by key.
     *
     * @param  the key type
     * @param  the value type
     * @return
     */
    public static , V> Comparator> reversedComparingByKey() {
        return new Comparator>() {
            @Override
            public int compare(Map.Entry a, Map.Entry b) {
                return N.compare(b.getKey(), a.getKey());
            }
        };
    }

    /**
     * Comparing by value.
     *
     * @param  the key type
     * @param  the value type
     * @return
     */
    public static > Comparator> comparingByValue() {
        return new Comparator>() {
            @Override
            public int compare(Map.Entry a, Map.Entry b) {
                return N.compare(a.getValue(), b.getValue());
            }
        };
    }

    /**
     * Reversed comparing by value.
     *
     * @param  the key type
     * @param  the value type
     * @return
     */
    public static > Comparator> reversedComparingByValue() {
        return new Comparator>() {
            @Override
            public int compare(Map.Entry a, Map.Entry b) {
                return N.compare(b.getValue(), a.getValue());
            }
        };
    }

    /**
     * Comparing by key.
     *
     * @param  the key type
     * @param  the value type
     * @param cmp
     * @return
     */
    public static  Comparator> comparingByKey(final Comparator cmp) {
        N.checkArgNotNull(cmp);

        return new Comparator>() {
            @Override
            public int compare(Map.Entry a, Map.Entry b) {
                return cmp.compare(a.getKey(), b.getKey());
            }
        };
    }

    /**
     * Comparing by value.
     *
     * @param  the key type
     * @param  the value type
     * @param cmp
     * @return
     */
    public static  Comparator> comparingByValue(final Comparator cmp) {
        N.checkArgNotNull(cmp);

        return new Comparator>() {
            @Override
            public int compare(Map.Entry a, Map.Entry b) {
                return cmp.compare(a.getValue(), b.getValue());
            }
        };
    }

    /**
     * Reversed comparing by key.
     *
     * @param  the key type
     * @param  the value type
     * @param cmp
     * @return
     */
    @Deprecated
    public static  Comparator> reversedComparingByKey(final Comparator cmp) {
        N.checkArgNotNull(cmp);

        return new Comparator>() {
            @Override
            public int compare(Map.Entry a, Map.Entry b) {
                return cmp.compare(b.getKey(), a.getKey());
            }
        };
    }

    /**
     * Reversed comparing by value.
     *
     * @param  the key type
     * @param  the value type
     * @param cmp
     * @return
     */
    @Deprecated
    public static  Comparator> reversedComparingByValue(final Comparator cmp) {
        N.checkArgNotNull(cmp);

        return new Comparator>() {
            @Override
            public int compare(Map.Entry a, Map.Entry b) {
                return cmp.compare(b.getValue(), a.getValue());
            }
        };
    }

    /**
     * Comparing by length.
     *
     * @param 
     * @return
     */
    public static  Comparator comparingByLength() {
        return (Comparator) COMPARING_BY_LENGTH;
    }

    /**
     * Comparing by size.
     *
     * @param 
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static  Comparator comparingBySize() {
        return (Comparator) COMPARING_BY_SIZE;
    }

    /**
     * Comparing by sizee.
     *
     * @param 
     * @return
     */
    @SuppressWarnings("rawtypes")
    static  Comparator comparingBySizee() {
        return (Comparator) COMPARING_BY_SIZEE;
    }

}