001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.util.comparator; 018 019import java.util.Comparator; 020 021/** 022 * Convenient entry point with generically typed factory methods 023 * for common Spring {@link Comparator} variants. 024 * 025 * @author Juergen Hoeller 026 * @since 5.0 027 */ 028public abstract class Comparators { 029 030 /** 031 * Return a {@link Comparable} adapter. 032 * @see ComparableComparator#INSTANCE 033 */ 034 @SuppressWarnings("unchecked") 035 public static <T> Comparator<T> comparable() { 036 return ComparableComparator.INSTANCE; 037 } 038 039 /** 040 * Return a {@link Comparable} adapter which accepts 041 * null values and sorts them lower than non-null values. 042 * @see NullSafeComparator#NULLS_LOW 043 */ 044 @SuppressWarnings("unchecked") 045 public static <T> Comparator<T> nullsLow() { 046 return NullSafeComparator.NULLS_LOW; 047 } 048 049 /** 050 * Return a decorator for the given comparator which accepts 051 * null values and sorts them lower than non-null values. 052 * @see NullSafeComparator#NullSafeComparator(boolean) 053 */ 054 public static <T> Comparator<T> nullsLow(Comparator<T> comparator) { 055 return new NullSafeComparator<>(comparator, true); 056 } 057 058 /** 059 * Return a {@link Comparable} adapter which accepts 060 * null values and sorts them higher than non-null values. 061 * @see NullSafeComparator#NULLS_HIGH 062 */ 063 @SuppressWarnings("unchecked") 064 public static <T> Comparator<T> nullsHigh() { 065 return NullSafeComparator.NULLS_HIGH; 066 } 067 068 /** 069 * Return a decorator for the given comparator which accepts 070 * null values and sorts them higher than non-null values. 071 * @see NullSafeComparator#NullSafeComparator(boolean) 072 */ 073 public static <T> Comparator<T> nullsHigh(Comparator<T> comparator) { 074 return new NullSafeComparator<>(comparator, false); 075 } 076 077}