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 import org.apache.log4j.spi.LoggingEventFieldResolver;
22
23 import java.util.Stack;
24
25
26 /***
27 * A Rule class implementing not equals against two strings.
28 *
29 * @author Scott Deboy (sdeboy@apache.org)
30 */
31 public class NotEqualsRule extends AbstractRule {
32 /***
33 * Serialization ID.
34 */
35 static final long serialVersionUID = -1135478467213793211L;
36 /***
37 * Resolver.
38 */
39 private static final LoggingEventFieldResolver RESOLVER =
40 LoggingEventFieldResolver.getInstance();
41 /***
42 * Field.
43 */
44 private final String field;
45 /***
46 * Value.
47 */
48 private final String value;
49
50 /***
51 * Create new instance.
52 * @param field field
53 * @param value value
54 */
55 private NotEqualsRule(final String field, final String value) {
56 super();
57 if (!RESOLVER.isField(field)) {
58 throw new IllegalArgumentException(
59 "Invalid NOT EQUALS rule - " + field + " is not a supported field");
60 }
61
62 this.field = field;
63 this.value = value;
64 }
65
66 /***
67 * Get new instance.
68 * @param field field
69 * @param value value
70 * @return new instance.
71 */
72 public static Rule getRule(final String field, final String value) {
73 return new NotEqualsRule(field, value);
74 }
75
76 /***
77 * Get new instance from top two elements of stack.
78 * @param stack stack.
79 * @return new instance.
80 */
81 public static Rule getRule(final Stack stack) {
82 if (stack.size() < 2) {
83 throw new IllegalArgumentException(
84 "Invalid NOT EQUALS rule - expected two parameters but received "
85 + stack.size());
86 }
87
88 String p2 = stack.pop().toString();
89 String p1 = stack.pop().toString();
90
91 return new NotEqualsRule(p1, p2);
92 }
93
94 /*** {@inheritDoc} */
95 public boolean evaluate(final LoggingEvent event) {
96 Object p2 = RESOLVER.getValue(field, event);
97
98 return ((p2 != null) && !(p2.toString().equals(value)));
99 }
100 }