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

org.mentalog.util.StringBuilderUtils Maven / Gradle / Ivy

There is a newer version: 2.1.2
Show newest version
package org.mentalog.util;

import org.mentalog.Log;

/**
 * This code is NOT thread-safe at all.
 * 
 * You can use to_sb to bypass autoboxing and avoid the object creation. So intead of:
 * 
 * Info.log(myInteger);
 * 
 * you would do:
 * 
 * Info.log(to_sb(myInteger));
 * 
 * NOTE: If you want to use this from multiple threads you MUST synchronize around to_sb:
 * 
 * synchronized(Info) { Info.log(to_sb(myInteger)); }
 * 
 * @author Sergio Oliveira Jr.
 */
public class StringBuilderUtils {

	private static volatile boolean isSynchronized;
	private static ThreadLocal threadLocal;
	private static final StringBuilderPool singlePool = new StringBuilderPool();
	
	static {
		threadLocal = new ThreadLocal() {
			@Override
			protected StringBuilderPool initialValue() {
				return new StringBuilderPool();
			}
		};

		isSynchronized = Log.isSynchronized();
	}
	
	private final static StringBuilderPool getPool() {
		if (isSynchronized) {
			return threadLocal.get();
		} else {
			return singlePool;
		}
	}

	public final static StringBuilder to_sb(int x) {
		return getPool().to_sb(x);
	}
	
	public final static StringBuilder to_sb(float x) {
		return getPool().to_sb(x);
	}
	
	public final static StringBuilder to_sb(short x) {
		return getPool().to_sb(x);
	}
	
	public final static StringBuilder to_sb(byte x) {
		return getPool().to_sb(x);
	}
	
	public final static StringBuilder to_sb(char x) {
		return getPool().to_sb(x);
	}
	
	public final static StringBuilder to_sb(boolean x) {
		return getPool().to_sb(x);
	}
	
	public final static StringBuilder to_sb(long x) {
		return getPool().to_sb(x);
	}
	
	public final static StringBuilder to_sb(double x) {
		return getPool().to_sb(x);
	}

	public final static void reset() {
		getPool().reset();
	}

	public final static int getPointer() {
		return getPool().getPointer();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy