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 org.apache.logging.log4j.core.config.Node;
021import org.apache.logging.log4j.core.config.plugins.Plugin;
022import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
023import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
024
025/**
026 * Key/Value pair configuration item.
027 *
028 * @since 2.1 implements {@link #hashCode()} and {@link #equals(Object)}
029 */
030@Plugin(name = "KeyValuePair", category = Node.CATEGORY, printObject = true)
031public final class KeyValuePair {
032
033    private final String key;
034    private final String value;
035
036    /**
037     * Constructs a key/value pair. The constructor should only be called from test classes.
038     * @param key The key.
039     * @param value The value.
040     */
041    public KeyValuePair(final String key, final String value) {
042        this.key = key;
043        this.value = value;
044    }
045
046    /**
047     * Returns the key.
048     * @return the key.
049     */
050    public String getKey() {
051        return key;
052    }
053
054    /**
055     * Returns the value.
056     * @return The value.
057     */
058    public String getValue() {
059        return value;
060    }
061
062    @Override
063    public String toString() {
064        return key + '=' + value;
065    }
066
067    @PluginBuilderFactory
068    public static Builder newBuilder() {
069        return new Builder();
070    }
071
072    public static class Builder implements org.apache.logging.log4j.core.util.Builder<KeyValuePair> {
073
074        @PluginBuilderAttribute
075        private String key;
076
077        @PluginBuilderAttribute
078        private String value;
079
080        public Builder setKey(final String aKey) {
081            this.key = aKey;
082            return this;
083        }
084
085        public Builder setValue(final String aValue) {
086            this.value = aValue;
087            return this;
088        }
089
090        @Override
091        public KeyValuePair build() {
092            return new KeyValuePair(key, value);
093        }
094
095    }
096
097    @Override
098    public int hashCode() {
099        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}