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.util.Objects;
021
022import org.apache.logging.log4j.core.config.Node;
023import org.apache.logging.log4j.core.config.plugins.Plugin;
024import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
025import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
026
027/**
028 * Key/Value pair configuration item.
029 *
030 * @since 2.1 implements {@link #hashCode()} and {@link #equals(Object)}
031 */
032@Plugin(name = "KeyValuePair", category = Node.CATEGORY, printObject = true)
033public final class KeyValuePair {
034
035    /**
036     * The empty array.
037     */
038    public static final KeyValuePair[] EMPTY_ARRAY = {};
039
040    private final String key;
041    private final String value;
042
043    /**
044     * Constructs a key/value pair. The constructor should only be called from test classes.
045     * @param key The key.
046     * @param value The value.
047     */
048    public KeyValuePair(final String key, final String value) {
049        this.key = key;
050        this.value = value;
051    }
052
053    /**
054     * Returns the key.
055     * @return the key.
056     */
057    public String getKey() {
058        return key;
059    }
060
061    /**
062     * Returns the value.
063     * @return The value.
064     */
065    public String getValue() {
066        return value;
067    }
068
069    @Override
070    public String toString() {
071        return key + '=' + value;
072    }
073
074    @PluginBuilderFactory
075    public static Builder newBuilder() {
076        return new Builder();
077    }
078
079    public static class Builder implements org.apache.logging.log4j.core.util.Builder<KeyValuePair> {
080
081        @PluginBuilderAttribute
082        private String key;
083
084        @PluginBuilderAttribute
085        private String value;
086
087        public Builder setKey(final String aKey) {
088            this.key = aKey;
089            return this;
090        }
091
092        public Builder setValue(final String aValue) {
093            this.value = aValue;
094            return this;
095        }
096
097        @Override
098        public KeyValuePair build() {
099            return new KeyValuePair(key, value);
100        }
101
102    }
103
104    @Override
105    public int hashCode() {
106        return Objects.hash(key, value);
107    }
108
109    @Override
110    public boolean equals(final Object obj) {
111        if (this == obj) {
112            return true;
113        }
114        if (obj == null) {
115            return false;
116        }
117        if (getClass() != obj.getClass()) {
118            return false;
119        }
120        final KeyValuePair other = (KeyValuePair) obj;
121        if (!Objects.equals(key, other.key)) {
122            return false;
123        }
124        if (!Objects.equals(value, other.value)) {
125            return false;
126        }
127        return true;
128    }
129}