From 99e135f983f91e7353c639ffe5eae20b608c2e42 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Tue, 20 Mar 2018 00:35:41 +0100 Subject: [PATCH] Continued: - implemented Comparable - added utilities classes for both Category and Product instances - added depdendency to jcore-utils.jar - added depdendency to commons-lang3 MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- lib/nblibraries.properties | 5 ++ nbproject/project.properties | 4 ++ .../jproduct/model/category/Categories.java | 67 +++++++++++++++++++ .../jproduct/model/category/Category.java | 2 +- .../model/category/ProductCategory.java | 27 ++++++++ .../model/product/GenericProduct.java | 49 +++++++++++++- .../jproduct/model/product/Product.java | 2 +- .../jproduct/model/product/Products.java | 67 +++++++++++++++++++ 8 files changed, 220 insertions(+), 3 deletions(-) create mode 100644 src/org/mxchange/jproduct/model/category/Categories.java create mode 100644 src/org/mxchange/jproduct/model/product/Products.java diff --git a/lib/nblibraries.properties b/lib/nblibraries.properties index 167f5fe..b03e047 100644 --- a/lib/nblibraries.properties +++ b/lib/nblibraries.properties @@ -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 diff --git a/nbproject/project.properties b/nbproject/project.properties index de3ed3b..9fa8445 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -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 index 0000000..390ebae --- /dev/null +++ b/src/org/mxchange/jproduct/model/category/Categories.java @@ -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 . + */ +package org.mxchange.jproduct.model.category; + +import java.io.Serializable; +import java.util.Objects; + +/** + * An utilities class for product categories + *

+ * @author Roland Häder + */ +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. + *

+ * @param category1 Category instance 1 + * @param category2 Category instance 2 + *

+ * @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 + } + +} diff --git a/src/org/mxchange/jproduct/model/category/Category.java b/src/org/mxchange/jproduct/model/category/Category.java index 68f7940..9b916ba 100644 --- a/src/org/mxchange/jproduct/model/category/Category.java +++ b/src/org/mxchange/jproduct/model/category/Category.java @@ -24,7 +24,7 @@ import java.util.Date; *

* @author Roland Häder */ -public interface Category extends Serializable { +public interface Category extends Comparable, Serializable { /** * Getter for created timestamp diff --git a/src/org/mxchange/jproduct/model/category/ProductCategory.java b/src/org/mxchange/jproduct/model/category/ProductCategory.java index 3483670..195bdc6 100644 --- a/src/org/mxchange/jproduct/model/category/ProductCategory.java +++ b/src/org/mxchange/jproduct/model/category/ProductCategory.java @@ -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) { diff --git a/src/org/mxchange/jproduct/model/product/GenericProduct.java b/src/org/mxchange/jproduct/model/product/GenericProduct.java index 01e76d5..bc357b6 100644 --- a/src/org/mxchange/jproduct/model/product/GenericProduct.java +++ b/src/org/mxchange/jproduct/model/product/GenericProduct.java @@ -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) { diff --git a/src/org/mxchange/jproduct/model/product/Product.java b/src/org/mxchange/jproduct/model/product/Product.java index 95c9fea..7fc1a81 100644 --- a/src/org/mxchange/jproduct/model/product/Product.java +++ b/src/org/mxchange/jproduct/model/product/Product.java @@ -28,7 +28,7 @@ import org.mxchange.jproduct.model.product.agegroup.AgeGroup; *

* @author Roland Häder */ -public interface Product extends Serializable { +public interface Product extends Comparable, 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 index 0000000..028a10d --- /dev/null +++ b/src/org/mxchange/jproduct/model/product/Products.java @@ -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 . + */ +package org.mxchange.jproduct.model.product; + +import java.io.Serializable; +import java.util.Objects; + +/** + * An utilities class for generic products + *

+ * @author Roland Häder + */ +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. + *

+ * @param product1 Product instance 1 + * @param product2 Product instance 2 + *

+ * @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 + } + +} -- 2.39.5