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