/*
* Copyright 2013, Arondor
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arondor.common.reflection.reflect.instantiator;
import java.util.HashMap;
import java.util.Map;
public class FastPrimitiveConverter
{
private interface PrimitiveConverter
{
Object convert(String value);
}
private final Map<String, PrimitiveConverter> primitiveConverterMap = new HashMap<String, FastPrimitiveConverter.PrimitiveConverter>();
public FastPrimitiveConverter()
{
primitiveConverterMap.put("java.lang.String", new PrimitiveConverter()
{
public Object convert(String value)
{
return value;
}
});
primitiveConverterMap.put("long", new PrimitiveConverter()
{
public Object convert(String value)
{
return Long.parseLong(value);
}
});
primitiveConverterMap.put("int", new PrimitiveConverter()
{
public Object convert(String value)
{
return Integer.parseInt(value);
}
});
primitiveConverterMap.put("boolean", new PrimitiveConverter()
{
public Object convert(String value)
{
return Boolean.parseBoolean(value);
}
});
primitiveConverterMap.put("float", new PrimitiveConverter()
{
public Object convert(String value)
{
return Float.parseFloat(value);
}
});
}
public Object convert(String value, String primitiveClass)
{
PrimitiveConverter converter = primitiveConverterMap.get(primitiveClass);
if (converter == null)
{
throw new IllegalArgumentException("Not supported : primitiveClass=" + primitiveClass);
}
return converter.convert(value);
}
}