Coverage Report - org.apache.commons.io.comparator.LastModifiedFileComparator
 
Classes in this File Line Coverage Branch Coverage Complexity
LastModifiedFileComparator
100%
9/9
100%
4/4
5
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  * 
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.io.comparator;
 18  
 
 19  
 import java.io.File;
 20  
 import java.io.Serializable;
 21  
 import java.util.Comparator;
 22  
 
 23  
 /**
 24  
  * Compare the <b>last modified date/time</b> of two files for order 
 25  
  * (see {@link File#lastModified()}).
 26  
  * <p>
 27  
  * This comparator can be used to sort lists or arrays of files
 28  
  * by their last modified date/time.
 29  
  * <p>
 30  
  * Example of sorting a list of files using the
 31  
  * {@link #LASTMODIFIED_COMPARATOR} singleton instance:
 32  
  * <pre>
 33  
  *       List&lt;File&gt; list = ...
 34  
  *       ((AbstractFileComparator) LastModifiedFileComparator.LASTMODIFIED_COMPARATOR).sort(list);
 35  
  * </pre>
 36  
  * <p>
 37  
  * Example of doing a <i>reverse</i> sort of an array of files using the
 38  
  * {@link #LASTMODIFIED_REVERSE} singleton instance:
 39  
  * <pre>
 40  
  *       File[] array = ...
 41  
  *       ((AbstractFileComparator) LastModifiedFileComparator.LASTMODIFIED_REVERSE).sort(array);
 42  
  * </pre>
 43  
  * <p>
 44  
  *
 45  
  * @version $Id: LastModifiedFileComparator.java 1469107 2013-04-17 23:45:53Z sebb $
 46  
  * @since 1.4
 47  
  */
 48  42
 public class LastModifiedFileComparator extends AbstractFileComparator implements Serializable {
 49  
 
 50  
     /** Last modified comparator instance */
 51  2
     public static final Comparator<File> LASTMODIFIED_COMPARATOR = new LastModifiedFileComparator();
 52  
 
 53  
     /** Reverse last modified comparator instance */
 54  2
     public static final Comparator<File> LASTMODIFIED_REVERSE = new ReverseComparator(LASTMODIFIED_COMPARATOR);
 55  
 
 56  
     /**
 57  
      * Compare the last the last modified date/time of two files.
 58  
      * 
 59  
      * @param file1 The first file to compare
 60  
      * @param file2 The second file to compare
 61  
      * @return a negative value if the first file's lastmodified date/time
 62  
      * is less than the second, zero if the lastmodified date/time are the
 63  
      * same and a positive value if the first files lastmodified date/time
 64  
      * is greater than the second file.
 65  
      * 
 66  
      */
 67  
     public int compare(final File file1, final File file2) {
 68  28
         final long result = file1.lastModified() - file2.lastModified();
 69  28
         if (result < 0) {
 70  16
             return -1;
 71  12
         } else if (result > 0) {
 72  8
             return 1;
 73  
         } else {
 74  4
             return 0;
 75  
         }
 76  
     }
 77  
 }