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 org.apache.logging.log4j.util.PerformanceSensitive;
020
021/**
022 * Mutable Message wrapper around a String message.
023 * @since 2.6
024 */
025@PerformanceSensitive("allocation")
026public class ReusableSimpleMessage implements ReusableMessage, CharSequence {
027    private static final long serialVersionUID = -9199974506498249809L;
028    private static Object[] EMPTY_PARAMS = new Object[0];
029    private CharSequence charSequence;
030
031    public void set(final String message) {
032        this.charSequence = message;
033    }
034
035    public void set(final CharSequence charSequence) {
036        this.charSequence = charSequence;
037    }
038
039    @Override
040    public String getFormattedMessage() {
041        return String.valueOf(charSequence);
042    }
043
044    @Override
045    public String getFormat() {
046        return getFormattedMessage();
047    }
048
049    @Override
050    public Object[] getParameters() {
051        return EMPTY_PARAMS;
052    }
053
054    @Override
055    public Throwable getThrowable() {
056        return null;
057    }
058
059    @Override
060    public void formatTo(final StringBuilder buffer) {
061        buffer.append(charSequence);
062    }
063
064    /**
065     * This message does not have any parameters, so this method returns the specified array.
066     * @param emptyReplacement the parameter array to return
067     * @return the specified array
068     */
069    @Override
070    public Object[] swapParameters(final Object[] emptyReplacement) {
071        return emptyReplacement;
072    }
073
074    /**
075     * This message does not have any parameters so this method always returns zero.
076     * @return 0 (zero)
077     */
078    @Override
079    public short getParameterCount() {
080        return 0;
081    }
082
083    @Override
084    public Message memento() {
085        return new SimpleMessage(charSequence);
086    }
087
088    // CharSequence impl
089
090    @Override
091    public int length() {
092        return charSequence == null ? 0 : charSequence.length();
093    }
094
095    @Override
096    public char charAt(final int index) {
097        return charSequence.charAt(index);
098    }
099
100    @Override
101    public CharSequence subSequence(final int start, final int end) {
102        return charSequence.subSequence(start, end);
103    }
104}
105