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