001/*
002 * Copyright (c) 2012, 2013, Werner Keil, Credit Suisse (Anatole Tresch). Licensed under the Apache
003 * License, Version 2.0 (the "License"); you may not use this file except in compliance with the
004 * License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
005 * Unless required by applicable law or agreed to in writing, software distributed under the License
006 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
007 * or implied. See the License for the specific language governing permissions and limitations under
008 * the License. Contributors: Anatole Tresch - initial version.
009 */
010package org.javamoney.tck.tests.internal;
011
012import javax.money.NumberValue;
013import java.math.BigDecimal;
014import java.math.BigInteger;
015import java.math.MathContext;
016import java.util.Objects;
017
018/**
019 * Created by Anatole on 26.04.2014.
020 */
021public final class TestNumberValue extends NumberValue{
022    private static final long serialVersionUID = 1L;
023
024    private BigDecimal value;
025
026    public TestNumberValue(Number value){
027        Objects.requireNonNull(value);
028        this.value = new BigDecimal(String.valueOf(value));
029    }
030
031    @Override
032    public long longValue(){
033        return value.longValue();
034    }
035
036    @Override
037    public int intValue(){
038        return value.intValue();
039    }
040
041    @Override
042    public float floatValue(){
043        return value.floatValue();
044    }
045
046    @Override
047    public double doubleValue(){
048        return value.doubleValue();
049    }
050
051    @Override
052    public <T extends Number> T numberValueExact(Class<T> numberType){
053        return null;
054    }
055
056    @Override
057    public long getAmountFractionNumerator(){
058        return 0;
059    }
060
061    @Override
062    public long getAmountFractionDenominator(){
063        return 0;
064    }
065
066    @Override
067    public <T extends Number> T numberValue(Class<T> numberType){
068        if(numberType.equals(Integer.class)){
069            return (T) Integer.valueOf(value.intValue());
070        }
071        if(numberType.equals(BigInteger.class)){
072            return (T) BigInteger.valueOf(value.intValue());
073        }
074        if(numberType.equals(BigDecimal.class)){
075            return (T) BigDecimal.valueOf(value.doubleValue());
076        }
077        throw new UnsupportedOperationException(numberType.getCanonicalName());
078    }
079
080    @Override
081    public NumberValue round(MathContext mathContext) {
082        return new TestNumberValue(this.value.round(mathContext));
083    }
084
085    @Override
086    public long longValueExact(){
087        return value.longValue();
088    }
089
090    @Override
091    public int intValueExact(){
092        return value.intValue();
093    }
094
095    @Override
096    public int getScale(){
097        return value.scale();
098    }
099
100    @Override
101    public int getPrecision(){
102        return value.precision();
103    }
104
105    @Override
106    public Class<?> getNumberType(){
107        return BigDecimal.class;
108    }
109
110    @Override
111    public double doubleValueExact(){
112        return value.doubleValue();
113    }
114
115    @Override
116    public String toString(){
117        return this.value.toString();
118    }
119}