1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.logging.log4j.core.util;
19
20 import java.io.InvalidObjectException;
21 import java.io.ObjectInputStream;
22 import java.io.ObjectStreamException;
23 import java.io.Serializable;
24
25 import org.apache.logging.log4j.core.config.Node;
26 import org.apache.logging.log4j.core.config.plugins.Plugin;
27 import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
28 import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
29
30
31
32
33
34
35
36 @Plugin(name = "KeyValuePair", category = Node.CATEGORY, printObject = true)
37 public final class KeyValuePair implements Serializable {
38
39 private static final long serialVersionUID = 4331228262821046866L;
40
41 private final String key;
42 private final String value;
43
44
45
46
47
48
49 public KeyValuePair(final String key, final String value) {
50 this.key = key;
51 this.value = value;
52 }
53
54
55
56
57
58 public String getKey() {
59 return key;
60 }
61
62
63
64
65
66 public String getValue() {
67 return value;
68 }
69
70 @Override
71 public String toString() {
72 return key + '=' + value;
73 }
74
75 @PluginBuilderFactory
76 public static Builder newBuilder() {
77 return new Builder();
78 }
79
80 protected Object writeReplace() throws ObjectStreamException {
81 return newBuilder().setKey(this.key).setValue(this.value);
82 }
83
84 private void readObject(final ObjectInputStream stream) throws InvalidObjectException {
85 throw new InvalidObjectException("Builder proxy required");
86 }
87
88 public static class Builder implements org.apache.logging.log4j.core.util.Builder<KeyValuePair>, Serializable {
89
90 private static final long serialVersionUID = 1L;
91
92 @PluginBuilderAttribute
93 private String key;
94
95 @PluginBuilderAttribute
96 private String value;
97
98 public Builder setKey(final String key) {
99 this.key = key;
100 return this;
101 }
102
103 public Builder setValue(final String value) {
104 this.value = value;
105 return this;
106 }
107
108 @Override
109 public KeyValuePair build() {
110 return new KeyValuePair(key, value);
111 }
112
113 protected Object readResolve() throws ObjectStreamException {
114 return new KeyValuePair(key, value);
115 }
116 }
117
118 @Override
119 public int hashCode() {
120 final int prime = 31;
121 int result = 1;
122 result = prime * result + ((key == null) ? 0 : key.hashCode());
123 result = prime * result + ((value == null) ? 0 : value.hashCode());
124 return result;
125 }
126
127 @Override
128 public boolean equals(final Object obj) {
129 if (this == obj) {
130 return true;
131 }
132 if (obj == null) {
133 return false;
134 }
135 if (getClass() != obj.getClass()) {
136 return false;
137 }
138 final KeyValuePair other = (KeyValuePair) obj;
139 if (key == null) {
140 if (other.key != null) {
141 return false;
142 }
143 } else if (!key.equals(other.key)) {
144 return false;
145 }
146 if (value == null) {
147 if (other.value != null) {
148 return false;
149 }
150 } else if (!value.equals(other.value)) {
151 return false;
152 }
153 return true;
154 }
155 }