001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017
018package org.apache.logging.log4j.core.util;
019
020import java.io.InvalidObjectException;
021import java.io.ObjectInputStream;
022import java.io.ObjectStreamException;
023import java.io.Serializable;
024
025import org.apache.logging.log4j.core.config.Node;
026import org.apache.logging.log4j.core.config.plugins.Plugin;
027import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
028import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
029
030/**
031 * Key/Value pair configuration item.
032 *
033 * @since 2.1 implements {@link Serializable}
034 * @since 2.1 implements {@link #hashCode()} and {@link #equals(Object)}
035 */
036@Plugin(name = "KeyValuePair", category = Node.CATEGORY, printObject = true)
037public final class KeyValuePair implements Serializable {
038
039    private static final long serialVersionUID = 4331228262821046866L;
040
041    private final String key;
042    private final String value;
043
044    /**
045     * Constructs a key/value pair. The constructor should only be called from test classes.
046     * @param key The key.
047     * @param value The value.
048     */
049    public KeyValuePair(final String key, final String value) {
050        this.key = key;
051        this.value = value;
052    }
053
054    /**
055     * Returns the key.
056     * @return the key.
057     */
058    public String getKey() {
059        return key;
060    }
061
062    /**
063     * Returns the value.
064     * @return The value.
065     */
066    public String getValue() {
067        return value;
068    }
069
070    @Override
071    public String toString() {
072        return key + '=' + value;
073    }
074
075    @PluginBuilderFactory
076    public static Builder newBuilder() {
077        return new Builder();
078    }
079
080    protected Object writeReplace() throws ObjectStreamException {
081        return newBuilder().setKey(this.key).setValue(this.value);
082    }
083
084    private void readObject(final ObjectInputStream stream) throws InvalidObjectException {
085        throw new InvalidObjectException("Builder proxy required");
086    }
087
088    public static class Builder implements org.apache.logging.log4j.core.util.Builder<KeyValuePair>, Serializable {
089
090        private static final long serialVersionUID = 1L;
091
092        @PluginBuilderAttribute
093        private String key;
094
095        @PluginBuilderAttribute
096        private String value;
097
098        public Builder setKey(final String key) {
099            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}