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

org.openforis.commons.lang.Strings Maven / Gradle / Ivy

The newest version!
package org.openforis.commons.lang;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * 
 * @author S. Ricci
 *
 */
public class Strings {

	public static String firstNotBlank(String... values) {
		for (String value : values) {
			if (StringUtils.isNotBlank(value)) {
				return value;
			}
		}
		return null;
	}
	
	public static > String joinNotBlank(C values, String separator) {
		List notBlankValues = filterNotBlank(values);
		String result = StringUtils.join(notBlankValues, separator);
		return result;
	}
	
	public static String textToHtml(String text) {
		String result;
		result = StringUtils.replace(text, "\n", "
"); result = StringUtils.replace(result, "\t", "    "); return result; } public static String htmlToText(String html) { String result = StringEscapeUtils.unescapeHtml4(html); result = result.replaceAll("
", "\n"); return result; } public static String[] filterNotBlank(String... texts) { List result = filterNotBlank(Arrays.asList(texts)); return result.toArray(new String[result.size()]); } public static void append(StringBuilder sb, String value, int count) { for (int i = 0; i < count; i++) { sb.append(value); } } public static List prependToList(List values, String prefix) { if (StringUtils.isBlank(prefix)) { return values; } else { List result = new ArrayList(values.size()); for (String val : values) { result.add(prefix + val); } return result; } } private static List filterNotBlank(Collection list) { return filterNotBlank(new ArrayList(list)); } private static List filterNotBlank(List list) { List result = new ArrayList(list.size()); for (String value : list) { if (StringUtils.isNotBlank(value)) { result.add(value); } } return result; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy