Selaa lähdekoodia

Adding support for BigDecimals

Stefano Fornari 2 vuotta sitten
vanhempi
commit
18852aa462

+ 94 - 0
materialfx/src/main/java/io/github/palexdev/materialfx/filter/BigDecimalFilter.java

@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2022 Parisi Alessandro
+ * This file is part of MaterialFX (https://github.com/palexdev/MaterialFX).
+ *
+ * MaterialFX is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * MaterialFX 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 Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with MaterialFX.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package io.github.palexdev.materialfx.filter;
+
+import io.github.palexdev.materialfx.beans.BiPredicateBean;
+import io.github.palexdev.materialfx.filter.base.NumberFilter;
+import io.github.palexdev.materialfx.i18n.I18N;
+import io.github.palexdev.materialfx.utils.FXCollectors;
+import java.math.BigDecimal;
+import javafx.collections.ObservableList;
+import javafx.util.StringConverter;
+
+import java.util.Collections;
+import java.util.function.Function;
+import java.util.stream.Stream;
+
+/**
+ * Extension of {@link NumberFilter} for float fields.
+ * <p></p>
+ * Offers the following default {@link BiPredicateBean}s:
+ * <p> - "is": checks for floats equality
+ * <p> - "is not": checks for floats inequality
+ * <p> - "greater than": checks if a float is greater than another float
+ * <p> - "greater or equal to": checks if a float is greater or equal to another float
+ * <p> - "lesser than": checks if a float is lesser than another float
+ * <p> - "lesser or equal to": checks if a float is lesser or equal to another float
+ *
+ * Example:
+ * <code>
+ *    MFXPaginatedTableView transactions = ...;
+ *    transactions.getFilters().addAll(
+ *      ...
+ *      new BigDecimalFilter<>("amount", Transaction::amount),
+ *      ...
+ *    );
+ * </code>
+ */
+public class BigDecimalFilter<T> extends NumberFilter<T, BigDecimal> {
+
+	//================================================================================
+	// Constructors
+	//================================================================================
+	public BigDecimalFilter(String name, Function<T, BigDecimal> extractor) {
+		super(name, extractor, new StringConverter<>() {
+			@Override
+			public String toString(BigDecimal d) {
+				return d.toPlainString();
+			}
+
+			@Override
+			public BigDecimal fromString(String s) {
+				return new BigDecimal(s);
+			}
+		});
+	}
+
+	//================================================================================
+	// Overridden Methods
+	//================================================================================
+	@Override
+	protected ObservableList<BiPredicateBean<BigDecimal, BigDecimal>> defaultPredicates() {
+		return Stream.<BiPredicateBean<BigDecimal, BigDecimal>>of(
+				new BiPredicateBean<>(I18N.getOrDefault("filter.is"), BigDecimal::equals),
+				new BiPredicateBean<>(I18N.getOrDefault("filter.isNot"), (aFloat, aFloat2) -> !aFloat.equals(aFloat2)),
+				new BiPredicateBean<>(I18N.getOrDefault("filter.greater"), (aFloat, aFloat2) -> aFloat.compareTo(aFloat2) > 0),
+				new BiPredicateBean<>(I18N.getOrDefault("filter.greaterEqual"), (aFloat, aFloat2) -> aFloat.compareTo(aFloat2) >= 0),
+				new BiPredicateBean<>(I18N.getOrDefault("filter.lesser"), (aFloat, aFloat2) -> aFloat.compareTo(aFloat2) < 0),
+				new BiPredicateBean<>(I18N.getOrDefault("filter.lesserEqual"), (aFloat, aFloat2) -> aFloat.compareTo(aFloat2) <= 0)
+		).collect(FXCollectors.toList());
+	}
+
+	@SafeVarargs
+	@Override
+	protected final BigDecimalFilter<T> extend(BiPredicateBean<BigDecimal, BigDecimal>... predicateBeans) {
+		Collections.addAll(super.predicates, predicateBeans);
+		return this;
+	}
+}

+ 73 - 0
materialfx/src/test/java/io/github/palexdev/materialfx/filter/BigDecimalFilterTest.java

@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2022 Parisi Alessandro
+ * This file is part of MaterialFX (https://github.com/palexdev/MaterialFX).
+ *
+ * MaterialFX is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * MaterialFX 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 Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with MaterialFX.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package io.github.palexdev.materialfx.filter;
+
+import io.github.palexdev.materialfx.beans.BiPredicateBean;
+import java.math.BigDecimal;
+import java.util.List;
+import static org.junit.Assert.*;
+import org.junit.Test;
+
+public class BigDecimalFilterTest {
+
+    @Test
+    public void constructor() {
+        final String NAME1 = "amount", NAME2 = "balance";
+
+        BigDecimalFilter<Account> f = new BigDecimalFilter<>("amount", Account::balance);
+        assertEquals(NAME1, f.name()); assertNotNull(f.getExtractor());
+        f = new BigDecimalFilter<>("amount", null);
+        assertEquals(NAME1, f.name()); assertNull(f.getExtractor());
+    }
+
+    @Test
+    public void predicates() {
+        BigDecimalFilter<Account> f = new BigDecimalFilter<>("amount", Account::balance);
+
+        List<BiPredicateBean<BigDecimal, BigDecimal>> predicates = f.defaultPredicates();
+        assertEquals(6, predicates.size());
+        assertEquals("is", predicates.get(0).name());
+        assertEquals("is not", predicates.get(1).name());
+        assertEquals("greater than", predicates.get(2).name());
+        assertEquals("greater or equal to", predicates.get(3).name());
+        assertEquals("lesser than", predicates.get(4).name());
+        assertEquals("lesser or equal to", predicates.get(5).name());
+    }
+
+    @Test public void getValue() {
+        final String D1 = "0.0000000000001", D2 = "123456789012345";
+
+        BigDecimalFilter<Account> f = new BigDecimalFilter<>("amount", Account::balance);
+
+        assertEquals(new BigDecimal(D1), f.getValue(D1));
+        assertEquals(new BigDecimal(D2), f.getValue(D2));
+    }
+
+    // Account -----------------------------------------------------------------
+    private class Account {
+        private BigDecimal balance;
+
+        public Account(BigDecimal balance) {
+            this.balance = balance;
+        }
+
+        public BigDecimal balance() {
+            return balance;
+        }
+    }
+}