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