Skip to content

Commit

Permalink
Merge pull request #3 from hexmind/jitpack
Browse files Browse the repository at this point in the history
add jitpack
  • Loading branch information
mortezaadi authored Jun 26, 2017
2 parents c434642 + 8b47dde commit 0182231
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 48 deletions.
63 changes: 28 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![](https://jitpack.io/v/mortezaadi/bigdecimal-utils.svg)](https://jitpack.io/#mortezaadi/bigdecimal-utils)

BigDecimal Utils
===============================================
Comparing BigDecimal is always hard to read and so error prone. I wrote this library
Expand All @@ -10,18 +12,14 @@ Money somewhere in your code, you probably faced comparing two BigDecimals a lot

What's wrong with BigDecimal comparison
--------------------------------------
Well, First of all you should know that if you use **equal** method of BigDecimal to compare two objects they only considered equal if they are equal in value and scale thus 2.0 is not equal to 2.00 when compared by
**equal** method. refer to JavaDocs.
Well, first of all you should know that if you use **equal** method of BigDecimal to compare two objects they only considered equal if they are equal in value and scale thus 2.0 is not equal to 2.00 when compared by
**equal** method refer to JavaDocs.

To compare BigDecimal without considering their scale we should use **compareTo** method. So, probably this is
the most common way to compare two BigDecimals. However it is so error prone and lacks readability.
to feel what it looks like take a look at this line of code :

if(balance.compareTo(amount) < 0) && amount.compareTo(minAmount) >= 0)) {
// ....
}else {
// ...
}
return balance.compareTo(amount) < 0) && amount.compareTo(minAmount) >= 0));

the code above try to check condition "balance < amount && amount >= minAmount". You
definitely spotted the problem. But how to solve this?
Expand All @@ -32,51 +30,46 @@ BigDecimalUtils is a simple library to make comparing BigDecimal more readable a
see the same comparison rewritten with this library

import static ir.cafebabe.math.utils.BigDecimalUtils.*;
..
..
..
..
if( is(balance).lt(amount) && is(amount).gte(minAmount) ) {
// ....
}else {
// ...
}

// ...

return is(balance).lt(amount) && is(amount).gte(minAmount));

**Other methods in this library:**

is(bigdecimal).eq(four); //Equal
is(bigdecimal).gt(two); //Greater than
is(bigdecimal).gte(one); //Greater than equal
is(bigdecimal).lt(two); //Less than
is(bigdecimal).lte(two); //Less than equal
is(bigdecimal).eq(four); // equal
is(bigdecimal).gt(two); // greater than
is(bigdecimal).gte(one); // greater than equal
is(bigdecimal).lt(two); // less than
is(bigdecimal).lte(two); // less than equal

is(bigdecimal).notEq(four); //Not Equal
is(bigdecimal).notGt(two); //Not Greater than
is(bigdecimal).notGte(one); //Not Greater than equal
is(bigdecimal).notLt(two); //Not Less than
is(bigdecimal).notLte(two); //Not Less than equal
is(bigdecimal).notEq(four); // not equal
is(bigdecimal).notGt(two); // not greater than
is(bigdecimal).notGte(one); // not greater than equal
is(bigdecimal).notLt(two); // not less than
is(bigdecimal).notLte(two); // not less than equal

is(bigdecimal).isZero();
is(bigdecimal).notZero();
is(bigdecimal).isPositive(); // greater than zero
is(bigdecimal).isNegetive(); // less than zero
is(bigdecimal).isNonPositive(); //less than or equal zero
is(bigdecimal).isNonNegetive(); //greater than or equal zero
is(bigdecimal).isNonPositive(); // less than or equal zero
is(bigdecimal).isNonNegetive(); // greater than or equal zero

is(bigdecimal).isNullOrZero(); //is null or zero
is(bigdecimal).notNullOrZero(); //not null or zero
is(bigdecimal).isNullOrZero(); // is null or zero
is(bigdecimal).notNullOrZero(); // not null or zero

You can also compare a BigDecimal to other numerical values. for instance instead of writing
You can also compare a BigDecimal to other numerical values. For instance instead of writing

is(bigdecimal).notEq(BigDecimal.valueOf(4));

you can simply write:
you can simply and interchangeably write:

is(bigdecimal).notEq(4);
is(bigdecimal).notEq(4L); //or
is(bigdecimal).notEq(4D); //or
is(bigdecimal).notEq(4L);
is(bigdecimal).notEq(4D);
PS
Why BigDecimal Utils?
--------------------------
I didn't find any library to handle BigDecimal comparison. If you found one please let me know.

Expand Down
73 changes: 60 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,61 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ir.cafebabe.math.utils</groupId>
<artifactId>bigdecimal-utils</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ir.cafebabe.math.utils</groupId>
<artifactId>bigdecimal-utils</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
<maven.javadoc.skip>true</maven.javadoc.skip>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

0 comments on commit 0182231

Please sign in to comment.