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