]> git.mxchange.org Git - jproduct-core.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Mon, 19 Mar 2018 23:35:41 +0000 (00:35 +0100)
committerRoland Häder <roland@mxchange.org>
Mon, 19 Mar 2018 23:35:41 +0000 (00:35 +0100)
- implemented Comparable
- added utilities classes for both Category and Product instances
- added depdendency to jcore-utils.jar
- added depdendency to commons-lang3

Signed-off-by: Roland Häder <roland@mxchange.org>
lib/nblibraries.properties
nbproject/project.properties
src/org/mxchange/jproduct/model/category/Categories.java [new file with mode: 0644]
src/org/mxchange/jproduct/model/category/Category.java
src/org/mxchange/jproduct/model/category/ProductCategory.java
src/org/mxchange/jproduct/model/product/GenericProduct.java
src/org/mxchange/jproduct/model/product/Product.java
src/org/mxchange/jproduct/model/product/Products.java [new file with mode: 0644]

index 167f5febee1cf41108fe0ee16eedf2a7e60eee66..b03e047ee816b15a363266aeb8a8a86998b3291f 100644 (file)
@@ -1,3 +1,8 @@
+libs.commons-lang3.classpath=\
+    ${base}/commons-lang3/commons-lang3-3.7.jar
+libs.commons-lang3.displayName=Commons Lang3 3.7
+libs.commons-lang3.javadoc=\
+    https://commons.apache.org/proper/commons-lang/javadocs/api-release/
 libs.CopyLibs.classpath=\
     ${base}/CopyLibs/org-netbeans-modules-java-j2seproject-copylibstask.jar
 libs.CopyLibs.displayName=CopyLibs Task
index de3ed3b10ba70066163751de1df86503d884d685..9fa844556ec9935bfdd891ef18fbef44baa6edbd 100644 (file)
@@ -31,12 +31,15 @@ dist.javadoc.dir=${dist.dir}/javadoc
 endorsed.classpath=
 excludes=
 file.reference.jcontacts-business-core.jar=lib/jcontacts-business-core.jar
+file.reference.jcore-utils.jar=lib/jcore-utils.jar
 includes=**
 jar.archive.disabled=${jnlp.enabled}
 jar.compress=false
 jar.index=${jnlp.enabled}
 javac.classpath=\
     ${file.reference.jcontacts-business-core.jar}:\
+    ${file.reference.jcore-utils.jar}:\
+    ${libs.commons-lang3.classpath}:\
     ${libs.javaee-api-7.0.classpath}
 # Space-separated list of extra javac options
 javac.compilerargs=-Xlint:unchecked -Xlint:deprecation
@@ -91,5 +94,6 @@ run.test.classpath=\
     ${build.test.classes.dir}
 source.encoding=UTF-8
 source.reference.jcontacts-business-core.jar=../jcontacts-business-core/src/
+source.reference.jcore-utils.jar=../jcore-utils/src/
 src.dir=src
 test.src.dir=test
diff --git a/src/org/mxchange/jproduct/model/category/Categories.java b/src/org/mxchange/jproduct/model/category/Categories.java
new file mode 100644 (file)
index 0000000..390ebae
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jproduct.model.category;
+
+import java.io.Serializable;
+import java.util.Objects;
+
+/**
+ * An utilities class for product categories
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+public class Categories implements Serializable {
+
+       /**
+        * Serial number
+        */
+       private static final long serialVersionUID = 157_687_661_634_901L;
+
+       /**
+        * Compares both Category instances. This method returns -1 if second
+        * instance is null.
+        * <p>
+        * @param category1 Category instance 1
+        * @param category2 Category instance 2
+        * <p>
+        * @return Comparison value
+        */
+       public static int compare (final Category category1, final Category category2) {
+               // Check euqality, then at least first must be given
+               if (Objects.equals(category1, category2)) {
+                       // Both are same
+                       return 0;
+               } else if (null == category1) {
+                       // First is null
+                       return -1;
+               } else if (null == category2) {
+                       // Second is null
+                       return 1;
+               }
+
+               // Invoke compareTo() method
+               return category1.compareTo(category2);
+       }
+
+       /**
+        * Utility classes should have no instances
+        */
+       private Categories () {
+               // Private constructor
+       }
+
+}
index 68f79407176be13ad5f05bdc8e04dda1d1294717..9b916bad3ce54153436be639e4bb9fa1bd75ca46 100644 (file)
@@ -24,7 +24,7 @@ import java.util.Date;
  * <p>
  * @author Roland Häder<roland@mxchange.org>
  */
-public interface Category extends Serializable {
+public interface Category extends Comparable<Category>, Serializable {
 
        /**
         * Getter for created timestamp
index 3483670178b2e5f3457b24f906f45a7cfd89b757..195bdc67036fd2cecf74f69c5450a40f27dd42d1 100644 (file)
@@ -33,6 +33,7 @@ import javax.persistence.Table;
 import javax.persistence.Temporal;
 import javax.persistence.TemporalType;
 import javax.persistence.Transient;
+import org.mxchange.jcoreutils.Comparables;
 
 /**
  * A product category
@@ -116,6 +117,32 @@ public class ProductCategory implements Category {
        public ProductCategory () {
        }
 
+       @Override
+       public int compareTo (final Category category) {
+               // Check parameter on null-reference and equality to this
+               if (null == category) {
+                       // Should not happen
+                       throw new NullPointerException("category is null"); //NOI18N
+               } else if (category.equals(this)) {
+                       // Same object
+                       return 0;
+               }
+
+               // Init comparators
+               final int comparators[] = {
+                       // First check parent category
+                       Categories.compare(this.getParentCategory(), category.getParentCategory()),
+                       // ... last is i18n key as it is unique
+                       this.getCategoryI18nKey().compareTo(category.getCategoryI18nKey())
+               };
+
+               // Check all values
+               final int comparison = Comparables.checkAll(comparators);
+
+               // Return value
+               return comparison;
+       }
+
        @Override
        public boolean equals (final Object object) {
                if (this == object) {
index 01e76d5a603807192ed5de945393ac9277e60e0b..bc357b637d0f9caf0f17ff4bee82967c5f9fdb60 100644 (file)
@@ -38,8 +38,13 @@ 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;
@@ -200,7 +205,7 @@ public class GenericProduct implements Product {
         * @throws IllegalArgumentException If a parameter is empty (string) or out
         * of bounds
         */
-       public GenericProduct (final String productI18nKey, final BigDecimal productGrossPrice, final String productCurrencyCode, final Category productCategory, final Boolean productAvailability, final BigDecimal 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();
 
@@ -259,6 +264,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) {
index 95c9fea9df7d5b117f83f014e6fb097afafdd958..7fc1a8151769f93dd8a52030c698a1702d992c6e 100644 (file)
@@ -28,7 +28,7 @@ import org.mxchange.jproduct.model.product.agegroup.AgeGroup;
  * <p>
  * @author Roland Häder<roland@mxchange.org>
  */
-public interface Product extends Serializable {
+public interface Product extends Comparable<Product>, Serializable {
 
        /**
         * Getter for created timestamp
diff --git a/src/org/mxchange/jproduct/model/product/Products.java b/src/org/mxchange/jproduct/model/product/Products.java
new file mode 100644 (file)
index 0000000..028a10d
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jproduct.model.product;
+
+import java.io.Serializable;
+import java.util.Objects;
+
+/**
+ * An utilities class for generic products
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+public class Products implements Serializable {
+
+       /**
+        * Serial number
+        */
+       private static final long serialVersionUID = 157_687_661_634_902L;
+
+       /**
+        * Compares both Product instances. This method returns -1 if second
+        * instance is null.
+        * <p>
+        * @param product1 Product instance 1
+        * @param product2 Product instance 2
+        * <p>
+        * @return Comparison value
+        */
+       public static int compare (final Product product1, final Product product2) {
+               // Check euqality, then at least first must be given
+               if (Objects.equals(product1, product2)) {
+                       // Both are same
+                       return 0;
+               } else if (null == product1) {
+                       // First is null
+                       return -1;
+               } else if (null == product2) {
+                       // Second is null
+                       return 1;
+               }
+
+               // Invoke compareTo() method
+               return product1.compareTo(product2);
+       }
+
+       /**
+        * Utility classes should have no instances
+        */
+       private Products () {
+               // Private constructor
+       }
+
+}