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

org.solovyev.common.text.NumberMapper 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;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: serso
 * Date: 9/26/11
 * Time: 11:10 PM
 */
public class NumberMapper implements Mapper{

	@NotNull
	private final Formatter formatter = ValueOfFormatter.getNotNullFormatter();

	@NotNull
	private final Parser parser;

    /**
     * Use org.solovyev.common.text.NumberMapper#getMapper(java.lang.Class) instead
     * @param clazz class representing parsed object
     */
    @Deprecated
	public NumberMapper(@NotNull Class clazz) {
		this.parser = NumberParser.getParser(clazz);
	}

	@Override
	public String formatValue(@Nullable N value) throws IllegalArgumentException {
		return formatter.formatValue(value);
	}

	@Override
	public N parseValue(@Nullable String value) throws IllegalArgumentException {
		return this.parser.parseValue(value);
	}

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

    private static final List> supportedClasses = NumberParser.supportedClasses;

    private static final Map, Mapper> mappers = new HashMap, Mapper>(supportedClasses.size());

    static {
        for (Class supportedClass : supportedClasses) {
            mappers.put(supportedClass, new NumberMapper(supportedClass));
        }
    }

    @NotNull
    public static  Mapper getMapper(@NotNull Class clazz) {
        assert supportedClasses.contains(clazz) : "Class " + clazz + " is not supported by " + NumberMapper.class;
        return (Mapper) mappers.get(clazz);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy