View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.appender.rolling.action;
18  
19  import java.nio.file.Path;
20  import java.nio.file.attribute.BasicFileAttributes;
21  import java.util.Objects;
22  
23  import org.apache.logging.log4j.core.config.plugins.Plugin;
24  import org.apache.logging.log4j.core.config.plugins.PluginElement;
25  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
26  
27  /**
28   * Wrapper {@code PathCondition} that accepts objects that are rejected by the wrapped component filter.
29   */
30  @Plugin(name = "IfNot", category = "Core", printObject = true)
31  public final class IfNot implements PathCondition {
32  
33      private final PathCondition negate;
34  
35      private IfNot(final PathCondition negate) {
36          this.negate = Objects.requireNonNull(negate, "filter");
37      }
38  
39      public PathCondition getWrappedFilter() {
40          return negate;
41      }
42  
43      /*
44       * (non-Javadoc)
45       * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#accept(java.nio.file.Path, java.nio.file.Path, java.nio.file.attribute.BasicFileAttributes)
46       */
47      @Override
48      public boolean accept(final Path baseDir, final Path relativePath, final BasicFileAttributes attrs) {
49          return !negate.accept(baseDir, relativePath, attrs);
50      }
51  
52      /*
53       * (non-Javadoc)
54       * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#beforeFileTreeWalk()
55       */
56      @Override
57      public void beforeFileTreeWalk() {
58          negate.beforeFileTreeWalk();
59      }
60  
61      /**
62       * Create an IfNot PathCondition.
63       * 
64       * @param condition The condition to negate.
65       * @return An IfNot PathCondition.
66       */
67      @PluginFactory
68      public static IfNot createNotCondition( //
69              @PluginElement("PathConditions") final PathCondition condition) {
70          return new IfNot(condition);
71      }
72  
73      @Override
74      public String toString() {
75          return "IfNot(" + negate + ")";
76      }
77  }