StringBuilder.java

package org.microspace.table.query.sql;

import java.util.List;

/**
 * A Smart StringBuilder.
 * 
 * @author Gaspar Sinai - {@literal gaspar.sinai@microspace.org}
 * @version 2017-07-06
 */
public class StringBuilder {
	
	StringBuffer buffer;
	
	public StringBuilder() {
		buffer = new StringBuffer();
	}
	
	public StringBuilder append (String value) {
		buffer.append(value);
		return this;
	}
	public StringBuilder append(StringBuilder in) {
		
		buffer.append(in.buffer);
		return this;
	}
	public int length () {
		return buffer.length();
	}
	
	@Override
	public String toString () {
		return buffer.toString();
	}
	
	public void join (String separator, String... values) {
		if (values.length == 0) {
			return;
		}
		StringBuffer ret = new StringBuffer();
		for (int i=0; i<values.length; i++) {
			if (ret.length() > 0 && values[i].length() != 0) {
				ret.append(separator);
			}
			ret.append(values[i]);
		}
		buffer.append(ret);
	}
	public void join (String separator, List<String> values) {
		if (values.size() == 0) {
			return;
		}
		StringBuffer ret = new StringBuffer();
		for (String value : values) {
			if (ret.length() > 0 && value.length() != 0) {
				ret.append(separator);
			}
			ret.append(value);
		}
		buffer.append(ret);
	}
	public void prefix (String prefix, String... values) {
		if (values.length == 0) {
			return;
		}
		StringBuffer ret = new StringBuffer();
		for (String value : values) {
			ret.append(value);
		}
		if (ret.length() == 0) return;
		if (buffer.length() > 0) {
			buffer.append(prefix);
		}
		buffer.append(ret);
	}
	
	public void prefix (String prefix, List<String> values) {
		if (values.size() == 0) {
			return;
		}
		StringBuffer ret = new StringBuffer();
		for (String value : values) {
			ret.append(value);
		}
		if (ret.length() == 0) return;
		
		if (buffer.length() > 0) {
			buffer.append(prefix);
		}
		buffer.append(ret);
	}
}