| 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 | * https://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 | import org.apache.commons.io.FileUtils; |
| 24 | |
| 25 | /** |
| 26 | * Compare the <strong>last modified date/time</strong> of two files for order |
| 27 | * (see {@link FileUtils#lastModifiedUnchecked(File)}). |
| 28 | * <p> |
| 29 | * This comparator can be used to sort lists or arrays of files |
| 30 | * by their last modified date/time. |
| 31 | * </p> |
| 32 | * <p> |
| 33 | * Example of sorting a list of files using the |
| 34 | * {@link #LASTMODIFIED_COMPARATOR} singleton instance: |
| 35 | * </p> |
| 36 | * <pre> |
| 37 | * List<File> list = ... |
| 38 | * ((AbstractFileComparator) LastModifiedFileComparator.LASTMODIFIED_COMPARATOR).sort(list); |
| 39 | * </pre> |
| 40 | * <p> |
| 41 | * Example of doing a <em>reverse</em> sort of an array of files using the |
| 42 | * {@link #LASTMODIFIED_REVERSE} singleton instance: |
| 43 | * </p> |
| 44 | * <pre> |
| 45 | * File[] array = ... |
| 46 | * ((AbstractFileComparator) LastModifiedFileComparator.LASTMODIFIED_REVERSE).sort(array); |
| 47 | * </pre> |
| 48 | * <h2>Deprecating Serialization</h2> |
| 49 | * <p> |
| 50 | * <em>Serialization is deprecated and will be removed in 3.0.</em> |
| 51 | * </p> |
| 52 | * |
| 53 | * @since 1.4 |
| 54 | */ |
| 55 | public class LastModifiedFileComparator extends AbstractFileComparator implements Serializable { |
| 56 | |
| 57 | private static final long serialVersionUID = 7372168004395734046L; |
| 58 | |
| 59 | /** Last modified comparator instance. */ |
| 60 | public static final Comparator<File> LASTMODIFIED_COMPARATOR = new LastModifiedFileComparator(); |
| 61 | |
| 62 | /** Reverse last modified comparator instance. */ |
| 63 | public static final Comparator<File> LASTMODIFIED_REVERSE = new ReverseFileComparator(LASTMODIFIED_COMPARATOR); |
| 64 | |
| 65 | /** |
| 66 | * Construct a new instance. |
| 67 | */ |
| 68 | public LastModifiedFileComparator() { |
| 69 | // empty |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Compares the last modified date/time of two files. |
| 74 | * |
| 75 | * @param file1 The first file to compare. |
| 76 | * @param file2 The second file to compare. |
| 77 | * @return a negative value if the first file's last modified date/time is less than the second, zero if the last |
| 78 | * modified date/time are the same and a positive value if the first files last modified date/time is |
| 79 | * greater than the second file. |
| 80 | */ |
| 81 | @Override |
| 82 | public int compare(final File file1, final File file2) { |
| 83 | final long result = FileUtils.lastModifiedUnchecked(file1) - FileUtils.lastModifiedUnchecked(file2); |
| 84 | if (result < 0) { |
| 85 | return -1; |
| 86 | } |
| 87 | if (result > 0) { |
| 88 | return 1; |
| 89 | } |
| 90 | return 0; |
| 91 | } |
| 92 | } |