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          buffer.append(charSequence);
73      }
74  
75      /**
76       * Returns the message.
77       * @return the message.
78       */
79      @Override
80      public String getFormat() {
81          return getFormattedMessage();
82      }
83  
84      /**
85       * Returns null since there are no parameters.
86       * @return null.
87       */
88      @Override
89      public Object[] getParameters() {
90          return null;
91      }
92  
93      @Override
94      public boolean equals(final Object o) {
95          if (this == o) {
96              return true;
97          }
98          if (o == null || getClass() != o.getClass()) {
99              return false;
100         }
101 
102         final SimpleMessage that = (SimpleMessage) o;
103 
104         return !(charSequence != null ? !charSequence.equals(that.charSequence) : that.charSequence != null);
105     }
106 
107     @Override
108     public int hashCode() {
109         return charSequence != null ? charSequence.hashCode() : 0;
110     }
111 
112     @Override
113     public String toString() {
114         return getFormattedMessage();
115     }
116 
117     /**
118      * Always returns null.
119      *
120      * @return null
121      */
122     @Override
123     public Throwable getThrowable() {
124         return null;
125     }
126 
127 
128     // CharSequence impl
129 
130     @Override
131     public int length() {
132         return charSequence == null ? 0 : charSequence.length();
133     }
134 
135     @Override
136     public char charAt(final int index) {
137         return charSequence.charAt(index);
138     }
139 
140     @Override
141     public CharSequence subSequence(final int start, final int end) {
142         return charSequence.subSequence(start, end);
143     }
144 
145 
146     private void writeObject(final ObjectOutputStream out) throws IOException {
147         getFormattedMessage(); // initialize the message:String field
148         out.defaultWriteObject();
149     }
150 
151     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
152         in.defaultReadObject();
153         charSequence = message;
154     }
155 }