Last active
March 23, 2016 09:50
-
-
Save ghusta/e7992f321a2462bb58c4 to your computer and use it in GitHub Desktop.
Comparison of BigDecimal, working with Builder Pattern (isLessThan, isGreaterThan, etc)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fr.husta.util.bigdecimal; | |
import java.math.BigDecimal; | |
public class BigDecimalComparison | |
{ | |
/** | |
* Initiate the comparison. | |
* | |
* @param bigDecimal The firstTerm term of the comparison. | |
* @return BigDecimalComparisonBuilder | |
*/ | |
public static BigDecimalComparisonBuilder compare(BigDecimal bigDecimal) | |
{ | |
return new BigDecimalComparisonBuilder(bigDecimal); | |
} | |
public static boolean isLessThan(BigDecimal bd1, BigDecimal bd2) | |
{ | |
return compare(bd1).isLessThan(bd2); | |
} | |
public static class BigDecimalComparisonBuilder | |
extends ComparableComparisonBuilder | |
{ | |
private BigDecimalComparisonBuilder(BigDecimal firstBD) | |
{ | |
super(firstBD); | |
} | |
} | |
public static abstract class ComparableComparisonBuilder | |
{ | |
protected Comparable firstTerm; | |
public ComparableComparisonBuilder(Comparable firstTerm) | |
{ | |
this.firstTerm = firstTerm; | |
} | |
public boolean isLessThan(Comparable compTerm) | |
{ | |
return firstTerm.compareTo(compTerm) < 0; | |
} | |
public boolean isLessThanOrEqualTo(Comparable compTerm) | |
{ | |
return firstTerm.compareTo(compTerm) <= 0; | |
} | |
public boolean isEqualByComparingTo(Comparable compTerm) | |
{ | |
return firstTerm.compareTo(compTerm) == 0; | |
} | |
public boolean isGreaterThan(Comparable compTerm) | |
{ | |
return firstTerm.compareTo(compTerm) > 0; | |
} | |
public boolean isGreaterThanOrEqualTo(Comparable compTerm) | |
{ | |
return firstTerm.compareTo(compTerm) >= 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment