From b8c23e6043365c703a4c6d9145e8001bfbbe4178 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Fri, 30 Mar 2018 00:05:16 +0200 Subject: [PATCH] Continued: - added filterByPrice() method for p:dataTable being able to filter on BigDecimal values MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../jcoreee/bean/faces/BaseFacesBean.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/org/mxchange/jcoreee/bean/faces/BaseFacesBean.java b/src/org/mxchange/jcoreee/bean/faces/BaseFacesBean.java index 16dbefe..93d502c 100644 --- a/src/org/mxchange/jcoreee/bean/faces/BaseFacesBean.java +++ b/src/org/mxchange/jcoreee/bean/faces/BaseFacesBean.java @@ -17,6 +17,7 @@ package org.mxchange.jcoreee.bean.faces; import java.io.Serializable; +import java.math.BigDecimal; import java.security.Principal; import java.text.MessageFormat; import java.util.ArrayList; @@ -80,6 +81,41 @@ public abstract class BaseFacesBean implements Serializable { super(); } + /** + * A filter for prices + *

+ * @param value Actual value to check + * @param filter Filter value to check against {@code value} + * @param locale Locale + *

+ * @return Whether price ({@code value}) is at least {@code filter} + * + * @see https://www.primefaces.org/showcase/ui/data/datatable/filter.xhtml + */ + @SuppressWarnings ("unchecked") + public boolean filterByPrice (final Object value, final Object filter, final Locale locale) { + // Get trimmed string or NULL + final String filterText = (filter == null) ? null : filter.toString().trim(); + + // Is null or empty? + if (filterText == null || filterText.isEmpty()) { + // Then return TRUE + return true; + } + + // Is value NULL or not castable? + if (value == null) { + // Return FALSE + return false; + } else if (!(value instanceof Comparable)) { + // Not castable + throw new ClassCastException("value is not instance of Comparable"); + } + + // Do the cast and compare + return ((Comparable) value).compareTo(BigDecimal.valueOf(Double.valueOf(filterText))) > 0; + } + /** * Determines principal's name or returns null if no principal (security) is * set. -- 2.39.5