Java tutorial
/* * Copyright 2013 htfv (Aliaksei Lahachou) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.github.htfv.utils.el.internal; import java.lang.reflect.Method; import org.apache.commons.lang3.StringUtils; public class StringFunctions { /** * Caches reference to the {@link StringFunctions#concat} method. * * @author htfv (Aliaksei Lahachou) */ public static class Concat { /** * Reference to the {@link StringFunctions#concat} method. */ public static final Method INSTANCE = getMethod("concat", String[].class); } /** * Caches reference to the {@link StringFunctions#join} method. * * @author htfv (Aliaksei Lahachou) */ public static class Join { /** * Reference to the {@link StringFunctions#join} method. */ public static final Method INSTANCE = getMethod("join", String.class, String[].class); } /** * Concatenates array of strings without a separator, ignoring any empty and * {@code null} strings. * * @param strings * the array of strings to join. * * @return the joined string. */ public static String concat(final String... strings) { final StringBuilder joinedString = new StringBuilder(); for (int i = 0; i < strings.length; i += 1) { final String string = strings[i]; if (StringUtils.isNotEmpty(string)) { joinedString.append(string); } } return joinedString.toString(); } /** * Returns a {@link Method} object which reflects a public method of the * {@link DefaultFunctionMapper} class. * * @param name * the name of the method. * @param parameterTypes * the list of parameters. * * @return the {@code Method} object that matches the specified {@code name} * and {@code parameterTypes} or {@code null} if method was not * found. */ private static Method getMethod(final String name, final Class<?>... parameterTypes) { try { return StringFunctions.class.getMethod(name, parameterTypes); } catch (final NoSuchMethodException e) { return null; } } /** * Joins array of strings with a separator, ignoring any empty and * {@code null} strings. * * @param separator * the separator string to use. * @param strings * the array of strings to join. * * @return the joined string. */ public static String join(final String separator, final String... strings) { final StringBuilder joinedString = new StringBuilder(); final int stringCount = strings.length; int i = 0; for (; i < stringCount; i += 1) { final String string = strings[i]; if (StringUtils.isNotEmpty(string)) { joinedString.append(string); break; } } for (i += 1; i < stringCount; i += 1) { final String string = strings[i]; if (StringUtils.isNotEmpty(string)) { joinedString.append(separator).append(string); } } return joinedString.toString(); } }