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
    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   * Key/Value pair configuration item.
    32   *
    33   * @since 2.1 implements {@link Serializable}
    34   * @since 2.1 implements {@link #hashCode()} and {@link #equals(Object)}
    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       * Constructs a key/value pair. The constructor should only be called from test classes.
    46       * @param key The key.
    47       * @param value The value.
    48       */
    49      public KeyValuePair(final String key, final String value) {
    50          this.key = key;
    51          this.value = value;
    52      }
    53
    54      /**
    55       * Returns the key.
    56       * @return the key.
    57       */
    58      public String getKey() {
    59          return key;
    60      }
    61
    62      /**
    63       * Returns the value.
    64       * @return The value.
    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 }