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.layout;
19  
20  import java.io.ObjectStreamException;
21  import java.io.Serializable;
22  
23  import org.apache.logging.log4j.core.config.Node;
24  import org.apache.logging.log4j.core.config.plugins.Plugin;
25  import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
26  import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
27  
28  /**
29   * PatternMatch configuration item.
30   *
31   * @since 2.4.1 implements {@link Serializable}
32   */
33  @Plugin(name = "PatternMatch", category = Node.CATEGORY, printObject = true)
34  public final class PatternMatch {
35  
36      private final String key;
37      private final String pattern;
38  
39      /**
40       * Constructs a key/value pair. The constructor should only be called from test classes.
41       * @param key The key.
42       * @param pattern The value.
43       */
44      public PatternMatch(final String key, final String pattern) {
45          this.key = key;
46          this.pattern = pattern;
47      }
48  
49      /**
50       * Returns the key.
51       * @return the key.
52       */
53      public String getKey() {
54          return key;
55      }
56  
57      /**
58       * Returns the pattern.
59       * @return The pattern.
60       */
61      public String getPattern() {
62          return pattern;
63      }
64  
65      @Override
66      public String toString() {
67          return key + '=' + pattern;
68      }
69  
70      @PluginBuilderFactory
71      public static Builder newBuilder() {
72          return new Builder();
73      }
74  
75      public static class Builder implements org.apache.logging.log4j.core.util.Builder<PatternMatch>, Serializable {
76  
77          private static final long serialVersionUID = 1L;
78  
79          @PluginBuilderAttribute
80          private String key;
81  
82          @PluginBuilderAttribute
83          private String pattern;
84  
85          public Builder setKey(final String key) {
86              this.key = key;
87              return this;
88          }
89  
90          public Builder setPattern(final String pattern) {
91              this.pattern = pattern;
92              return this;
93          }
94  
95          @Override
96          public PatternMatch build() {
97              return new PatternMatch(key, pattern);
98          }
99  
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 }