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.Arrays;
022import java.util.Objects;
023
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 * Composite {@code PathCondition} that accepts objects that are accepted by <em>any</em> component conditions.
030 * Corresponds to logical "OR".
031 */
032@Plugin(name = "IfAny", category = "Core", printObject = true)
033public final class IfAny implements PathCondition {
034
035    private final PathCondition[] components;
036
037    private IfAny(final PathCondition... filters) {
038        this.components = Objects.requireNonNull(filters, "filters");
039    }
040
041    public PathCondition[] getDeleteFilters() {
042        return components;
043    }
044
045    /*
046     * (non-Javadoc)
047     * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#accept(java.nio.file.Path, java.nio.file.Path, java.nio.file.attribute.BasicFileAttributes)
048     */
049    @Override
050    public boolean accept(final Path baseDir, final Path relativePath, final BasicFileAttributes attrs) {
051        for (final PathCondition component : components) {
052            if (component.accept(baseDir, relativePath, attrs)) {
053                return true;
054            }
055        }
056        return false;
057    }
058
059    /*
060     * (non-Javadoc)
061     * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#beforeFileTreeWalk()
062     */
063    @Override
064    public void beforeFileTreeWalk() {
065        for (final PathCondition condition : components) {
066            condition.beforeFileTreeWalk();
067        }
068    }
069
070    /**
071     * Create a Composite PathCondition: accepts if any of the nested conditions accepts.
072     * 
073     * @param components The component conditions.
074     * @return A Composite PathCondition.
075     */
076    @PluginFactory
077    public static IfAny createOrCondition( //
078            @PluginElement("PathConditions") final PathCondition... components) {
079        return new IfAny(components);
080    }
081
082    @Override
083    public String toString() {
084        return "IfAny" + Arrays.toString(components);
085    }
086}