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 java.io.Serializable;
021import java.math.BigDecimal;
022
023import javax.validation.constraints.NotNull;
024import javax.validation.constraints.Size;
025import javax.xml.bind.annotation.XmlAccessType;
026import javax.xml.bind.annotation.XmlAccessorType;
027import javax.xml.bind.annotation.XmlAttribute;
028import javax.xml.bind.annotation.XmlType;
029import javax.xml.bind.annotation.XmlValue;
030import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
031import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
032
033import io.konik.jaxb.adapter.QuantityRoundingAdapter;
034import io.konik.zugferd.unece.codes.UnitOfMeasurement;
035
036/**
037 * = The Quantity
038 * 
039 * Defined by the amount and Unit
040 * 
041 * Units are based on Recommendation N°. 20 - Codes for Units of Measure Used in International Trade
042 */
043@XmlAccessorType(XmlAccessType.FIELD)
044@XmlType(name = "QuantityType", propOrder = { "value" })
045public class Quantity implements Serializable {
046
047   @XmlValue
048   @XmlJavaTypeAdapter(QuantityRoundingAdapter.class)
049   private BigDecimal value;
050
051   @XmlAttribute(name = "unitCode")
052   @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
053   private String unitCode;
054
055   /** Instantiates a new quantity. */
056   public Quantity() {
057   }
058
059   /**
060    * Instantiates a new quantity.
061    *
062    * @param value the value
063    * @param unitCode the unit code
064    */
065   public Quantity(int value, String unitCode) {
066      super();
067      this.value = BigDecimal.valueOf(value);
068      this.unitCode = unitCode;
069   }
070
071   /**
072    * Instantiates a new quantity.
073    * 
074    * @param value the value
075    * @param unitCode the unit code
076    */
077   public Quantity(BigDecimal value, String unitCode) {
078      super();
079      this.value = value;
080      this.unitCode = unitCode;
081   }
082
083   /**
084    * Instantiates a new quantity.
085    *
086    * @param value the value
087    * @param unit the unit
088    */
089   public Quantity(BigDecimal value, UnitOfMeasurement unit) {
090      super();
091      this.value = value;
092      this.unitCode = unit != null ? unit.getCode() : null;
093   }
094
095   /**
096    * Instantiates a new quantity.
097    *
098    * @param value the integer value
099    * @param unit the unit
100    */
101   public Quantity(int value, UnitOfMeasurement unit) {
102      super();
103      this.value = BigDecimal.valueOf(value);
104      this.unitCode = unit.getCode();
105   }
106
107   /**
108    * Gets the value.
109    * 
110    * @return the value
111    */
112   @NotNull
113   public BigDecimal getValue() {
114      return value;
115   }
116
117   /**
118    * Sets the value.
119    *
120    * @param value the new value
121    * @return the quantity
122    */
123   public Quantity setValue(BigDecimal value) {
124      this.value = value;
125      return this;
126   }
127
128   /**
129    * Gets the unit.
130    *
131    * @return the unit or null if unit is not known.
132    */
133   public UnitOfMeasurement getUnit() {
134      return UnitOfMeasurement.getByCode(unitCode);
135   }
136
137   /**
138    * Sets the unit.
139    *
140    * @param unit the new unit
141    */
142   public void setUnit(UnitOfMeasurement unit) {
143      unitCode = unit != null ? unit.getCode() : null;
144   }
145
146   /**
147    * Gets the unit code.
148    * 
149    * @return the unit code
150    */
151   @Size(min = 1, max = 3)
152   public String getUnitCode() {
153      return unitCode;
154   }
155
156   /**
157    * Sets the unit code.
158    * 
159    * @param newUnitCode the new unit code
160    */
161   public void setUnitCode(String newUnitCode) {
162      this.unitCode = newUnitCode;
163   }
164
165}