Java tutorial
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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 candr.yoclip; import candr.yoclip.annotation.Options; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.text.StrBuilder; /** * A collection of static helper functions. */ public final class OptionUtils { private OptionUtils() { // should never be instanced } /** * Combines an array of strings into a single string. Multiple strings are joined together separated by a space * character unless proceeded by an {@link Options#LINE_BREAK Options} line break.<p/> * As an example the string array <code>String[] example = { "Hello", "World!" }</code> will result * in the string <code>"Hello World!"</code>. The string array <code>String[] example = { "Hello\n", * "World!" }</code> will result in the string <code>"Hello${nl}World!"</code> (where ${nl} is the OS * dependent new line character string). * * @param descriptions The strings that will be combined. * @return a string created from the array of strings. */ public static String combine(final String[] descriptions) { if (ArrayUtils.isEmpty(descriptions)) { return StringUtils.EMPTY; } if (descriptions.length == 1) { return descriptions[0]; } final StrBuilder descriptionBuilder = new StrBuilder(descriptions[0]); for (int i = 1; i < descriptions.length; i++) { if (!descriptions[i].startsWith(Options.LINE_BREAK)) { final String prior = StringUtils.isEmpty(descriptions[i - 1]) ? StringUtils.EMPTY : descriptions[i - 1]; if (!prior.endsWith(Options.LINE_BREAK)) { descriptionBuilder.append(' '); } } descriptionBuilder.append(descriptions[i]); } return descriptionBuilder.toString(); } }