001/** 002 * Copyright (c) 2012, 2014, Credit Suisse (Anatole Tresch), Werner Keil and others by the @author tag. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 005 * use this file except in compliance with the License. You may obtain a copy of 006 * the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 013 * License for the specific language governing permissions and limitations under 014 * the License. 015 */ 016package org.javamoney.moneta.internal; 017 018import java.math.RoundingMode; 019 020import javax.money.CurrencyUnit; 021import javax.money.MonetaryContext; 022import javax.money.MonetaryContextBuilder; 023import javax.money.NumberValue; 024 025import org.javamoney.moneta.Money; 026import org.javamoney.moneta.spi.AbstractAmountFactory; 027 028/** 029 * Implementation of {@link javax.money.MonetaryAmountFactory} creating instances of {@link Money}. 030 * 031 * @author Anatole Tresch 032 */ 033public class MoneyAmountFactory extends AbstractAmountFactory<Money> { 034 035 static final MonetaryContext DEFAULT_CONTEXT = MonetaryContextBuilder.create( 036 Money.class).set(64) 037 .setMaxScale(63).set(RoundingMode.HALF_EVEN).build(); 038 static final MonetaryContext MAX_CONTEXT = MonetaryContextBuilder.create( 039 Money.class).setPrecision(0) 040 .setMaxScale(-1).set(RoundingMode.HALF_EVEN).build(); 041 042 @Override 043 protected Money create(Number number, CurrencyUnit currency, 044 MonetaryContext monetaryContext) { 045 return Money.of(number, currency, 046 MonetaryContext.from(monetaryContext, Money.class)); 047 } 048 049 @Override 050 public NumberValue getMaxNumber(){ 051 return null; 052 } 053 054 @Override 055 public NumberValue getMinNumber(){ 056 return null; 057 } 058 059 @Override 060 public Class<Money> getAmountType() { 061 return Money.class; 062 } 063 064 @Override 065 protected MonetaryContext loadDefaultMonetaryContext() { 066 return DEFAULT_CONTEXT; 067 } 068 069 @Override 070 protected MonetaryContext loadMaxMonetaryContext() { 071 return MAX_CONTEXT; 072 } 073 074}