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  package org.apache.logging.log4j.core.jackson;
18  
19  import org.apache.logging.log4j.util.Strings;
20  
21  import com.fasterxml.jackson.annotation.JsonCreator;
22  import com.fasterxml.jackson.annotation.JsonProperty;
23  import com.fasterxml.jackson.annotation.JsonPropertyOrder;
24  import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
25  
26  /**
27   * <p>
28   * <em>Consider this class private.</em>
29   * </p>
30   * <p>
31   * Used to represent map entries in a generic fashion because the default Jackson behavior uses the key as the element tag. Using the key as
32   * an element/property name would mean that you cannot have a generic JSON/XML schema for all log event.
33   * </p>
34   */
35  @JsonPropertyOrder({ "key", "value" })
36  final class MapEntry {
37  
38      @JsonProperty
39      @JacksonXmlProperty(isAttribute = true)
40      private String key;
41  
42      @JsonProperty
43      @JacksonXmlProperty(isAttribute = true)
44      private String value;
45  
46      @JsonCreator
47      public MapEntry(@JsonProperty("key") final String key, @JsonProperty("value") final String value) {
48          this.setKey(key);
49          this.setValue(value);
50      }
51  
52      @Override
53      public boolean equals(final Object obj) {
54          if (this == obj) {
55              return true;
56          }
57          if (obj == null) {
58              return false;
59          }
60          if (!(obj instanceof MapEntry)) {
61              return false;
62          }
63          final MapEntry other = (MapEntry) obj;
64          if (this.getKey() == null) {
65              if (other.getKey() != null) {
66                  return false;
67              }
68          } else if (!this.getKey().equals(other.getKey())) {
69              return false;
70          }
71          if (this.getValue() == null) {
72              if (other.getValue() != null) {
73                  return false;
74              }
75          } else if (!this.getValue().equals(other.getValue())) {
76              return false;
77          }
78          return true;
79      }
80  
81      public String getKey() {
82          return this.key;
83      }
84  
85      public String getValue() {
86          return this.value;
87      }
88  
89      @Override
90      public int hashCode() {
91          final int prime = 31;
92          int result = 1;
93          result = prime * result + ((this.getKey() == null) ? 0 : this.getKey().hashCode());
94          result = prime * result + ((this.getValue() == null) ? 0 : this.getValue().hashCode());
95          return result;
96      }
97  
98      public void setKey(final String key) {
99          this.key = key;
100     }
101 
102     public void setValue(final String value) {
103         this.value = value;
104     }
105 
106     @Override
107     public String toString() {
108         return Strings.EMPTY + this.getKey() + "=" + this.getValue();
109     }
110 }