]> git.mxchange.org Git - jproduct-core.git/blobdiff - src/org/mxchange/jproduct/model/product/GenericProduct.java
Continued:
[jproduct-core.git] / src / org / mxchange / jproduct / model / product / GenericProduct.java
index bdbcc0e61fb2dcc26445df9b70404f7cc5da24f1..1c04f3b20a5a17956016e673d48742fe1bf14774 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016, 2017 Roland Häder
+ * Copyright (C) 2016 - 2018 Free Software Foundation
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -16,6 +16,7 @@
  */
 package org.mxchange.jproduct.model.product;
 
+import java.math.BigDecimal;
 import java.text.MessageFormat;
 import java.util.Date;
 import java.util.Objects;
@@ -37,17 +38,21 @@ import javax.persistence.Table;
 import javax.persistence.Temporal;
 import javax.persistence.TemporalType;
 import javax.persistence.Transient;
+import org.apache.commons.lang3.StringUtils;
 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
+import org.mxchange.jcontactsbusiness.model.basicdata.BasicDataUtils;
 import org.mxchange.jcontactsbusiness.model.basicdata.BusinessBasicData;
+import org.mxchange.jcoreutils.Comparables;
+import org.mxchange.jcoreutils.SafeNumberUtils;
+import org.mxchange.jproduct.model.category.Categories;
 import org.mxchange.jproduct.model.category.Category;
 import org.mxchange.jproduct.model.category.ProductCategory;
 import org.mxchange.jproduct.model.product.agegroup.AgeGroup;
 
 /**
- * Generic product class
+ * Generic product POJO (entity)
  * <p>
  * @author Roland Häder<roland@mxchange.org>
- * TODO: Find a better name
  */
 @Entity (name = "generic_products")
 @Table (
@@ -115,8 +120,8 @@ public class GenericProduct implements Product {
         * Gross price of product
         */
        @Basic (optional = false)
-       @Column (name = "product_gross_price", nullable = false)
-       private Float productGrossPrice;
+       @Column (name = "product_gross_price", nullable = false, precision = 10, scale = 4)
+       private BigDecimal productGrossPrice;
 
        /**
         * I18n key of product
@@ -143,8 +148,8 @@ public class GenericProduct implements Product {
        /**
         * Net price of product
         */
-       @Column (name = "product_net_price")
-       private Float productNetPrice;
+       @Column (name = "product_net_price", precision = 10, scale = 4)
+       private BigDecimal productNetPrice;
 
        /**
         * Number of product
@@ -161,15 +166,15 @@ public class GenericProduct implements Product {
        /**
         * Tax rate (0-1, by 1=100%)
         */
-       @Column (name = "product_tax_rate")
-       private Float productTaxRate;
+       @Column (name = "product_tax_rate", precision = 3, scale = 1)
+       private BigDecimal productTaxRate;
 
        /**
         * Amount of this product (for example 1 for 1 liter)
         */
        @Basic (optional = false)
-       @Column (name = "product_unit_amount", nullable = false)
-       private Float productUnitAmount;
+       @Column (name = "product_unit_amount", nullable = false, precision = 10, scale = 2)
+       private BigDecimal productUnitAmount;
 
        /**
         * Unit type (for example liter)
@@ -199,7 +204,7 @@ public class GenericProduct implements Product {
         * @throws IllegalArgumentException If a parameter is empty (string) or out
         * of bounds
         */
-       public GenericProduct (final String productI18nKey, final Float productGrossPrice, final String productCurrencyCode, final Category productCategory, final Boolean productAvailability, final Float productUnitAmount, final String productUnitI18nKey) {
+       public GenericProduct (final String productI18nKey, final BigDecimal productGrossPrice, final String productCurrencyCode, final Category productCategory, final Boolean productAvailability, final BigDecimal productUnitAmount, final String productUnitI18nKey) {
                // Call other constructor first
                this();
 
@@ -225,7 +230,7 @@ public class GenericProduct implements Product {
                } else if (null == productGrossPrice) {
                        // Throw it again
                        throw new NullPointerException("productGrossPrice is null"); //NOI18N
-               } else if (productGrossPrice < 0) {
+               } else if (productGrossPrice.longValue() < 0) {
                        // Throw IAE
                        throw new IllegalArgumentException(MessageFormat.format("productGrossPrice={0} is invalid. Do not enter discounts as products.", productGrossPrice)); //NOI18N
                } else if (null == productI18nKey) {
@@ -237,7 +242,7 @@ public class GenericProduct implements Product {
                } else if (null == productUnitAmount) {
                        // Throw it again
                        throw new NullPointerException("productUnitAmount is null"); //NOI18N
-               } else if (productUnitAmount < 0) {
+               } else if (productUnitAmount.longValue() < 0) {
                        // Throw IAE
                        throw new IllegalArgumentException(MessageFormat.format("productUnitAmount={0} is invalid. Do not enter discounts as products.", productUnitAmount)); //NOI18N
                } else if (null == productUnitI18nKey) {
@@ -258,6 +263,48 @@ public class GenericProduct implements Product {
                this.productUnitI18nKey = productUnitI18nKey;
        }
 
+       @Override
+       public int compareTo (final Product product) {
+               // Check parameter on null-reference and equality to this
+               if (null == product) {
+                       // Should not happen
+                       throw new NullPointerException("product is null"); //NOI18N
+               } else if (product.equals(this)) {
+                       // Same object
+                       return 0;
+               }
+
+               // Init comparators
+               final int comparators[] = {
+                       // First check product number
+                       SafeNumberUtils.compare(this.getProductNumber(), product.getProductNumber()),
+                       // ... size
+                       StringUtils.compare(this.getProductSize(), product.getProductSize()),
+                       // ... then i18n key
+                       this.getProductI18nKey().compareTo(product.getProductI18nKey()),
+                       // ... gross price
+                       this.getProductGrossPrice().compareTo(product.getProductGrossPrice()),
+                       // ... net price
+                       SafeNumberUtils.compare(this.getProductNetPrice(), product.getProductNetPrice()),
+                       // ... tax rate
+                       SafeNumberUtils.compare(this.getProductTaxRate(), product.getProductTaxRate()),
+                       // ... unit amount
+                       this.getProductUnitAmount().compareTo(product.getProductUnitAmount()),
+                       // ... currency code
+                       this.getProductCurrencyCode().compareTo(product.getProductCurrencyCode()),
+                       // ... manufacturer
+                       BasicDataUtils.compare(this.getProductManufacturer(), product.getProductManufacturer()),
+                       // ... category
+                       Categories.compare(this.getProductCategory(), product.getProductCategory())
+               };
+
+               // Check all values
+               final int comparison = Comparables.checkAll(comparators);
+
+               // Return value
+               return comparison;
+       }
+
        @Override
        public boolean equals (final Object object) {
                if (this == object) {
@@ -270,7 +317,9 @@ public class GenericProduct implements Product {
 
                final Product product = (Product) object;
 
-               if (!Objects.equals(this.getProductId(), product.getProductId())) {
+               if (!Objects.equals(this.getProductGrossPrice(), product.getProductGrossPrice())) {
+                       return false;
+               } else if (!Objects.equals(this.getProductId(), product.getProductId())) {
                        return false;
                } else if (!Objects.equals(this.getProductI18nKey(), product.getProductI18nKey())) {
                        return false;
@@ -336,12 +385,12 @@ public class GenericProduct implements Product {
        }
 
        @Override
-       public Float getProductGrossPrice () {
+       public BigDecimal getProductGrossPrice () {
                return this.productGrossPrice;
        }
 
        @Override
-       public void setProductGrossPrice (final Float productGrossPrice) {
+       public void setProductGrossPrice (final BigDecimal productGrossPrice) {
                this.productGrossPrice = productGrossPrice;
        }
 
@@ -376,12 +425,12 @@ public class GenericProduct implements Product {
        }
 
        @Override
-       public Float getProductNetPrice () {
+       public BigDecimal getProductNetPrice () {
                return this.productNetPrice;
        }
 
        @Override
-       public void setProductNetPrice (final Float productNetPrice) {
+       public void setProductNetPrice (final BigDecimal productNetPrice) {
                this.productNetPrice = productNetPrice;
        }
 
@@ -406,22 +455,22 @@ public class GenericProduct implements Product {
        }
 
        @Override
-       public Float getProductTaxRate () {
+       public BigDecimal getProductTaxRate () {
                return this.productTaxRate;
        }
 
        @Override
-       public void setProductTaxRate (final Float productTaxRate) {
+       public void setProductTaxRate (final BigDecimal productTaxRate) {
                this.productTaxRate = productTaxRate;
        }
 
        @Override
-       public Float getProductUnitAmount () {
+       public BigDecimal getProductUnitAmount () {
                return this.productUnitAmount;
        }
 
        @Override
-       public void setProductUnitAmount (final Float productUnitAmount) {
+       public void setProductUnitAmount (final BigDecimal productUnitAmount) {
                this.productUnitAmount = productUnitAmount;
        }
 
@@ -439,6 +488,7 @@ public class GenericProduct implements Product {
        public int hashCode () {
                int hash = 7;
 
+               hash = 23 * hash + Objects.hashCode(this.getProductGrossPrice());
                hash = 23 * hash + Objects.hashCode(this.getProductId());
                hash = 23 * hash + Objects.hashCode(this.getProductI18nKey());
                hash = 23 * hash + Objects.hashCode(this.getProductAgeGroup());