001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.message;
018
019import java.io.IOException;
020import java.io.ObjectInputStream;
021import java.io.ObjectOutputStream;
022import org.apache.logging.log4j.util.StringBuilderFormattable;
023
024/**
025 * The simplest possible implementation of Message. It just returns the String given as the constructor argument.
026 */
027public class SimpleMessage implements Message, StringBuilderFormattable, CharSequence {
028    private static final long serialVersionUID = -8398002534962715992L;
029
030    private String message;
031    private transient CharSequence charSequence;
032
033    /**
034     * Basic constructor.
035     */
036    public SimpleMessage() {
037        this(null);
038    }
039
040    /**
041     * Constructor that includes the message.
042     * @param message The String message.
043     */
044    public SimpleMessage(final String message) {
045        this.message = message;
046        this.charSequence = message;
047    }
048
049    /**
050     * Constructor that includes the message.
051     * @param charSequence The CharSequence message.
052     */
053    public SimpleMessage(final CharSequence charSequence) {
054        // this.message = String.valueOf(charSequence); // postponed until getFormattedMessage
055        this.charSequence = charSequence;
056    }
057
058    /**
059     * Returns the message.
060     * @return the message.
061     */
062    @Override
063    public String getFormattedMessage() {
064        return message = message == null ? String.valueOf(charSequence) : message ;
065    }
066
067    @Override
068    public void formatTo(final StringBuilder buffer) {
069        buffer.append(message != null ? message : charSequence);
070    }
071
072    /**
073     * Returns the message.
074     * @return the message.
075     */
076    @Override
077    public String getFormat() {
078        return getFormattedMessage();
079    }
080
081    /**
082     * Returns null since there are no parameters.
083     * @return null.
084     */
085    @Override
086    public Object[] getParameters() {
087        return null;
088    }
089
090    @Override
091    public boolean equals(final Object o) {
092        if (this == o) {
093            return true;
094        }
095        if (o == null || getClass() != o.getClass()) {
096            return false;
097        }
098
099        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}