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

org.solovyev.common.text.ValueOfFormatter Maven / Gradle / Ivy

/*
 * Copyright (c) 2009-2011. Created by serso aka se.solovyev.
 * For more information, please, contact [email protected]
 * or visit http://se.solovyev.org
 */

package org.solovyev.common.text;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * User: serso
 * Date: 9/20/11
 * Time: 10:52 PM
 */
public class ValueOfFormatter implements Formatter {

    /*
    **********************************************************************
    *
    *                           STATIC
    *
    **********************************************************************
    */

    @NotNull
    private static final ValueOfFormatter notNullFormatter = new ValueOfFormatter(false);

    @NotNull
    private static final ValueOfFormatter nullableFormatter = new ValueOfFormatter(true);

    @NotNull
    public static  ValueOfFormatter getNotNullFormatter() {
        return (ValueOfFormatter) notNullFormatter;
    }

    @NotNull
    public static  ValueOfFormatter getNullableFormatter() {
        return (ValueOfFormatter) nullableFormatter;
    }

    /*
    **********************************************************************
    *
    *
    *
    **********************************************************************
    */

	private final boolean processNulls;

    /**
     *  Use org.solovyev.common.text.ValueOfFormatter#getNullableFormatter() or org.solovyev.common.text.ValueOfFormatter#getNotNullFormatter() instead
     */
    @Deprecated
	public ValueOfFormatter() {
		this(false);
	}

	public ValueOfFormatter(boolean processNulls) {
		this.processNulls = processNulls;
	}


	@Override
	public String formatValue(@Nullable T t) throws IllegalArgumentException {
		return t == null ? (processNulls ? String.valueOf(t) : null) : String.valueOf(t);
	}
}