001/*
002 * Copyright (C) 2014 konik.io
003 *
004 * This file is part of Konik library.
005 *
006 * Konik library is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU Affero General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * Konik library is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU Affero General Public License for more details.
015 *
016 * You should have received a copy of the GNU Affero General Public License
017 * along with Konik library.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package io.konik.zugferd.unqualified;
020
021import io.konik.zugferd.unece.codes.UnitOfMeasurement;
022
023import java.math.BigDecimal;
024
025import javax.validation.constraints.Size;
026import javax.xml.bind.annotation.XmlAccessType;
027import javax.xml.bind.annotation.XmlAccessorType;
028import javax.xml.bind.annotation.XmlAttribute;
029import javax.xml.bind.annotation.XmlType;
030import javax.xml.bind.annotation.XmlValue;
031import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
032import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
033
034/**
035 * = The Quantity 
036 * 
037 * Defined by the amount and Unit 
038 * 
039 * Units are based on Recommendation N°. 20 - Codes for Units of Measure Used in International Trade
040 */
041@XmlAccessorType(XmlAccessType.FIELD)
042@XmlType(name = "QuantityType", propOrder = { "value" })
043public class Quantity {
044
045   @XmlValue
046   private BigDecimal value;
047
048   @XmlAttribute(name = "unitCode")
049   @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
050   @Size(min = 1, max = 3)
051   private String unitCode;
052
053   /** Instantiates a new quantity. */
054   public Quantity() {}
055
056   /**
057    * Instantiates a new quantity.
058    * 
059    * @param value the value
060    * @param unitCode the unit code
061    */
062   public Quantity(BigDecimal value, String unitCode) {
063      super();
064      this.value = value;
065      this.unitCode = unitCode;
066   }
067
068   /**
069    * Instantiates a new quantity.
070    *
071    * @param value the value
072    * @param unit the unit
073    */
074   public Quantity(BigDecimal value, UnitOfMeasurement unit) {
075      super();
076      this.value = value;
077      this.unitCode = unit.getCode();
078   }
079   
080   /**
081    * Gets the value.
082    * 
083    * @return the value
084    */
085   public BigDecimal getValue() {
086      return value;
087   }
088
089   /**
090    * Sets the value.
091    * 
092    * @param value the new value
093    */
094   public void setValue(BigDecimal value) {
095      this.value = value;
096   }
097   
098   /**
099    * Gets the unit.
100    *
101    * @return the unit or null if unit is not known.
102    */
103   public UnitOfMeasurement getUnit(){
104      return UnitOfMeasurement.getByCode(unitCode);
105   }
106   
107   /**
108    * Sets the unit.
109    *
110    * @param unit the new unit
111    */
112   public void setUnit(UnitOfMeasurement unit){
113      unitCode = unit.getCode();
114   }
115
116   /**
117    * Gets the unit code.
118    * 
119    * @return the unit code
120    */
121   public String getUnitCode() {
122      return unitCode;
123   }
124
125   /**
126    * Sets the unit code.
127    * 
128    * @param newUnitCode the new unit code
129    */
130   public void setUnitCode(String newUnitCode) {
131      this.unitCode = newUnitCode;
132   }
133
134}