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