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  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 }