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

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

/*
 * 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.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 abstract class Comparators {

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

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

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

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

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

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

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

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

    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 keyMapper) {
        N.checkArgNotNull(keyMapper);

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

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

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

    public static  Comparator comparingBoolean(final ToBooleanFunction keyMapper) {
        N.checkArgNotNull(keyMapper);

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

    public static  Comparator comparingChar(final ToCharFunction keyMapper) {
        N.checkArgNotNull(keyMapper);

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

    public static  Comparator comparingByte(final ToByteFunction keyMapper) {
        N.checkArgNotNull(keyMapper);

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

    public static  Comparator comparingShort(final ToShortFunction keyMapper) {
        N.checkArgNotNull(keyMapper);

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

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

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

    public static  Comparator comparingFloat(final ToFloatFunction keyMapper) {
        N.checkArgNotNull(keyMapper);

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

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

    public static Comparator comparingIgnoreCase() {
        return COMPARING_IGNORE_CASE;
    }

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

    @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.checkArgNotNull(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.checkArgNotNull(cmp);

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

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

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

    public static  Comparator comparingByLength() {
        return (Comparator) COMPARING_BY_LENGTH;
    }

    @SuppressWarnings("rawtypes")
    public static  Comparator comparingBySize() {
        return (Comparator) COMPARING_BY_SIZE;
    }

    @SuppressWarnings("rawtypes")
    static  Comparator comparingBySizee() {
        return (Comparator) COMPARING_BY_SIZEE;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy