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

brooklyn.util.text.StringFunctions Maven / Gradle / Ivy

Go to download

Utility classes and methods developed for Brooklyn but not dependendent on Brooklyn or much else

There is a newer version: 0.7.0-M1
Show newest version
package brooklyn.util.text;

import javax.annotation.Nullable;

import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Preconditions;

public class StringFunctions {

    public static Function append(final String suffix) {
        return new Function() {
            @Override
            @Nullable
            public String apply(@Nullable String input) {
                if (input==null) return null;
                return input + suffix;
            }
        };
    }

    /** given e.g. "hello %s" returns a function which will insert a string into that pattern */
    public static Function formatter(final String pattern) {
        return new Function() {
            public String apply(@Nullable Object input) {
                return String.format(pattern, input);
            }
        };
    }

    /** given e.g. "hello %s %s" returns a function which will insert an array of two strings into that pattern */
    public static Function formatterForArray(final String pattern) {
        return new Function() {
            public String apply(@Nullable Object[] input) {
                return String.format(pattern, input);
            }
        };
    }

    /** joins the given objects in a collection as a toString with the given separator */
    public static Function, String> joiner(final String separator) {
        return new Function, String>() {
            public String apply(@Nullable Iterable input) {
                return Strings.join(input, separator);
            }
        };
    }

    /** joins the given objects as a toString with the given separator, but expecting an array of objects, not a collection */
    public static Function joinerForArray(final String separator) {
        return new Function() {
            public String apply(@Nullable Object[] input) {
                return Strings.join(input, separator);
            }
        };
    }

    /** provided here as a convenience; prefer {@link Functions#toStringFunction()} */
    public static Function toStringFunction() {
        return Functions.toStringFunction();
    }

    /** Surrounds an input string with the given prefix and suffix */
    public static Function surround(final String prefix, final String suffix) {
        Preconditions.checkNotNull(prefix);
        Preconditions.checkNotNull(suffix);
        return new Function() {
            @Override
            public String apply(String input) {
                if (input==null) return null;
                return prefix+input+suffix;
            }
        };
    }

    public static Function trim() {
        return new Function() {
            @Override
            public String apply(String input) {
                return input.trim();
            }
        };
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy