org.yestech.lib.currency.Money Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of yeslib Show documentation
Show all versions of yeslib Show documentation
A collection of classes that can be used across yestech artifacts/components, but must not be dependant
on any yestech component. Most of the code is utility type code. When more than a few classes are
found to be in a package or the package start to handle more that a few reposibilities then a new
independant component is created and the existing code in yeslib is ported to the new component.
/*
* Copyright LGPL3
* YES Technology Association
* http://yestech.org
*
* http://www.opensource.org/licenses/lgpl-3.0.html
*/
/*
*
* Author: Artie Copeland
* Last Modified Date: $DateTime: $
*/
package org.yestech.lib.currency;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import javax.persistence.Embeddable;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Currency;
import java.util.Locale;
import com.thoughtworks.xstream.annotations.XStreamAlias;
/**
* Represents an immutable monetary value. By default it assumes {@link Locale#US} and
* the corresponding Currency.
*
* @author Artie Copeland
* @version $Revision: $
*/
@XStreamAlias("money")
@XmlRootElement(name = "money")
public class Money implements Serializable, Comparable {
private BigDecimal amount = BigDecimal.ZERO;
private Locale locale;
private Currency curreny;
/**
* ONLY used for xml serialization and deserialization!!!!!
*/
public Money() {
}
/**
* ONLY used for xml deserialization!!!!!
*/
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
/**
* ONLY used for xml deserialization!!!!!
*/
public void setLocale(Locale locale) {
this.locale = locale;
}
/**
* ONLY used for xml deserialization!!!!!
*/
public void setCurreny(Currency curreny) {
this.curreny = curreny;
}
public Money(double amount) {
this(amount, Locale.US);
}
public Money(double amount, Locale locale) {
if (amount < 0) {
throw new CurrencyException("money amount must be a positive number");
}
if (locale == null) {
throw new CurrencyException("can't have a null locale");
}
this.amount = new BigDecimal(amount);
this.locale = locale;
this.curreny = Currency.getInstance(locale);
}
public Money(String amount) {
this(amount, Locale.US);
}
public Money(String amount, Locale locale) {
BigDecimal tmpAmount;
if (StringUtils.isBlank(amount) || !NumberUtils.isNumber(amount)) {
throw new CurrencyException("money amount must be a positive number");
} else {
tmpAmount = new BigDecimal(amount);
validateAmount(tmpAmount);
}
if (locale == null) {
throw new CurrencyException("can't have a null locale");
}
this.amount = tmpAmount;
this.locale = locale;
this.curreny = Currency.getInstance(locale);
}
public Money(BigDecimal amount) {
this(amount, Locale.US);
}
public Money(BigDecimal amount, Locale locale) {
validateAmount(amount);
if (locale == null) {
throw new CurrencyException("can't have a null locale");
}
this.amount = amount;
this.locale = locale;
this.curreny = Currency.getInstance(locale);
}
private void validateAmount(BigDecimal amount) {
if (amount == null || amount.doubleValue() < BigDecimal.ZERO.doubleValue()) {
throw new CurrencyException("can't have a null of negative money amount");
}
}
public BigDecimal getAmount() {
return amount;
}
public Currency getCurreny() {
return curreny;
}
public Locale getLocale() {
return locale;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Money)) return false;
Money money = (Money) o;
if (amount != null ? !amount.equals(money.amount) : money.amount != null) return false;
if (curreny != null ? !curreny.equals(money.curreny) : money.curreny != null) return false;
if (locale != null ? !locale.equals(money.locale) : money.locale != null) return false;
return true;
}
@Override
public int hashCode() {
int result = amount != null ? amount.hashCode() : 0;
result = 31 * result + (locale != null ? locale.hashCode() : 0);
result = 31 * result + (curreny != null ? curreny.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "Money{" +
"amount=" + amount +
", locale=" + locale +
", curreny=" + curreny +
'}';
}
@Override
public int compareTo(Money comparable) {
return getAmount().compareTo(comparable.getAmount());
}
}