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   * A Rule class implementing logical or.
26   *
27   * @author Scott Deboy (sdeboy@apache.org)
28   */
29  public class OrRule extends AbstractRule {
30      /***
31       * Serialization ID.
32       */
33    static final long serialVersionUID = 2088765995061413165L;
34      /***
35       * rule 1.
36       */
37    private final Rule rule1;
38      /***
39       * Rule 2.
40       */
41    private final Rule rule2;
42  
43      /***
44       * Create new instance.
45       * @param firstParam first rule
46       * @param secondParam second rule
47       */
48    private OrRule(final Rule firstParam, final Rule secondParam) {
49      super();
50      this.rule1 = firstParam;
51      this.rule2 = secondParam;
52    }
53  
54      /***
55       * Create new instance.
56       * @param firstParam first rule
57       * @param secondParam second rule
58       * @return new instance
59       */
60    public static Rule getRule(final Rule firstParam, final Rule secondParam) {
61        return new OrRule(firstParam, secondParam);
62    }
63  
64      /***
65       * Create new instance from top two elements of stack.
66       * @param stack stack
67       * @return new instance
68       */
69    public static Rule getRule(final Stack stack) {
70        if (stack.size() < 2) {
71            throw new IllegalArgumentException(
72                    "Invalid OR rule - expected two rules but received "
73                            + stack.size());
74        }
75        Object o2 = stack.pop();
76        Object o1 = stack.pop();
77        if ((o2 instanceof Rule) && (o1 instanceof Rule)) {
78            Rule p2 = (Rule) o2;
79            Rule p1 = (Rule) o1;
80            return new OrRule(p1, p2);
81        }
82        throw new IllegalArgumentException("Invalid OR rule: " + o2 + "..." + o1);
83    }
84  
85    /*** {@inheritDoc} */
86    public boolean evaluate(final LoggingEvent event) {
87      return (rule1.evaluate(event) || rule2.evaluate(event));
88    }
89  }