001/*
002 * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit.
003 *
004 * Based in London, we are world leaders in the design and development
005 * of bespoke applications for the securities financing markets.
006 *
007 * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a>
008 *           ___  _     _           _   _          _
009 *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
010 *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
011 *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
012 *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
013 *                   |__/
014 *
015 *                     www.ObjectLab.co.uk
016 *
017 * $Id$
018 *
019 * Copyright 2006 the original author or authors.
020 *
021 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
022 * use this file except in compliance with the License. You may obtain a copy of
023 * the License at
024 *
025 * http://www.apache.org/licenses/LICENSE-2.0
026 *
027 * Unless required by applicable law or agreed to in writing, software
028 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
029 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
030 * License for the specific language governing permissions and limitations under
031 * the License.
032 */
033package net.objectlab.kit.datecalc.common;
034
035import java.io.Serializable;
036
037/**
038 * Holds only valid unit and TenorCode: Day, Week, Month, Year, Overnight,
039 * Spot.
040 *
041 * @author Benoit Xhenseval
042 *
043 */
044public class Tenor implements Serializable {
045    private static final long serialVersionUID = 1L;
046    private final int units;
047
048    private final TenorCode code;
049
050    public Tenor(final int units, final TenorCode code) {
051        this.units = units;
052        this.code = code;
053    }
054
055    public int getUnits() {
056        return units;
057    }
058
059    public TenorCode getCode() {
060        return code;
061    }
062
063    public boolean hasUnits() {
064        return code.acceptUnits();
065    }
066
067    @Override
068    public String toString() {
069        return (units != 0 ? String.valueOf(units) : "") + code.getCode();
070    }
071
072    // -----------------------------------------------------------------------
073    //
074    // ObjectLab, world leaders in the design and development of bespoke
075    // applications for the securities financing markets.
076    // www.ObjectLab.co.uk
077    //
078    // -----------------------------------------------------------------------
079
080    /**
081     * @param tenor
082     *            the tenor, e.g. 1D, 3W, SP etc
083     * @exception IllegalArgumentException
084     *                if the tenor is not a valid on
085     */
086    public static Tenor valueOf(final String tenor) {
087        final StringBuilder unitsBuf = new StringBuilder();
088        final StringBuilder codeBuf = new StringBuilder();
089        final boolean invalid = false;
090        final int size = tenor.length();
091
092        parseCode(tenor, unitsBuf, codeBuf, invalid, size);
093
094        int parsedUnits = 0;
095        if (unitsBuf.length() > 0) {
096            parsedUnits = Integer.parseInt(unitsBuf.toString());
097        }
098        final TenorCode parsedCode = TenorCode.fromCode(codeBuf.toString());
099        if (parsedCode == null) {
100            throw new IllegalArgumentException("[" + codeBuf + "] is not a valid TenorCode");
101        }
102
103        if (!parsedCode.acceptUnits() && unitsBuf.length() > 0) {
104            throw new IllegalArgumentException("[" + codeBuf + "] does not accept units");
105        }
106
107        if (parsedCode.acceptUnits() && unitsBuf.length() == 0) {
108            throw new IllegalArgumentException("[" + codeBuf + "] requires units");
109        }
110
111        return new Tenor(parsedUnits, parsedCode);
112    }
113
114    private static void parseCode(final String tenor, final StringBuilder unitsBuf, final StringBuilder codeBuf, final boolean invalid,
115            final int size) {
116        for (int i = 0; i < size && !invalid; i++) {
117            final char c = tenor.charAt(i);
118
119            if (c >= '0' && c <= '9') {
120                if (codeBuf.length() != 0) {
121                    throw new IllegalArgumentException("[" + tenor + "] is not a valid tenor");
122                } else {
123                    unitsBuf.append(c);
124                }
125            } else {
126                codeBuf.append(c);
127            }
128        }
129    }
130
131    @Override
132    public int hashCode() {
133        final int prime = 31;
134        int result = 1;
135        result = prime * result + (code == null ? 0 : code.hashCode());
136        result = prime * result + units;
137        return result;
138    }
139
140    @Override
141    public boolean equals(final Object obj) {
142        if (this == obj) {
143            return true;
144        }
145        if (obj == null) {
146            return false;
147        }
148        if (getClass() != obj.getClass()) {
149            return false;
150        }
151        final Tenor other = (Tenor) obj;
152        if (code == null) {
153            if (other.code != null) {
154                return false;
155            }
156        } else if (!code.equals(other.code)) {
157            return false;
158        }
159        return units == other.units;
160    }
161}
162
163/*
164 * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit.
165 *
166 * Based in London, we are world leaders in the design and development
167 * of bespoke applications for the securities financing markets.
168 *
169 * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
170 *           ___  _     _           _   _          _
171 *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
172 *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
173 *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
174 *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
175 *                   |__/
176 *
177 *                     www.ObjectLab.co.uk
178 */