001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017
018package org.apache.logging.log4j.core.appender.rolling.action;
019
020import java.io.Serializable;
021
022import org.apache.logging.log4j.core.Core;
023import org.apache.logging.log4j.core.config.plugins.Plugin;
024import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
025import org.apache.logging.log4j.core.config.plugins.PluginFactory;
026
027/**
028 * {@link PathSorter} that sorts path by their LastModified attribute.
029 */
030@Plugin(name = "SortByModificationTime", category = Core.CATEGORY_NAME, printObject = true)
031public class PathSortByModificationTime implements PathSorter, Serializable {
032
033    private static final long serialVersionUID = 1L;
034
035    private final boolean recentFirst;
036    private final int multiplier;
037
038    /**
039     * Constructs a new SortByModificationTime sorter.
040     *
041     * @param recentFirst if true, most recently modified paths should come first
042     */
043    public PathSortByModificationTime(final boolean recentFirst) {
044        this.recentFirst = recentFirst;
045        this.multiplier = recentFirst ? 1 : -1;
046    }
047
048    /**
049     * Create a PathSorter that sorts by lastModified time.
050     *
051     * @param recentFirst if true, most recently modified paths should come first.
052     * @return A PathSorter.
053     */
054    @PluginFactory
055    public static PathSorter createSorter(
056            @PluginAttribute(value = "recentFirst", defaultBoolean = true) final boolean recentFirst) {
057        return new PathSortByModificationTime(recentFirst);
058    }
059
060    /**
061     * Returns whether this sorter sorts recent files first.
062     *
063     * @return whether this sorter sorts recent files first
064     */
065    public boolean isRecentFirst() {
066        return recentFirst;
067    }
068
069    /*
070     * (non-Javadoc)
071     *
072     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
073     */
074    @Override
075    public int compare(final PathWithAttributes path1, final PathWithAttributes path2) {
076        final long lastModified1 = path1.getAttributes().lastModifiedTime().toMillis();
077        final long lastModified2 = path2.getAttributes().lastModifiedTime().toMillis();
078        int result = Long.signum(lastModified2 - lastModified1);
079        if (result == 0) { // if same time compare paths lexicographically
080            try {
081                // assuming paths contain counters and dates, use reverse lexicographical order:
082                // 20151129 before 20151128, path-2.log before path-1.log
083                result = path2.getPath().compareTo(path1.getPath());
084            } catch (final ClassCastException ex) {
085                result = path2.getPath().toString().compareTo(path1.getPath().toString());
086            }
087        }
088        return multiplier * result;
089    }
090}