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 if (message == null) { 065 message = String.valueOf(charSequence); 066 } 067 return message; 068 } 069 070 @Override 071 public void formatTo(final StringBuilder buffer) { 072 buffer.append(charSequence); 073 } 074 075 /** 076 * Returns the message. 077 * @return the message. 078 */ 079 @Override 080 public String getFormat() { 081 return getFormattedMessage(); 082 } 083 084 /** 085 * Returns null since there are no parameters. 086 * @return null. 087 */ 088 @Override 089 public Object[] getParameters() { 090 return null; 091 } 092 093 @Override 094 public boolean equals(final Object o) { 095 if (this == o) { 096 return true; 097 } 098 if (o == null || getClass() != o.getClass()) { 099 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(int index) { 137 return charSequence.charAt(index); 138 } 139 140 @Override 141 public CharSequence subSequence(int start, 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}