All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.milyn.function.StringFunctionExecutor Maven / Gradle / Ivy

The newest version!
/*
 * Milyn - Copyright (C) 2006 - 2010
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License (version 2.1) as published by the Free Software
 * Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * See the GNU Lesser General Public License for more details:
 * http://www.gnu.org/licenses/lgpl.txt
 */

package org.milyn.function;

import org.milyn.assertion.AssertArgument;
import org.apache.commons.lang.builder.ToStringBuilder;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * Takes a StringFunction definition and executes it on a string
 *
 * @author [email protected]
 */
public class StringFunctionExecutor {

    private static ConcurrentMap cache = new ConcurrentHashMap();

    public static StringFunctionExecutor getInstance(String functionDefinition) {
        StringFunctionExecutor executor = cache.get(functionDefinition);
        if(executor == null) {
            executor = new StringFunctionExecutor(functionDefinition, StringFunctionDefinitionParser.parse(functionDefinition));

            StringFunctionExecutor existing = cache.putIfAbsent(functionDefinition, executor);

            if(existing != null) {
                executor = existing;
            }
        }

        return executor;
    }

    private List functions;

    private final String functionDefinition;

    private StringFunctionExecutor(String functionDefinition, List functions) {
        this.functionDefinition = functionDefinition;
        this.functions = functions;
    }

    /**
     * Takes a StringFunction definition and executes it on a string
     *
     * @param input The input string
     * @return The result string
     */
    public String execute(String input) {
        AssertArgument.isNotNull(input, "input");

        for(StringFunction function : functions) {
            input = function.execute(input);
        }

        return input;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this)
                    .append("functionDefinition", functionDefinition)
                    .toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy