/*
 This file is part of the software framework JStatCom, 
 a library for building applications for data based analysis.

 Copyright (C) 2005  Markus Kraetzig

 This library 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 2.1 of the
 License, or (at your option) any later version.

 This library 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
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 USA
 */
package com.jstatcom.util;

import java.io.Serializable;

import com.jstatcom.parser.MaturityParser;

/**
 * Immutable representation of a maturity. This class is needed by financial
 * tools. Instances can be created via the constructor or with
 * valueOf from a string.
 * 
 * @author Markus Kraetzig 
 */
public final class Maturity implements Serializable {
    private static final MaturityParser maturityParser = new MaturityParser();

    /**
     * The number of the time units.
     */
    public final int COUNT;

    /**
     * Time unit (day, month, year).
     */
    public final MaturityUnits UNIT;

    private final int hashCode;

    /**
     * Creates a maturity instance.
     * 
     * @param count
     *            number of time units
     * @param unit
     *            time unit
     * @throws IllegalArgumentException
     *             if (unit == null) or
     *             if (count < 0) 
     */
    public Maturity(int count, MaturityUnits unit) {
        if (unit == null)
            throw new IllegalArgumentException("Argument was null.");
        if (count < 0)
            throw new IllegalArgumentException("Count < 0.");

        this.COUNT = count;
        this.UNIT = unit;
        int result = 17;
        result = 37 * result + COUNT;
        result = 37 * result + UNIT.hashCode();
        hashCode = result;
    }

    /**
     * Creates a maturity from a string representation. The general format is
     * x[-,' ']?[DAYS, MONTHS, YEARS], where x is
     * an int. Examples are:
     * 
     * 

* As can be seen, days, months, and years can be parsed from either a * single character or the full name. * * @param toParse * @return maturity instance */ public static Maturity valueOf(String toParse) { try { return maturityParser.parseString(toParse); } catch (Throwable ex) { throw new RuntimeException("Could not parse \"" + toParse + "\" to maturity."); } } /** * Overrides hashCode, because equals was overwritten. The * algorithm was taken from (Joshua Bloch, "Effective Java", Item 8) * . * * @return hash code */ public int hashCode() { return hashCode; } /** * Overrides equals to provide logical comparison of two * different Maturity objects. * * @param o * object to compare with * @return true if COUNT and UNIT * are the same, false otherwise */ public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof Maturity)) return false; Maturity otherMat = (Maturity) o; boolean ret = (COUNT == otherMat.COUNT && UNIT == otherMat.UNIT); return ret; } public String toString() { return COUNT + " " + UNIT; } }

Ads help maintain this website.