001/* Copyright (C) 2014 konik.io
002 *
003 * This file is part of the Konik library.
004 *
005 * The Konik library is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * The Konik library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with the Konik library. If not, see <http://www.gnu.org/licenses/>.
017 */
018package io.konik.zugferd.unqualified;
019
020import com.neovisionaries.i18n.CurrencyCode;
021
022import javax.validation.constraints.NotNull;
023import javax.xml.bind.annotation.XmlAttribute;
024import javax.xml.bind.annotation.XmlType;
025import javax.xml.bind.annotation.XmlValue;
026import java.io.Serializable;
027import java.math.BigDecimal;
028
029/**
030 * = The Monetary Amount.
031 * 
032 * A number of monetary units specified by a currency where the currency is explicit or implied.
033 */
034@XmlType(name = "AmountType", propOrder = { "value" })
035public class Amount implements Serializable {
036
037   @NotNull
038   @XmlValue
039   private BigDecimal value;
040
041   @NotNull
042   @XmlAttribute(name = "currencyID")
043   private CurrencyCode currency;
044
045   Amount() {
046   }
047
048   /**
049    * Instantiates a new amount.
050    *
051    * @param value the monetary value as a long
052    * @param currency the currency code
053    * 
054    */
055   public Amount(long value, CurrencyCode currency) {
056      super();
057      this.value = BigDecimal.valueOf(value);
058      this.currency = currency;
059   }
060
061   /**
062    * Instantiates a new amount.
063    *
064    * @param value the monetary value as a string
065    * @param currency the currency code
066    * @throws NumberFormatException if +value+ is not a valid representation of a +BigDecimal+.
067    */
068   public Amount(String value, CurrencyCode currency) {
069      super();
070      this.value = new BigDecimal(value);
071      this.currency = currency;
072   }
073
074   /**
075    * Instantiates a new amount.
076    *
077    * @param value the value
078    * @param currency the currency code
079    */
080   public Amount(BigDecimal value, CurrencyCode currency) {
081      super();
082      this.value = value;
083      this.currency = currency;
084   }
085
086   /**
087    * Gets the amount value.
088    * 
089    * @return the value
090    */
091   public BigDecimal getValue() {
092      return value;
093   }
094
095   /**
096    * Sets the amount value.
097    *
098    * @param value the new value
099    * @return the amount
100    */
101   public Amount setValue(BigDecimal value) {
102      this.value = value;
103      return this;
104   }
105
106   /**
107    * Gets the currency code.
108    *
109    * @return the +ISO 4217 3A+ currency code
110    */
111   public CurrencyCode getCurrency() {
112      return currency;
113   }
114
115   /**
116    * Sets the currency code.
117    *
118    * @param currency the new +ISO 4217 3A+ currency code
119    * @return the amount
120    */
121   public Amount setCurrency(CurrencyCode currency) {
122      this.currency = currency;
123      return this;
124   }
125   
126   @Override
127   public String toString() {    
128      return getValue() + " " + getCurrency();
129   }
130
131}