1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 }