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.logging.log4j.core.util;
19  
20  import org.apache.logging.log4j.core.config.Node;
21  import org.apache.logging.log4j.core.config.plugins.Plugin;
22  import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
23  import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
24  
25  /**
26   * Key/Value pair configuration item.
27   *
28   * @since 2.1 implements {@link #hashCode()} and {@link #equals(Object)}
29   */
30  @Plugin(name = "KeyValuePair", category = Node.CATEGORY, printObject = true)
31  public final class KeyValuePair {
32  
33      private final String key;
34      private final String value;
35  
36      /**
37       * Constructs a key/value pair. The constructor should only be called from test classes.
38       * @param key The key.
39       * @param value The value.
40       */
41      public KeyValuePair(final String key, final String value) {
42          this.key = key;
43          this.value = value;
44      }
45  
46      /**
47       * Returns the key.
48       * @return the key.
49       */
50      public String getKey() {
51          return key;
52      }
53  
54      /**
55       * Returns the value.
56       * @return The value.
57       */
58      public String getValue() {
59          return value;
60      }
61  
62      @Override
63      public String toString() {
64          return key + '=' + value;
65      }
66  
67      @PluginBuilderFactory
68      public static Builder newBuilder() {
69          return new Builder();
70      }
71  
72      public static class Builder implements org.apache.logging.log4j.core.util.Builder<KeyValuePair> {
73  
74          @PluginBuilderAttribute
75          private String key;
76  
77          @PluginBuilderAttribute
78          private String value;
79  
80          public Builder setKey(final String aKey) {
81              this.key = aKey;
82              return this;
83          }
84  
85          public Builder setValue(final String aValue) {
86              this.value = aValue;
87              return this;
88          }
89  
90          @Override
91          public KeyValuePair build() {
92              return new KeyValuePair(key, value);
93          }
94  
95      }
96  
97      @Override
98      public int hashCode() {
99          final int prime = 31;
100         int result = 1;
101         result = prime * result + ((key == null) ? 0 : key.hashCode());
102         result = prime * result + ((value == null) ? 0 : value.hashCode());
103         return result;
104     }
105 
106     @Override
107     public boolean equals(final Object obj) {
108         if (this == obj) {
109             return true;
110         }
111         if (obj == null) {
112             return false;
113         }
114         if (getClass() != obj.getClass()) {
115             return false;
116         }
117         final KeyValuePair other = (KeyValuePair) obj;
118         if (key == null) {
119             if (other.key != null) {
120                 return false;
121             }
122         } else if (!key.equals(other.key)) {
123             return false;
124         }
125         if (value == null) {
126             if (other.value != null) {
127                 return false;
128             }
129         } else if (!value.equals(other.value)) {
130             return false;
131         }
132         return true;
133     }
134 }