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 */
017package org.apache.logging.log4j.core.appender.rolling.action;
018
019import java.nio.file.Path;
020import java.nio.file.attribute.BasicFileAttributes;
021import java.util.Objects;
022
023import org.apache.logging.log4j.core.Core;
024import org.apache.logging.log4j.core.config.plugins.Plugin;
025import org.apache.logging.log4j.core.config.plugins.PluginElement;
026import org.apache.logging.log4j.core.config.plugins.PluginFactory;
027
028/**
029 * Wrapper {@code PathCondition} that accepts objects that are rejected by the wrapped component filter.
030 */
031@Plugin(name = "IfNot", category = Core.CATEGORY_NAME, printObject = true)
032public final class IfNot implements PathCondition {
033
034    private final PathCondition negate;
035
036    private IfNot(final PathCondition negate) {
037        this.negate = Objects.requireNonNull(negate, "filter");
038    }
039
040    public PathCondition getWrappedFilter() {
041        return negate;
042    }
043
044    /*
045     * (non-Javadoc)
046     * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#accept(java.nio.file.Path, java.nio.file.Path, java.nio.file.attribute.BasicFileAttributes)
047     */
048    @Override
049    public boolean accept(final Path baseDir, final Path relativePath, final BasicFileAttributes attrs) {
050        return !negate.accept(baseDir, relativePath, attrs);
051    }
052
053    /*
054     * (non-Javadoc)
055     * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#beforeFileTreeWalk()
056     */
057    @Override
058    public void beforeFileTreeWalk() {
059        negate.beforeFileTreeWalk();
060    }
061
062    /**
063     * Create an IfNot PathCondition.
064     *
065     * @param condition The condition to negate.
066     * @return An IfNot PathCondition.
067     */
068    @PluginFactory
069    public static IfNot createNotCondition(
070            @PluginElement("PathConditions") final PathCondition condition) {
071        return new IfNot(condition);
072    }
073
074    @Override
075    public String toString() {
076        return "IfNot(" + negate + ")";
077    }
078}