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

org.batoo.common.util.BatooUtils Maven / Gradle / Ivy

/*
 * Copyright (c) 2012 - Batoo Software ve Consultancy Ltd.
 * 
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.batoo.common.util;

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

import org.apache.commons.lang.StringUtils;

import com.google.common.collect.Lists;

/**
 * 
 * @author hceylan
 * @since $version
 */
public class BatooUtils {

	/**
	 * Returns the acronym of the name
	 * 
	 * @param name
	 *            the name
	 * @return the acronym
	 * 
	 * @since $version
	 */
	public static String acronym(String name) {
		final StringBuilder builder = new StringBuilder();
		for (int i = 0; i < name.length(); i++) {
			if (Character.isUpperCase(name.charAt(i))) {
				builder.append(name.charAt(i));
			}
		}

		if (builder.length() == 0) {
			builder.append(name.charAt(0));
		}

		return builder.toString();
	}

	/**
	 * Adds all the elements in the source to target.
	 * 
	 * @param source
	 *            the source collection
	 * @param target
	 *            the destination collection
	 * @param 
	 *            the type of the collections
	 * 
	 * @since $version
	 */
	public static  void addAll(Collection source, Collection target) {
		if (source instanceof List) {
			final List list = (List) source;
			for (int i = 0; i < list.size(); i++) {
				target.add(list.get(i));
			}
		}
		else {
			target.addAll(source);
		}
	}

	/**
	 * Indents the string by one tab.
	 * 
	 * @param str
	 *            string to indent
	 * @return the indented string
	 * 
	 * @since $version
	 */
	public static String indent(String str) {
		if (StringUtils.isBlank(str)) {
			return "";
		}

		return "\t" + str.replaceAll("\n", "\n\t");
	}

	/**
	 * Converts the string to lower case.
	 * 
	 * @param string
	 *            the string to convert
	 * @return the converted string
	 * 
	 * @since $version
	 */
	public static String lower(String string) {
		return string != null ? string.toLowerCase() : null;
	}

	/**
	 * @param a
	 *            the collection a
	 * @param b
	 *            the collection b
	 * @return the subtracted collection
	 * @param 
	 *            the type of the collecions
	 * 
	 * @since $version
	 */
	public static  List subtract(final Collection a, final Collection b) {
		final List list = Lists.newArrayList(a);

		for (final Object element : b) {
			list.remove(element);
		}

		return list;
	}

	/**
	 * Indents the string by one tab.
	 * 
	 * @param str
	 *            string to indent
	 * @return the indented string
	 * 
	 * @since $version
	 */
	public static String tree(String str) {
		if (StringUtils.isBlank(str)) {
			return "";
		}

		return "|-->" + str.replaceAll("\n", "\n|   ");
	}

	/**
	 * Converts the string to upper case.
	 * 
	 * @param string
	 *            the string to convert
	 * @return the converted string
	 * 
	 * @since $version
	 */
	public static String upper(String string) {
		return string != null ? string.toUpperCase() : null;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy