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.core.pattern;
018
019import java.util.List;
020
021import org.apache.logging.log4j.core.config.Configuration;
022import org.apache.logging.log4j.core.config.plugins.Plugin;
023import org.apache.logging.log4j.core.layout.PatternLayout;
024import org.apache.logging.log4j.util.PerformanceSensitive;
025import org.apache.logging.log4j.util.StringBuilders;
026
027/**
028 * Equals pattern converter.
029 */
030@Plugin(name = "equals", category = PatternConverter.CATEGORY)
031@ConverterKeys({ "equals" })
032@PerformanceSensitive("allocation")
033public final class EqualsReplacementConverter extends EqualsBaseReplacementConverter {
034
035    /**
036     * Gets an instance of the class.
037     *
038     * @param config  The current Configuration.
039     * @param options pattern options, an array of three elements: pattern, testString, and substitution.
040     * @return instance of class.
041     */
042    public static EqualsReplacementConverter newInstance(final Configuration config, final String[] options) {
043        if (options.length != 3) {
044            LOGGER.error("Incorrect number of options on equals. Expected 3 received " + options.length);
045            return null;
046        }
047        if (options[0] == null) {
048            LOGGER.error("No pattern supplied on equals");
049            return null;
050        }
051        if (options[1] == null) {
052            LOGGER.error("No test string supplied on equals");
053            return null;
054        }
055        if (options[2] == null) {
056            LOGGER.error("No substitution supplied on equals");
057            return null;
058        }
059        final String p = options[1];
060        final PatternParser parser = PatternLayout.createPatternParser(config);
061        final List<PatternFormatter> formatters = parser.parse(options[0]);
062        return new EqualsReplacementConverter(formatters, p, options[2], parser);
063    }
064
065    /**
066     * Construct the converter.
067     *
068     * @param formatters   The PatternFormatters to generate the text to manipulate.
069     * @param testString   The test string.
070     * @param substitution The substitution string.
071     * @param parser        The PatternParser.
072     */
073    private EqualsReplacementConverter(final List<PatternFormatter> formatters, final String testString,
074                                       final String substitution, final PatternParser parser) {
075        super("equals", "equals", formatters, testString, substitution, parser);
076    }
077
078    @Override
079    protected boolean equals(final String str, final StringBuilder buff, final int from, final int len) {
080        return StringBuilders.equals(str, 0, str.length(), buff, from, len);
081    }
082}