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  package org.apache.logging.log4j.message;
18  
19  import java.io.IOException;
20  import java.io.ObjectInputStream;
21  import java.io.ObjectOutputStream;
22  import org.apache.logging.log4j.util.StringBuilderFormattable;
23  
24  /**
25   * The simplest possible implementation of Message. It just returns the String given as the constructor argument.
26   */
27  public class SimpleMessage implements Message, StringBuilderFormattable, CharSequence {
28      private static final long serialVersionUID = -8398002534962715992L;
29  
30      private String message;
31      private transient CharSequence charSequence;
32  
33      /**
34       * Basic constructor.
35       */
36      public SimpleMessage() {
37          this(null);
38      }
39  
40      /**
41       * Constructor that includes the message.
42       * @param message The String message.
43       */
44      public SimpleMessage(final String message) {
45          this.message = message;
46          this.charSequence = message;
47      }
48  
49      /**
50       * Constructor that includes the message.
51       * @param charSequence The CharSequence message.
52       */
53      public SimpleMessage(final CharSequence charSequence) {
54          // this.message = String.valueOf(charSequence); // postponed until getFormattedMessage
55          this.charSequence = charSequence;
56      }
57  
58      /**
59       * Returns the message.
60       * @return the message.
61       */
62      @Override
63      public String getFormattedMessage() {
64          if (message == null) {
65              message = String.valueOf(charSequence);
66          }
67          return message;
68      }
69  
70      @Override
71      public void formatTo(final StringBuilder buffer) {
72          if (message != null) {
73              buffer.append(message);
74          } else {
75              buffer.append(charSequence);
76          }
77      }
78  
79      /**
80       * Returns the message.
81       * @return the message.
82       */
83      @Override
84      public String getFormat() {
85          return getFormattedMessage();
86      }
87  
88      /**
89       * Returns null since there are no parameters.
90       * @return null.
91       */
92      @Override
93      public Object[] getParameters() {
94          return null;
95      }
96  
97      @Override
98      public boolean equals(final Object o) {
99          if (this == o) {
100             return true;
101         }
102         if (o == null || getClass() != o.getClass()) {
103             return false;
104         }
105 
106         final SimpleMessage that = (SimpleMessage) o;
107 
108         return !(charSequence != null ? !charSequence.equals(that.charSequence) : that.charSequence != null);
109     }
110 
111     @Override
112     public int hashCode() {
113         return charSequence != null ? charSequence.hashCode() : 0;
114     }
115 
116     @Override
117     public String toString() {
118         return getFormattedMessage();
119     }
120 
121     /**
122      * Always returns null.
123      *
124      * @return null
125      */
126     @Override
127     public Throwable getThrowable() {
128         return null;
129     }
130 
131 
132     // CharSequence impl
133 
134     @Override
135     public int length() {
136         return charSequence == null ? 0 : charSequence.length();
137     }
138 
139     @Override
140     public char charAt(final int index) {
141         return charSequence.charAt(index);
142     }
143 
144     @Override
145     public CharSequence subSequence(final int start, final int end) {
146         return charSequence.subSequence(start, end);
147     }
148 
149 
150     private void writeObject(final ObjectOutputStream out) throws IOException {
151         getFormattedMessage(); // initialize the message:String field
152         out.defaultWriteObject();
153     }
154 
155     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
156         in.defaultReadObject();
157         charSequence = message;
158     }
159 }