1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v2.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  
15  package ch.qos.logback.core.boolex;
16  
17  /**
18   * Condition that evaluates to {@code true} when a property
19   * equals a specified expected value.
20   *
21   * <p>The property named by {@link #key} is resolved using the
22   * inherited property lookup mechanism (see {@code PropertyConditionBase}).
23   * If the resolved property value equals {@link #value} (using
24   * {@link String#equals(Object)}), this condition evaluates to {@code true}.
25   *
26   * @since 1.5.20
27   */
28  public class PropertyEqualityCondition extends PropertyConditionBase {
29  
30      /**
31       * The property name (key) to look up. Must be set before starting.
32       */
33      String key;
34  
35      /**
36       * The expected value to compare the resolved property against.
37       */
38      String value;
39  
40      /**
41       * Start the component and validate required parameters.
42       * If either {@link #key} or {@link #value} is {@code null}, an error
43       * is reported and the component does not start.
44       */
45      public void start() {
46          if (key == null) {
47              addError("In PropertyEqualsValue 'key' parameter cannot be null");
48              return;
49          }
50          if (value == null) {
51              addError("In PropertyEqualsValue 'value' parameter cannot be null");
52              return;
53          }
54          super.start();
55      }
56  
57      /**
58       * Return the configured expected value.
59       *
60       * @return the expected value, or {@code null} if not set
61       */
62      public String getValue() {
63          return value;
64      }
65  
66      /**
67       * Set the expected value that the resolved property must equal for
68       * this condition to evaluate to {@code true}.
69       *
70       * @param value the expected value
71       */
72      public void setValue(String value) {
73          this.value = value;
74      }
75  
76      /**
77       * Return the property key that will be looked up when evaluating the
78       * condition.
79       *
80       * @return the property key, or {@code null} if not set
81       */
82      public String getKey() {
83          return key;
84      }
85  
86      /**
87       * Set the property key to resolve during evaluation.
88       *
89       * @param key the property key
90       */
91      public void setKey(String key) {
92          this.key = key;
93      }
94  
95      /**
96       * Evaluate the condition: resolve the property named by {@link #key}
97       * and compare it to {@link #value}.
98       *
99       * @return {@code true} if the resolved property equals the expected
100      *         value; {@code false} otherwise
101      */
102     @Override
103     public boolean evaluate() {
104         if (key == null) {
105             addError("key cannot be null");
106             return false;
107         }
108 
109         String val = p(key);
110         if (val == null)
111             return false;
112         else {
113             return val.equals(value);
114         }
115     }
116 
117 }