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.config.plugins.Plugin;
023import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
024import org.apache.logging.log4j.core.config.plugins.PluginFactory;
025
026/**
027 * {@link PathSorter} that sorts path by their LastModified attribute.
028 */
029@Plugin(name = "SortByModificationTime", category = "Core", printObject = true)
030public class PathSortByModificationTime implements PathSorter, Serializable {
031
032    private static final long serialVersionUID = 1L;
033
034    private final boolean recentFirst;
035    private final int multiplier;
036
037    /**
038     * Constructs a new SortByModificationTime sorter.
039     * 
040     * @param recentFirst if true, most recently modified paths should come first
041     */
042    public PathSortByModificationTime(final boolean recentFirst) {
043        this.recentFirst = recentFirst;
044        this.multiplier = recentFirst ? 1 : -1;
045    }
046
047    /**
048     * Create a PathSorter that sorts by lastModified time.
049     * 
050     * @param recentFirst if true, most recently modified paths should come first.
051     * @return A PathSorter.
052     */
053    @PluginFactory
054    public static PathSorter createSorter( //
055            @PluginAttribute(value = "recentFirst", defaultBoolean = true) final boolean recentFirst) {
056        return new PathSortByModificationTime(recentFirst);
057    }
058
059    /**
060     * Returns whether this sorter sorts recent files first.
061     * 
062     * @return whether this sorter sorts recent files first
063     */
064    public boolean isRecentFirst() {
065        return recentFirst;
066    }
067
068    /*
069     * (non-Javadoc)
070     * 
071     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
072     */
073    @Override
074    public int compare(final PathWithAttributes path1, final PathWithAttributes path2) {
075        final long lastModified1 = path1.getAttributes().lastModifiedTime().toMillis();
076        final long lastModified2 = path2.getAttributes().lastModifiedTime().toMillis();
077        int result = Long.signum(lastModified2 - lastModified1);
078        if (result == 0) { // if same time compare paths lexicographically
079            try {
080                // assuming paths contain counters and dates, use reverse lexicographical order:
081                // 20151129 before 20151128, path-2.log before path-1.log
082                result = path2.getPath().compareTo(path1.getPath());
083            } catch (final ClassCastException ex) {
084                result = path2.getPath().toString().compareTo(path1.getPath().toString());
085            }
086        }
087        return multiplier * result;
088    }
089}