Skip to content

Commit

Permalink
Add or-equal methods (#131)
Browse files Browse the repository at this point in the history
* Add isGreaterThanOrEqual()
* Add isLessThanOrEqual()

Fixes #98
  • Loading branch information
jodastephen authored Oct 29, 2023
1 parent ac9b77e commit d4d254d
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 24 deletions.
10 changes: 10 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
<body>

<!-- types are add, fix, remove, update -->
<release version="1.0.4" date="SNAPSHOT" description="v1.0.4">
<action dev="jodastephen" type="add">
Add isGreaterThanOrEqual() and isLessThanOrEqual() to money classes.
Fixes #98.
</action>
<action dev="jodastephen" type="fix">
Workaround issue with BigDecimal.stripTrailingZeros() on early Android versions.
Fixes #129.
</action>
</release>
<release version="1.0.3" date="2022-12-06" description="v1.0.3">
<action dev="janjaali" type="update">
Change currency HRK to EUR.
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/org/joda/money/BigMoney.java
Original file line number Diff line number Diff line change
Expand Up @@ -1643,6 +1643,18 @@ public boolean isGreaterThan(BigMoneyProvider other) {
return compareTo(other) > 0;
}

/**
* Checks if this monetary value is greater than or equal to another.
* The compared values must be in the same currency.
*
* @param other the other monetary value, not null
* @return true if this is greater than or equal to the specified monetary value
* @throws CurrencyMismatchException if the currencies differ
*/
public boolean isGreaterThanOrEqual(BigMoneyProvider other) {
return compareTo(other) >= 0;
}

/**
* Checks if this monetary value is less than another.
* The compared values must be in the same currency.
Expand All @@ -1655,6 +1667,18 @@ public boolean isLessThan(BigMoneyProvider other) {
return compareTo(other) < 0;
}

/**
* Checks if this monetary value is less or equal to than another.
* The compared values must be in the same currency.
*
* @param other the other monetary value, not null
* @return true if this is less than or equal to the specified monetary value
* @throws CurrencyMismatchException if the currencies differ
*/
public boolean isLessThanOrEqual(BigMoneyProvider other) {
return compareTo(other) <= 0;
}

//-----------------------------------------------------------------------
/**
* Checks if this monetary value equals another.
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/org/joda/money/Money.java
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,21 @@ public boolean isGreaterThan(BigMoneyProvider other) {
return money.isGreaterThan(other);
}

/**
* Checks if this monetary value is greater than or equal to another.
* <p>
* This allows {@code Money} to be compared to any {@code BigMoneyProvider}.
* Scale is ignored in the comparison.
* The compared values must be in the same currency.
*
* @param other the other monetary value, not null
* @return true is this is greater than or equal to the specified monetary value
* @throws CurrencyMismatchException if the currencies differ
*/
public boolean isGreaterThanOrEqual(BigMoneyProvider other) {
return money.isGreaterThanOrEqual(other);
}

/**
* Checks if this monetary value is less than another.
* <p>
Expand All @@ -1272,6 +1287,21 @@ public boolean isLessThan(BigMoneyProvider other) {
return money.isLessThan(other);
}

/**
* Checks if this monetary value is less than or equal to another.
* <p>
* This allows {@code Money} to be compared to any {@code BigMoneyProvider}.
* Scale is ignored in the comparison.
* The compared values must be in the same currency.
*
* @param other the other monetary value, not null
* @return true is this is less than or equal to the specified monetary value
* @throws CurrencyMismatchException if the currencies differ
*/
public boolean isLessThanOrEqual(BigMoneyProvider other) {
return money.isLessThanOrEqual(other);
}

//-----------------------------------------------------------------------
/**
* Checks if this monetary value equals another.
Expand Down
78 changes: 66 additions & 12 deletions src/test/java/org/joda/money/TestBigMoney.java
Original file line number Diff line number Diff line change
Expand Up @@ -2591,17 +2591,16 @@ public void test_isGreaterThan() {
BigMoney b = GBP_2_35;
BigMoney c = GBP_2_36;
assertEquals(false, a.isGreaterThan(a));
assertEquals(false, b.isGreaterThan(b));
assertEquals(false, c.isGreaterThan(c));

assertEquals(false, a.isGreaterThan(b));
assertEquals(true, b.isGreaterThan(a));

assertEquals(false, a.isGreaterThan(c));
assertEquals(true, c.isGreaterThan(a));

assertEquals(true, b.isGreaterThan(a));
assertEquals(false, b.isGreaterThan(b));
assertEquals(false, b.isGreaterThan(c));

assertEquals(true, c.isGreaterThan(a));
assertEquals(true, c.isGreaterThan(b));
assertEquals(false, c.isGreaterThan(c));
}

@Test(expected = CurrencyMismatchException.class)
Expand All @@ -2611,6 +2610,34 @@ public void test_isGreaterThan_currenciesDiffer() {
a.isGreaterThan(b);
}

//-----------------------------------------------------------------------
// isGreaterThanOrEqual()
//-----------------------------------------------------------------------
@Test
public void test_isGreaterThanOrEqual() {
BigMoney a = GBP_2_34;
BigMoney b = GBP_2_35;
BigMoney c = GBP_2_36;
assertEquals(true, a.isGreaterThanOrEqual(a));
assertEquals(false, a.isGreaterThanOrEqual(b));
assertEquals(false, a.isGreaterThanOrEqual(c));

assertEquals(true, b.isGreaterThanOrEqual(a));
assertEquals(true, b.isGreaterThanOrEqual(b));
assertEquals(false, b.isGreaterThanOrEqual(c));

assertEquals(true, c.isGreaterThanOrEqual(a));
assertEquals(true, c.isGreaterThanOrEqual(b));
assertEquals(true, c.isGreaterThanOrEqual(c));
}

@Test(expected = CurrencyMismatchException.class)
public void test_isGreaterThanOrEqual_currenciesDiffer() {
BigMoney a = GBP_2_34;
BigMoney b = USD_2_35;
a.isGreaterThanOrEqual(b);
}

//-----------------------------------------------------------------------
// isLessThan()
//-----------------------------------------------------------------------
Expand All @@ -2620,17 +2647,16 @@ public void test_isLessThan() {
BigMoney b = GBP_2_35;
BigMoney c = GBP_2_36;
assertEquals(false, a.isLessThan(a));
assertEquals(false, b.isLessThan(b));
assertEquals(false, c.isLessThan(c));

assertEquals(true, a.isLessThan(b));
assertEquals(false, b.isLessThan(a));

assertEquals(true, a.isLessThan(c));
assertEquals(false, c.isLessThan(a));

assertEquals(false, b.isLessThan(a));
assertEquals(false, b.isLessThan(b));
assertEquals(true, b.isLessThan(c));

assertEquals(false, c.isLessThan(a));
assertEquals(false, c.isLessThan(b));
assertEquals(false, c.isLessThan(c));
}

@Test(expected = CurrencyMismatchException.class)
Expand All @@ -2640,6 +2666,34 @@ public void test_isLessThan_currenciesDiffer() {
a.isLessThan(b);
}

//-----------------------------------------------------------------------
// isLessThanOrEqual()
//-----------------------------------------------------------------------
@Test
public void test_isLessThanOrEqual() {
BigMoney a = GBP_2_34;
BigMoney b = GBP_2_35;
BigMoney c = GBP_2_36;
assertEquals(true, a.isLessThanOrEqual(a));
assertEquals(true, a.isLessThanOrEqual(b));
assertEquals(true, a.isLessThanOrEqual(c));

assertEquals(false, b.isLessThanOrEqual(a));
assertEquals(true, b.isLessThanOrEqual(b));
assertEquals(true, b.isLessThanOrEqual(c));

assertEquals(false, c.isLessThanOrEqual(a));
assertEquals(false, c.isLessThanOrEqual(b));
assertEquals(true, c.isLessThanOrEqual(c));
}

@Test(expected = CurrencyMismatchException.class)
public void test_isLessThanOrEqual_currenciesDiffer() {
BigMoney a = GBP_2_34;
BigMoney b = USD_2_35;
a.isLessThanOrEqual(b);
}

//-----------------------------------------------------------------------
// equals() hashCode()
//-----------------------------------------------------------------------
Expand Down
78 changes: 66 additions & 12 deletions src/test/java/org/joda/money/TestMoney.java
Original file line number Diff line number Diff line change
Expand Up @@ -2139,17 +2139,16 @@ public void test_isGreaterThan() {
Money b = GBP_2_35;
Money c = GBP_2_36;
assertEquals(false, a.isGreaterThan(a));
assertEquals(false, b.isGreaterThan(b));
assertEquals(false, c.isGreaterThan(c));

assertEquals(false, a.isGreaterThan(b));
assertEquals(true, b.isGreaterThan(a));

assertEquals(false, a.isGreaterThan(c));
assertEquals(true, c.isGreaterThan(a));

assertEquals(true, b.isGreaterThan(a));
assertEquals(false, b.isGreaterThan(b));
assertEquals(false, b.isGreaterThan(c));

assertEquals(true, c.isGreaterThan(a));
assertEquals(true, c.isGreaterThan(b));
assertEquals(false, c.isGreaterThan(c));
}

@Test(expected = CurrencyMismatchException.class)
Expand All @@ -2159,6 +2158,34 @@ public void test_isGreaterThan_currenciesDiffer() {
a.isGreaterThan(b);
}

//-----------------------------------------------------------------------
// isGreaterThanOrEqual()
//-----------------------------------------------------------------------
@Test
public void test_isGreaterThanOrEqual() {
Money a = GBP_2_34;
Money b = GBP_2_35;
Money c = GBP_2_36;
assertEquals(true, a.isGreaterThanOrEqual(a));
assertEquals(false, a.isGreaterThanOrEqual(b));
assertEquals(false, a.isGreaterThanOrEqual(c));

assertEquals(true, b.isGreaterThanOrEqual(a));
assertEquals(true, b.isGreaterThanOrEqual(b));
assertEquals(false, b.isGreaterThanOrEqual(c));

assertEquals(true, c.isGreaterThanOrEqual(a));
assertEquals(true, c.isGreaterThanOrEqual(b));
assertEquals(true, c.isGreaterThanOrEqual(c));
}

@Test(expected = CurrencyMismatchException.class)
public void test_isGreaterThanOrEqual_currenciesDiffer() {
Money a = GBP_2_34;
Money b = USD_2_35;
a.isGreaterThanOrEqual(b);
}

//-----------------------------------------------------------------------
// isLessThan()
//-----------------------------------------------------------------------
Expand All @@ -2168,17 +2195,16 @@ public void test_isLessThan() {
Money b = GBP_2_35;
Money c = GBP_2_36;
assertEquals(false, a.isLessThan(a));
assertEquals(false, b.isLessThan(b));
assertEquals(false, c.isLessThan(c));

assertEquals(true, a.isLessThan(b));
assertEquals(false, b.isLessThan(a));

assertEquals(true, a.isLessThan(c));
assertEquals(false, c.isLessThan(a));

assertEquals(false, b.isLessThan(a));
assertEquals(false, b.isLessThan(b));
assertEquals(true, b.isLessThan(c));

assertEquals(false, c.isLessThan(a));
assertEquals(false, c.isLessThan(b));
assertEquals(false, c.isLessThan(c));
}

@Test(expected = CurrencyMismatchException.class)
Expand All @@ -2188,6 +2214,34 @@ public void test_isLessThan_currenciesDiffer() {
a.isLessThan(b);
}

//-----------------------------------------------------------------------
// isLessThanOrEqual()
//-----------------------------------------------------------------------
@Test
public void test_isLessThanOrEqual() {
Money a = GBP_2_34;
Money b = GBP_2_35;
Money c = GBP_2_36;
assertEquals(true, a.isLessThanOrEqual(a));
assertEquals(true, a.isLessThanOrEqual(b));
assertEquals(true, a.isLessThanOrEqual(c));

assertEquals(false, b.isLessThanOrEqual(a));
assertEquals(true, b.isLessThanOrEqual(b));
assertEquals(true, b.isLessThanOrEqual(c));

assertEquals(false, c.isLessThanOrEqual(a));
assertEquals(false, c.isLessThanOrEqual(b));
assertEquals(true, c.isLessThanOrEqual(c));
}

@Test(expected = CurrencyMismatchException.class)
public void test_isLessThanOrEqual_currenciesDiffer() {
Money a = GBP_2_34;
Money b = USD_2_35;
a.isLessThanOrEqual(b);
}

//-----------------------------------------------------------------------
// equals() hashCode()
//-----------------------------------------------------------------------
Expand Down

0 comments on commit d4d254d

Please sign in to comment.