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  
18  package org.apache.log4j.rule;
19  
20  import org.apache.log4j.spi.LoggingEvent;
21  
22  import java.util.Stack;
23  
24  
25  /***
26   * A Rule class implementing a logical 'and'.
27   *
28   * @author Scott Deboy (sdeboy@apache.org)
29   */
30  public class AndRule extends AbstractRule {
31      /***
32       * First rule.
33       */
34    private final Rule firstRule;
35      /***
36       * Second rule.
37       */
38    private final Rule secondRule;
39      /***
40       * Serialization id.
41       */
42    static final long serialVersionUID = -8233444426923854651L;
43  
44      /***
45       * Create new instance.
46       * @param first first rule.
47       * @param second second rule.
48       */
49    private AndRule(final Rule first, final Rule second) {
50      super();
51      this.firstRule = first;
52      this.secondRule = second;
53    }
54  
55      /***
56       * Create rule from top two elements of stack.
57       * @param stack stack of rules.
58       * @return Rule that evaluates true only if both rules are true.
59       */
60    public static Rule getRule(final Stack stack) {
61      if (stack.size() < 2) {
62          throw new IllegalArgumentException(
63                  "Invalid AND rule - expected two rules but received "
64                          + stack.size());
65      }
66      Object o2 = stack.pop();
67      Object o1 = stack.pop();
68      if ((o2 instanceof Rule) && (o1 instanceof Rule)) {
69          Rule p2 = (Rule) o2;
70          Rule p1 = (Rule) o1;
71          return new AndRule(p1, p2);
72      }
73      throw new IllegalArgumentException("Invalid AND rule: " + o2 + "..." + o1);
74    }
75  
76      /***
77       * Get rule.
78       * @param firstParam first rule.
79       * @param secondParam second rule.
80       * @return Rule that evaluates true only if both rules are true.
81       */
82    public static Rule getRule(final Rule firstParam, final Rule secondParam) {
83      return new AndRule(firstParam, secondParam);
84    }
85  
86      /***
87       * {@inheritDoc}
88       */
89    public boolean evaluate(final LoggingEvent event) {
90      return (firstRule.evaluate(event) && secondRule.evaluate(event));
91    }
92  }