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