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   @NotNull
048   @XmlJavaTypeAdapter(QuantityRoundingAdapter.class)
049   private BigDecimal value;
050
051   @XmlAttribute(name = "unitCode")
052   @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
053   @Size(min = 1, max = 3)
054   private String unitCode;
055
056   /** Instantiates a new quantity. */
057   public Quantity() {
058   }
059
060   /**
061    * Instantiates a new quantity.
062    *
063    * @param value the value
064    * @param unitCode the unit code
065    */
066   public Quantity(int value, String unitCode) {
067      super();
068      this.value = BigDecimal.valueOf(value);
069      this.unitCode = unitCode;
070   }
071
072   /**
073    * Instantiates a new quantity.
074    * 
075    * @param value the value
076    * @param unitCode the unit code
077    */
078   public Quantity(BigDecimal value, String unitCode) {
079      super();
080      this.value = value;
081      this.unitCode = unitCode;
082   }
083
084   /**
085    * Instantiates a new quantity.
086    *
087    * @param value the value
088    * @param unit the unit
089    */
090   public Quantity(BigDecimal value, UnitOfMeasurement unit) {
091      super();
092      this.value = value;
093      this.unitCode = unit.getCode();
094   }
095
096   /**
097    * Instantiates a new quantity.
098    *
099    * @param value the integer value
100    * @param unit the unit
101    */
102   public Quantity(int value, UnitOfMeasurement unit) {
103      super();
104      this.value = BigDecimal.valueOf(value);
105      this.unitCode = unit.getCode();
106   }
107
108   /**
109    * Gets the value.
110    * 
111    * @return the value
112    */
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.getCode();
144   }
145
146   /**
147    * Gets the unit code.
148    * 
149    * @return the unit code
150    */
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}