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

edu.emory.mathcs.nlp.common.util.Joiner Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2015, Emory University
 * 
 * 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 edu.emory.mathcs.nlp.common.util;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.StringJoiner;
import java.util.function.Function;
import java.util.stream.Collectors;

import edu.emory.mathcs.nlp.common.collection.tuple.ObjectDoublePair;
import edu.emory.mathcs.nlp.common.constant.StringConst;

/**
 * @author Jinho D. Choi ({@code [email protected]})
 */
public class Joiner
{
	static public String join(Collection collection, String delim)
	{
		return join(collection, delim, p -> p.toString());
	}
	
	static public String join(Collection collection, String delim, Function f)
	{
		return collection.stream().map(f).collect(Collectors.joining(delim));
	}
	
	
	
	static public String join(T[] array, String delim, int beginIndex, int endIndex, Function f)
	{
		if (endIndex <= beginIndex) return StringConst.EMPTY;
		StringJoiner build = new StringJoiner(delim);
		
		for (int i=beginIndex; iString join(T[] array, String delim, int beginIndex, int endIndex)
	{
		return join(array, delim, beginIndex, endIndex, n -> n.toString());
	}
	
	static public String join(T[] array, String delim, int beginIndex)
	{
		return join(array, delim, beginIndex, array.length, n -> n.toString());
	}
	
	static public String join(T[] array, String delim)
	{
		return join(array, delim, 0, array.length);
	}
	
	static public String join(List list, String delim, int beginIndex, int endIndex, Function f)
	{
		if (endIndex - beginIndex == 0) return StringConst.EMPTY;
		StringJoiner build = new StringJoiner(delim);
		
		for (int i=beginIndex; iString join(List list, String delim, int beginIndex, int endIndex)
	{
		return join(list, delim, beginIndex, endIndex, T::toString);
	}
	
	static public >String join(List list, String delim, boolean sort)
	{
		if (sort) Collections.sort(list);
		return join(list, delim, 0, list.size());
	}
	
	static public String joinObject(List> ps, String delim)
	{
		StringJoiner build = new StringJoiner(delim);
		
		for (ObjectDoublePair p : ps)
			build.add(p.o.toString());
		
		return build.toString();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy