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