org.brocalc.domain.CurrencyAmount.java Source code

Java tutorial

Introduction

Here is the source code for org.brocalc.domain.CurrencyAmount.java

Source

/*******************************************************************************
 * Copyright (c) 2013 51zero Ltd.
 *
 * This file is part of the Brokerage Calculation Library (brocalc).
 *
 * brocalc is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * brocalc is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even he implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
 * more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with brocalc.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package org.brocalc.domain;

import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.math.BigDecimal;

import javax.annotation.concurrent.Immutable;

import org.apache.commons.lang.Validate;

@Immutable
public class CurrencyAmount implements Serializable {
    private final Currency currency;
    private final BigDecimal amount;

    public CurrencyAmount(Currency currency, BigDecimal amount) {
        this.currency = currency;

        Validate.notNull(amount, "Currency amount must not be null");
        this.amount = amount;
    }

    public Currency getCurrency() {
        return currency;
    }

    public BigDecimal getAmount() {
        return amount;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((amount == null) ? 0 : amount.hashCode());
        result = prime * result + ((currency == null) ? 0 : currency.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        CurrencyAmount other = (CurrencyAmount) obj;
        if (amount == null) {
            if (other.amount != null)
                return false;
        } else if (!amount.equals(other.amount))
            return false;
        if (currency == null) {
            if (other.currency != null)
                return false;
        } else if (!currency.equals(other.currency))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return amount + " " + currency;
    }

    //Serializable details - BEGIN
    private static final long serialVersionUID = SerializationProxy.serialVersionUID;

    private Object writeReplace() {
        return new SerializationProxy(this);
    }

    private void readObject(ObjectInputStream ois) throws InvalidObjectException {
        throw new InvalidObjectException("Serialization Proxy required");
    }

    /**
     * Serialization proxy (See Effective Java 2nd Ed : Item 78).
     * @author gary
     */
    private static class SerializationProxy implements Serializable {
        private static final long serialVersionUID = -5761795111065859619L;
        private final Currency currency;
        private final BigDecimal amount;

        public SerializationProxy(CurrencyAmount ccyAmount) {
            this.currency = ccyAmount.currency;
            this.amount = ccyAmount.amount;
        }

        private Object readResolve() {
            return new CurrencyAmount(currency, amount);
        }
    }
    //Serializable details - END

}