View Javadoc

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