View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.file.comparator;
8   
9   import org.mule.util.ClassUtils;
10  import org.mule.util.FileUtils;
11  
12  import java.io.File;
13  import java.text.MessageFormat;
14  import java.util.Comparator;
15  
16  /**
17   * <p><code>OlderComparatorComparator</code> is a {@link Comparator} of File
18   * which is capable of comparing files for equality based on their modification dates.</p>
19   */
20  public class OlderFirstComparator implements Comparator
21  {
22      public int compare(Object o1, Object o2)
23      {
24          if (o1 instanceof File && o2 instanceof File)
25          {
26              File f = (File) o1;
27              File f1 = (File) o2;
28              boolean fileNewer = FileUtils.isFileNewer(f, f1);
29              boolean fileOlder = FileUtils.isFileOlder(f, f1);
30              if (!fileNewer && !fileOlder)
31              {
32                  return 0;
33              }
34              else if (fileNewer)
35              {
36                  return 1;
37              }
38              else
39              {
40                  return -1;
41              }
42  
43          }
44          throw new IllegalArgumentException(MessageFormat.format(
45                  "Expected java.io.File instance, but was {0} and {1}",
46                  ClassUtils.getShortClassName(o1, "<null>"),
47                  ClassUtils.getShortClassName(o2, "<null")));
48      }
49  }