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