1919
2020@ SuppressWarnings ("rawtypes" )
2121public class GenericComparator implements Comparator , Serializable {
22- protected static final long serialVersionUID = -2293914106471884607L ;
23- protected static final int LESSER = -1 ;
24- protected static final int EQUAL = 0 ;
25- protected static final int GREATER = 1 ;
26- protected static final String METHOD_GET_PREFIX = "get" ;
27- protected static final String DATATYPE_STRING = "java.lang.String" ;
28- protected static final String DATATYPE_DATE = "java.time.LocalDate" ;
29- protected static final String DATATYPE_INTEGER = "java.lang.Integer" ;
30- protected static final String DATATYPE_LONG = "java.lang.Long" ;
31- protected static final String DATATYPE_FLOAT = "java.lang.Float" ;
32- protected static final String DATATYPE_DOUBLE = "java.lang.Double" ;
33- protected static final String DATATYPE_BOOLEAN = "java.lang.Boolean" ;
34-
35- protected enum CompareMode { EQUAL , LESS_THAN , GREATER_THAN , DEFAULT }
36-
37- protected String targetMethod ;
38- protected boolean sortAscending ;
39-
40- /**
22+ protected static final long serialVersionUID = -2293914106471884607L ;
23+ protected static final int LESSER = -1 ;
24+ protected static final int EQUAL = 0 ;
25+ protected static final int GREATER = 1 ;
26+ protected static final String METHOD_GET_PREFIX = "get" ;
27+ protected static final String DATATYPE_STRING = "java.lang.String" ;
28+ protected static final String DATATYPE_DATE = "java.time.LocalDate" ;
29+ protected static final String DATATYPE_INTEGER = "java.lang.Integer" ;
30+ protected static final String DATATYPE_LONG = "java.lang.Long" ;
31+ protected static final String DATATYPE_FLOAT = "java.lang.Float" ;
32+ protected static final String DATATYPE_DOUBLE = "java.lang.Double" ;
33+ protected static final String DATATYPE_BOOLEAN = "java.lang.Boolean" ;
34+
35+ protected enum CompareMode { EQUAL , LESS_THAN , GREATER_THAN , DEFAULT }
36+
37+ protected String targetMethod ;
38+ protected boolean sortAscending ;
39+
40+ /**
4141 * <p>default constructor - assumes comparator for Type List</p>
4242 *
4343 * <p>For Example-</p>
@@ -110,6 +110,9 @@ public GenericComparator(String sortField, boolean sortAscending) {
110110 * {@inheritDoc}
111111 */
112112 public int compare (Object o1 , Object o2 ) {
113+ if (o1 == null || o2 == null ) {
114+ throw new NullPointerException ("Arguments to compare() must not be null" );
115+ }
113116 int response = LESSER ;
114117 Object v1 , v2 ;
115118 String returnType ;
@@ -136,21 +139,21 @@ public int compare(Object o1, Object o2) {
136139 return response ;
137140 }
138141
139- /**
142+ /**
140143 * alternate to actual value comparison i.e., either (lsh & rhs) one the value could be null
141144 *
142145 * @param cm - a enum used to idetify the position for sorting
143146 */
144147 protected int compareAlternate (CompareMode cm ) {
145- return switch (cm ) {
146- case LESS_THAN -> LESSER * determinePosition ();
147- case GREATER_THAN -> GREATER * determinePosition ();
148- case EQUAL -> EQUAL * determinePosition ();
149- default -> LESSER ;
150- };
151- }
152-
153- /**
148+ return switch (cm ) {
149+ case LESS_THAN -> LESSER * determinePosition ();
150+ case GREATER_THAN -> GREATER * determinePosition ();
151+ case EQUAL -> EQUAL * determinePosition ();
152+ default -> LESSER ;
153+ };
154+ }
155+
156+ /**
154157 * actual value comparison for sorting; both lsh & rhs value available
155158 *
156159 * @param v1 - value of lhs
@@ -228,11 +231,11 @@ protected Object getValue(Object obj) throws InvocationTargetException, IllegalA
228231 * @return compareMode - a {@link com.devopsdemo.utilities.GenericComparator.CompareMode}
229232 */
230233 protected CompareMode findCompareMode (Object o1 , Object o2 ) {
231- if (o1 == null && o2 == null ) return CompareMode .EQUAL ;
232- if (o1 == null ) return CompareMode .LESS_THAN ;
233- if (o2 == null ) return CompareMode .GREATER_THAN ;
234- return CompareMode .DEFAULT ;
235- }
234+ if (o1 == null && o2 == null ) return CompareMode .EQUAL ;
235+ if (o1 == null ) return CompareMode .LESS_THAN ;
236+ if (o2 == null ) return CompareMode .GREATER_THAN ;
237+ return CompareMode .DEFAULT ;
238+ }
236239
237240 /**
238241 * Determining positing for sorting
0 commit comments