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

com.arosbio.commons.StringUtils Maven / Gradle / Ivy

Go to download

Conformal AI package, including all data IO, transformations, machine learning models and predictor classes. Without inclusion of chemistry-dependent code.

There is a newer version: 2.0.0
Show newest version
/*
 * Copyright (C) Aros Bio AB.
 *
 * CPSign is an Open Source Software that is dual licensed to allow you to choose a license that best suits your requirements:
 *
 * 1) GPLv3 (GNU General Public License Version 3) with Additional Terms, including an attribution clause as well as a limitation to use the software for commercial purposes.
 *
 * 2) CPSign Proprietary License that allows you to use CPSign for commercial activities, such as in a revenue-generating operation or environment, or integrate CPSign in your proprietary software without worrying about disclosing the source code of your proprietary software, which is required if you choose to use the software under GPLv3 license. See arosbio.com/cpsign/commercial-license for details.
 */
package com.arosbio.commons;

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

import org.apache.commons.text.CaseUtils;
import org.apache.commons.text.WordUtils;

public class StringUtils {

	private static final String NEW_LINE_SEP = System.lineSeparator();

	private static final String NL_REGEX = "\\R|%n";
	
	public static String wrap(String txt, int width) {
		return wrap(txt,width,null);
	}
	
	public static String wrap(String txt, int totalWidth, String newLineStart) {
		return wrap(txt, totalWidth, newLineStart, true);
	}
	
	public static String wrap(final String txt, int totalWidth, final String newLineStart, boolean applyToFirst) {
		return wrap(txt, totalWidth, newLineStart, applyToFirst ? newLineStart : "");
	}
	
	public static String wrap(final String txt, int totalWidth, final String newLineStart, final String beginFirstLine) {
		// Handle the input text
		if (txt == null || txt.isEmpty())
			return "";
		if (totalWidth < 10)
			return txt;
		
		// Handle the new-line-text
		int indentW = 0;
		String indent = null, indentToWordUtils = ' ' +NEW_LINE_SEP;
		boolean containsNL = false;
		if (newLineStart == null) {
			indent = "";
		} else {
			indent = newLineStart; //.replaceAll("\t", TAB);
			String[] splits = indent.split(NL_REGEX);
			if (splits.length>=2) {
				// Only the width of the last thing actually matters
				indentW = splits[splits.length-1].length();
				containsNL = true;
				indentToWordUtils = ' ' +newLineStart;
			} else {
				indentW = indent.length();
				indentToWordUtils = ' '+NEW_LINE_SEP + indent;
			}
		}

		String[] lines = txt.split(NL_REGEX);
		StringBuilder sb = new StringBuilder();
		
		// Special-treat first index in the array
		if (beginFirstLine != null && !beginFirstLine.isEmpty()) {
			sb.append(beginFirstLine);
			sb.append(WordUtils.wrap(lines[0], totalWidth-Math.max(indentW, beginFirstLine.length()), indentToWordUtils,false));
		} else {
			sb.append(WordUtils.wrap(lines[0], totalWidth-indentW, indentToWordUtils,false));
		}
		for (int i=1; i Logistic Regression)
		String[] camelCaseQueries = withBlanks.split("(?1)
			return txt+"s";
		return txt;
	}

	public static String joinCollection(String delimiter, Collection args) {
		List lst = new ArrayList<>(args.size());
		for (Object o : args){
			lst.add(o);
		}
		return join(delimiter,lst);
	}

	public static String join(String delimiter, List args) {
		StringBuilder builder = new StringBuilder();

		for (int i = 0; i < args.size(); i++) {
			builder.append(args.get(i));

			if (i + 1 < args.size())
				builder.append(delimiter);
		}
		return builder.toString();
	}

	public static String join(String delimiter, Object[] args) {
		StringBuilder builder = new StringBuilder();

		for (int i = 0; i < args.length; i++) {
			builder.append(args[i]);

			if (i + 1 < args.length)
				builder.append(delimiter);
		}
		return builder.toString();
	}

	public static String join(String delimiter, int[] args) {
		StringBuilder builder = new StringBuilder();

		for (int i = 0; i < args.length; i++) {
			builder.append(args[i]);

			if (i + 1 < args.length)
				builder.append(delimiter);
		}
		return builder.toString();
	}

	public static String join(String delimiter, double[] args) {
		StringBuilder builder = new StringBuilder();

		for (int i = 0; i < args.length; i++) {
			builder.append(args[i]);

			if (i + 1 < args.length)
				builder.append(delimiter);
		}
		return builder.toString();
	}

	public static String stripQuotesAndEscape(String input) {
		String trimmed = input.trim();
		if (trimmed.startsWith("\"") && trimmed.endsWith("\"")) {
			trimmed = trimmed.substring(1, trimmed.length()-1);
		}
		else if (trimmed.startsWith("\\")) {
			trimmed = trimmed.substring(1);
		}
		return trimmed;
	}
	
	public static String quoteEscapes(String input) {
		if (input == null || input.isEmpty())
			return input;
		return input.replaceAll("\t", "\\\\t").replaceAll("\n", "\\\\n").replaceAll("\r", "\\\\r");
	}

	public static String toStringNoBrackets(List list) {
		if (list==null || list.isEmpty())
			return "";
		StringBuilder sb = new StringBuilder();
		for (int i=0;i