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

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

There is a newer version: 1.8.1
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.Collections;
import java.util.Comparator;
import java.util.Map;

import com.landawn.abacus.util.function.Function;
import com.landawn.abacus.util.function.ToBooleanFunction;
import com.landawn.abacus.util.function.ToByteFunction;
import com.landawn.abacus.util.function.ToCharFunction;
import com.landawn.abacus.util.function.ToDoubleFunction;
import com.landawn.abacus.util.function.ToFloatFunction;
import com.landawn.abacus.util.function.ToIntFunction;
import com.landawn.abacus.util.function.ToLongFunction;
import com.landawn.abacus.util.function.ToShortFunction;

/**
 * 
 * Factory utility class for Comparator.
 * 
 * @author haiyangl
 *
 */
public final class Comparators {
    @SuppressWarnings("rawtypes")
    private 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));
        }
    };

    @SuppressWarnings("rawtypes")
    private 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));
        }
    };

    @SuppressWarnings("rawtypes")
    private static final Comparator NATURAL_ORDER = NULL_FIRST_COMPARATOR;

    @SuppressWarnings("rawtypes")
    private static final Comparator REVERSED_ORDER = Collections.reverseOrder(NATURAL_ORDER);

    @SuppressWarnings("rawtypes")
    static final Comparator OBJ_COMPARATOR = NATURAL_ORDER;

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

    private Comparators() {
        // Singleton
    }

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

    public static  Comparator reversedOrder() {
        return REVERSED_ORDER;
    }

    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);
    }

    public static  Comparator nullsFirst() {
        return NULL_FIRST_COMPARATOR;
    }

    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));
            }
        };
    }

    public static  Comparator nullsLast() {
        return NULL_LAST_COMPARATOR;
    }

    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));
            }
        };
    }

    @SuppressWarnings("rawtypes")
    public static  Comparator comparingBy(final Function keyExtractor) {
        N.requireNonNull(keyExtractor);

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

    @SuppressWarnings("rawtypes")
    public static  Comparator reversedComparingBy(final Function keyExtractor) {
        N.requireNonNull(keyExtractor);

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

    public static  Comparator comparingBy(final Function keyExtractor, final Comparator keyComparator) {
        N.requireNonNull(keyExtractor);
        N.requireNonNull(keyComparator);

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

    public static  Comparator comparingBoolean(final ToBooleanFunction keyExtractor) {
        N.requireNonNull(keyExtractor);

        return new Comparator() {
            @Override
            public int compare(T a, T b) {
                return Boolean.compare(keyExtractor.applyAsBoolean(a), keyExtractor.applyAsBoolean(b));
            }
        };
    }

    public static  Comparator comparingChar(final ToCharFunction keyExtractor) {
        N.requireNonNull(keyExtractor);

        return new Comparator() {
            @Override
            public int compare(T a, T b) {
                return Character.compare(keyExtractor.applyAsChar(a), keyExtractor.applyAsChar(b));
            }
        };
    }

    public static  Comparator comparingByte(final ToByteFunction keyExtractor) {
        N.requireNonNull(keyExtractor);

        return new Comparator() {
            @Override
            public int compare(T a, T b) {
                return Byte.compare(keyExtractor.applyAsByte(a), keyExtractor.applyAsByte(b));
            }
        };
    }

    public static  Comparator comparingShort(final ToShortFunction keyExtractor) {
        N.requireNonNull(keyExtractor);

        return new Comparator() {
            @Override
            public int compare(T a, T b) {
                return Short.compare(keyExtractor.applyAsShort(a), keyExtractor.applyAsShort(b));
            }
        };
    }

    public static  Comparator comparingInt(final ToIntFunction keyExtractor) {
        N.requireNonNull(keyExtractor);

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

    public static  Comparator comparingLong(final ToLongFunction keyExtractor) {
        N.requireNonNull(keyExtractor);

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

    public static  Comparator comparingFloat(final ToFloatFunction keyExtractor) {
        N.requireNonNull(keyExtractor);

        return new Comparator() {
            @Override
            public int compare(T a, T b) {
                return Float.compare(keyExtractor.applyAsFloat(a), keyExtractor.applyAsFloat(b));
            }
        };
    }

    public static  Comparator comparingDouble(final ToDoubleFunction keyExtractor) {
        N.requireNonNull(keyExtractor);

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

    public static Comparator comparingIgnoreCase() {
        return COMPARING_IGNORE_CASE;
    }

    @SuppressWarnings("rawtypes")
    public static  Comparator> comparingByKey() {
        return new Comparator>() {
            @Override
            public int compare(Map.Entry a, Map.Entry b) {
                return N.compare(a.getKey(), b.getKey());
            }
        };
    }

    @SuppressWarnings("rawtypes")
    public static  Comparator> reversedComparingByKey() {
        return new Comparator>() {
            @Override
            public int compare(Map.Entry a, Map.Entry b) {
                return N.compare(b.getKey(), a.getKey());
            }
        };
    }

    @SuppressWarnings("rawtypes")
    public static  Comparator> comparingByValue() {
        return new Comparator>() {
            @Override
            public int compare(Map.Entry a, Map.Entry b) {
                return N.compare(a.getValue(), b.getValue());
            }
        };
    }

    @SuppressWarnings("rawtypes")
    public static  Comparator> reversedComparingByValue() {
        return new Comparator>() {
            @Override
            public int compare(Map.Entry a, Map.Entry b) {
                return N.compare(b.getValue(), a.getValue());
            }
        };
    }

    public static  Comparator> comparingByKey(final Comparator cmp) {
        N.requireNonNull(cmp);

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

    public static  Comparator> comparingByValue(final Comparator cmp) {
        N.requireNonNull(cmp);

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

    public static  Comparator> reversedComparingByKey(final Comparator cmp) {
        N.requireNonNull(cmp);

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

    public static  Comparator> reversedComparingByValue(final Comparator cmp) {
        N.requireNonNull(cmp);

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy