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