-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #285 from barspi/refactor-additional-amounts
Refactor additional amounts
- Loading branch information
Showing
11 changed files
with
617 additions
and
32 deletions.
There are no files selected for viewing
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
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
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
83 changes: 83 additions & 0 deletions
83
modules/cmf/src/main/java/org/jpos/cmf/CMFAdditionalAmount.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* jPOS Project [http://jpos.org] | ||
* Copyright (C) 2000-2023 jPOS Software SRL | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program 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 Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package org.jpos.cmf; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.jpos.iso.AdditionalAmount; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Represents one occurrence of an Additional Amount (from DE-54) in jPOS-CMF format. | ||
*/ | ||
public class CMFAdditionalAmount extends AdditionalAmount { | ||
|
||
public final static int SERIALIZED_DATA_LENGTH = 21; | ||
|
||
public CMFAdditionalAmount() { | ||
} | ||
|
||
public CMFAdditionalAmount(String accountType, BigDecimal amount, String currencyCode, | ||
String amountType, int currencyMinorUnit) { | ||
super(accountType, amount, currencyCode, amountType, currencyMinorUnit); | ||
} | ||
|
||
@Override | ||
public String serialize() { | ||
if (getAmountTypeCode() == null) | ||
throw new IllegalStateException("Amount type not set"); | ||
|
||
if (getAmount() == null) | ||
throw new IllegalStateException("Amount not set"); | ||
|
||
long absAmt= getAmount().movePointRight(getCurrencyMinorUnit()).abs().longValue(); | ||
|
||
return getAccountType() + | ||
getAmountTypeCode() + | ||
getCurrencyCode() + | ||
getCurrencyMinorUnit() + | ||
(getAmount().compareTo(BigDecimal.ZERO) >= 0 ? "C" : "D") + | ||
StringUtils.leftPad(Long.toString(absAmt), 12, '0'); | ||
} | ||
|
||
public static AdditionalAmount parse(String data) { | ||
Objects.requireNonNull(data); | ||
|
||
if (data.length() != SERIALIZED_DATA_LENGTH) | ||
throw new IllegalArgumentException("Invalid data length"); | ||
|
||
String accountType = StringUtils.mid(data, 0, 2); | ||
String amountType = StringUtils.mid(data, 2, 2); | ||
String currencyCode = StringUtils.mid(data, 4, 3); | ||
int minorUnit = Integer.parseInt(StringUtils.mid(data, 7, 1)); | ||
|
||
String amountSign = StringUtils.mid(data, 8, 1); | ||
BigDecimal amount = new BigDecimal(StringUtils.right(data, 12)).movePointLeft(minorUnit); | ||
|
||
if (!"C.D".contains(amountSign)) | ||
throw new IllegalArgumentException("Invalid amount sign"); | ||
|
||
if ("D".equalsIgnoreCase(amountSign)) | ||
amount = amount.negate(); | ||
|
||
return new CMFAdditionalAmount(accountType, amount, currencyCode, amountType, minorUnit); | ||
} | ||
|
||
} |
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
92 changes: 92 additions & 0 deletions
92
modules/cmf/src/main/java/org/jpos/iso/AdditionalAmount.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package org.jpos.iso; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.Objects; | ||
|
||
public abstract class AdditionalAmount { | ||
private String accountType; | ||
private String amountType; | ||
private BigDecimal amount; | ||
private String currencyCode; | ||
private int currencyMinorUnit; | ||
|
||
public AdditionalAmount() { | ||
} | ||
|
||
protected AdditionalAmount(String accountType, BigDecimal amount, String currencyCode, | ||
String amountType, int currencyMinorUnit) { | ||
|
||
setAccountType(accountType); | ||
setAmount(amount); | ||
setCurrencyCode(currencyCode); | ||
setAmountTypeCode(amountType); | ||
setCurrencyMinorUnit(currencyMinorUnit); | ||
} | ||
|
||
public abstract String serialize(); | ||
|
||
public String getAccountType() { | ||
return accountType; | ||
} | ||
|
||
public void setAccountType(String accountType) { | ||
Objects.requireNonNull(accountType); | ||
|
||
if (accountType.length() != 2) | ||
throw new IllegalArgumentException("Invalid account type length"); | ||
|
||
this.accountType = accountType; | ||
} | ||
|
||
/** | ||
* @return The internal amount type 2-char string | ||
*/ | ||
public String getAmountTypeCode() { | ||
return amountType; | ||
} | ||
|
||
public void setAmountTypeCode(String amountType) { | ||
Objects.requireNonNull(amountType); | ||
this.amountType = amountType; | ||
} | ||
|
||
public BigDecimal getAmount() { | ||
return amount; | ||
} | ||
|
||
public void setAmount(BigDecimal amount) { | ||
Objects.requireNonNull(amount); | ||
this.amount = amount; | ||
} | ||
|
||
public String getCurrencyCode() { | ||
return currencyCode; | ||
} | ||
|
||
public void setCurrencyCode(String currencyCode) { | ||
Objects.requireNonNull(currencyCode); | ||
|
||
if (currencyCode.length() != 3) | ||
throw new IllegalArgumentException("Invalid currency code"); | ||
|
||
this.currencyCode = currencyCode; | ||
} | ||
|
||
public int getCurrencyMinorUnit() { | ||
return currencyMinorUnit; | ||
} | ||
|
||
public void setCurrencyMinorUnit(int currencyMinorUnit) { | ||
if (currencyMinorUnit < 0 || currencyMinorUnit > 9) | ||
throw new IllegalArgumentException("Invalid currency minor unit value"); | ||
|
||
this.currencyMinorUnit = currencyMinorUnit; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getClass().getSimpleName() + | ||
String.format (" {%s,%s,%s,%d,%s}", accountType, amountType, currencyCode, currencyMinorUnit, amount); | ||
} | ||
|
||
} |
Oops, something went wrong.