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