org.javamoney.moneta.spi.ConvertNumberValue Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of moneta-bp Show documentation
Show all versions of moneta-bp Show documentation
JSR 354 provides an API for representing, transporting, and performing comprehensive calculations with
Money and Currency.
This module implements JSR 354.
/**
* Copyright (c) 2012, 2014, Credit Suisse (Anatole Tresch), Werner Keil and others by the @author tag.
*
* 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 org.javamoney.moneta.spi;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
/**
* Utility for converting numeric types.
*/
enum ConvertNumberValue {
INSTANCE;
public static T of(Class numberType, Number number) {
return INSTANCE.convert(numberType, number);
}
public static T ofExact(Class numberType, Number number) {
return INSTANCE.convertExact(numberType, number);
}
@SuppressWarnings("unchecked")
public T convert(Class numberType, Number number) {
ConvertNumberValueI converter = convertIMap.get(numberType);
if(converter==null){
throw new IllegalArgumentException("Unsupported numeric type: "
+ numberType);
}
return (T) converter.convert(numberType, number);
}
@SuppressWarnings("unchecked")
public T convertExact(Class numberType, Number number) {
ConvertNumberValueI converter = convertIMap.get(numberType);
if(converter==null){
throw new IllegalArgumentException("Unsupported numeric type: "
+ numberType);
}
return (T) converter.convertExact(numberType, number);
}
interface ConvertNumberValueI {
T convert(Class numberType, Number number);
T convertExact(Class numberType, Number number);
}
static class ConvertNumberValueBigDecimal implements ConvertNumberValueI {
@Override
public BigDecimal convert(Class numberType,
Number number) {
return ConvertBigDecimal.of(number);
}
@Override
public BigDecimal convertExact(Class numberType,
Number number) {
return ConvertBigDecimal.of(number);
}
}
static class ConvertNumberValueBigInteger implements ConvertNumberValueI {
@Override
public BigInteger convert(Class numberType,
Number number) {
return ConvertBigDecimal.of(number).toBigInteger();
}
@Override
public BigInteger convertExact(Class numberType,
Number number) {
return ConvertBigDecimal.of(number).toBigIntegerExact();
}
}
static class ConvertNumberValueDouble implements ConvertNumberValueI {
@Override
public Double convert(Class numberType,
Number number) {
return number.doubleValue();
}
@Override
public Double convertExact(Class numberType,
Number number) {
double d = number.doubleValue();
if (d == Double.NEGATIVE_INFINITY || d == Double.POSITIVE_INFINITY) {
throw new ArithmeticException(
"Value not exact mappable to double: " + number);
}
return d;
}
}
static class ConvertNumberValueFloat implements ConvertNumberValueI {
@Override
public Float convert(Class numberType,
Number number) {
return number.floatValue();
}
@Override
public Float convertExact(Class numberType,
Number number) {
float f = number.floatValue();
if (f == Float.NEGATIVE_INFINITY || f == Float.POSITIVE_INFINITY) {
throw new ArithmeticException(
"Value not exact mappable to float: " + number);
}
return f;
}
}
static class ConvertNumberValueLong implements ConvertNumberValueI {
@Override
public Long convert(Class numberType,
Number number) {
return number.longValue();
}
@Override
public Long convertExact(Class numberType,
Number number) {
return ConvertBigDecimal.of(number).longValueExact();
}
}
static class ConvertNumberValueInteger implements ConvertNumberValueI {
@Override
public Integer convert(Class numberType,
Number number) {
return number.intValue();
}
@Override
public Integer convertExact(Class numberType,
Number number) {
return ConvertBigDecimal.of(number).intValueExact();
}
}
static class ConvertNumberValueShort implements ConvertNumberValueI {
@Override
public Short convert(Class numberType,
Number number) {
return number.shortValue();
}
@Override
public Short convertExact(Class numberType,
Number number) {
return ConvertBigDecimal.of(number).shortValueExact();
}
}
static class ConvertNumberValueByte implements ConvertNumberValueI {
@Override
public Byte convert(Class numberType,
Number number) {
return number.byteValue();
}
@Override
public Byte convertExact(Class numberType,
Number number) {
return ConvertBigDecimal.of(number).byteValueExact();
}
}
static class ConvertNumberValueAtomicInteger implements ConvertNumberValueI {
@Override
public AtomicInteger convert(Class numberType,
Number number) {
return new AtomicInteger(number.intValue());
}
@Override
public AtomicInteger convertExact(
Class numberType, Number number) {
return new AtomicInteger(ConvertBigDecimal.of(
number).intValueExact());
}
}
static class ConvertNumberValueAtomicLong implements ConvertNumberValueI {
@Override
public AtomicLong convert(Class numberType,
Number number) {
return new AtomicLong(number.longValue());
}
@Override
public AtomicLong convertExact(Class numberType,
Number number) {
return new AtomicLong(ConvertBigDecimal.of(number).longValueExact());
}
}
@SuppressWarnings("rawtypes")
private final Map, ConvertNumberValueI> convertIMap = new HashMap<>();
{
convertIMap.put(BigDecimal.class, new ConvertNumberValueBigDecimal());
convertIMap.put(BigInteger.class, new ConvertNumberValueBigInteger());
convertIMap.put(Float.class, new ConvertNumberValueFloat());
convertIMap.put(Double.class, new ConvertNumberValueDouble());
convertIMap.put(Long.class, new ConvertNumberValueLong());
convertIMap.put(Integer.class, new ConvertNumberValueInteger());
convertIMap.put(Short.class, new ConvertNumberValueShort());
convertIMap.put(Byte.class, new ConvertNumberValueByte());
convertIMap.put(AtomicInteger.class, new ConvertNumberValueAtomicInteger());
convertIMap.put(AtomicLong.class, new ConvertNumberValueAtomicLong());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy