View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.comparator;
17  
18  import org.kuali.rice.core.web.format.DateFormatter;
19  
20  import java.io.Serializable;
21  import java.util.Comparator;
22  import java.util.Date;
23  
24  public class TemporalValueComparator implements Comparator, Serializable {
25      private static final TemporalValueComparator theInstance = new TemporalValueComparator();
26      
27      public TemporalValueComparator() {
28      }
29      
30      public static TemporalValueComparator getInstance() {
31          return theInstance;
32      }
33      
34      public int compare(Object o1, Object o2) {
35  
36          // null guard. non-null value is greater. equal if both are null
37          if (null == o1 || null == o2) {
38              return null == o1 && null == o2 ? 0 : null == o1 ? -1 : 1;
39          }
40  
41          String s1 = (String) o1;
42          String s2 = (String) o2;
43  
44          DateFormatter f1 = new DateFormatter();
45  
46          Date d1 = (Date) f1.convertFromPresentationFormat(s1);
47          Date d2 = (Date) f1.convertFromPresentationFormat(s2);
48          
49          if (null == d1 || null == d2) {
50              return null == d1 && null == d2 ? 0 : null == d1 ? -1 : 1;
51          }
52  
53          return d1.equals(d2) ? 0 : d1.before(d2) ? -1 : 1;
54      }
55  }