001: /*
002: * $Id: RomanNumberFactory.java 2685 2007-04-16 12:09:48Z blowagie $
003: * $Name$
004: *
005: * Copyright 2007 by Bruno Lowagie.
006: *
007: * The contents of this file are subject to the Mozilla Public License Version 1.1
008: * (the "License"); you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the License.
014: *
015: * The Original Code is 'iText, a free JAVA-PDF library'.
016: *
017: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
018: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
019: * All Rights Reserved.
020: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
021: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
022: *
023: * Contributor(s): all the names of the contributors are added in the source code
024: * where applicable.
025: *
026: * Alternatively, the contents of this file may be used under the terms of the
027: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
028: * provisions of LGPL are applicable instead of those above. If you wish to
029: * allow use of your version of this file only under the terms of the LGPL
030: * License and not to allow others to use your version of this file under
031: * the MPL, indicate your decision by deleting the provisions above and
032: * replace them with the notice and other provisions required by the LGPL.
033: * If you do not delete the provisions above, a recipient may use your version
034: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
035: *
036: * This library is free software; you can redistribute it and/or modify it
037: * under the terms of the MPL as stated above or under the terms of the GNU
038: * Library General Public License as published by the Free Software Foundation;
039: * either version 2 of the License, or any later version.
040: *
041: * This library is distributed in the hope that it will be useful, but WITHOUT
042: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
043: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
044: * details.
045: *
046: * If you didn't download this code from the following link, you should check if
047: * you aren't using an obsolete version:
048: * http://www.lowagie.com/iText/
049: */
050: package com.lowagie.text.factories;
051:
052: /**
053: * This class can produce String combinations representing a roman number.
054: */
055: public class RomanNumberFactory {
056: /**
057: * Helper class for Roman Digits
058: */
059: private static class RomanDigit {
060:
061: /** part of a roman number */
062: public char digit;
063:
064: /** value of the roman digit */
065: public int value;
066:
067: /** can the digit be used as a prefix */
068: public boolean pre;
069:
070: /**
071: * Constructs a roman digit
072: * @param digit the roman digit
073: * @param value the value
074: * @param pre can it be used as a prefix
075: */
076: RomanDigit(char digit, int value, boolean pre) {
077: this .digit = digit;
078: this .value = value;
079: this .pre = pre;
080: }
081: }
082:
083: /**
084: * Array with Roman digits.
085: */
086: private static final RomanDigit[] roman = {
087: new RomanDigit('m', 1000, false),
088: new RomanDigit('d', 500, false),
089: new RomanDigit('c', 100, true),
090: new RomanDigit('l', 50, false),
091: new RomanDigit('x', 10, true),
092: new RomanDigit('v', 5, false), new RomanDigit('i', 1, true) };
093:
094: /**
095: * Changes an int into a lower case roman number.
096: * @param index the original number
097: * @return the roman number (lower case)
098: */
099: public static final String getString(int index) {
100: StringBuffer buf = new StringBuffer();
101:
102: // lower than 0 ? Add minus
103: if (index < 0) {
104: buf.append('-');
105: index = -index;
106: }
107:
108: // greater than 3000
109: if (index > 3000) {
110: buf.append('|');
111: buf.append(getString(index / 1000));
112: buf.append('|');
113: // remainder
114: index = index - (index / 1000) * 1000;
115: }
116:
117: // number between 1 and 3000
118: int pos = 0;
119: while (true) {
120: // loop over the array with values for m-d-c-l-x-v-i
121: RomanDigit dig = roman[pos];
122: // adding as many digits as we can
123: while (index >= dig.value) {
124: buf.append(dig.digit);
125: index -= dig.value;
126: }
127: // we have the complete number
128: if (index <= 0) {
129: break;
130: }
131: // look for the next digit that can be used in a special way
132: int j = pos;
133: while (!roman[++j].pre)
134: ;
135:
136: // does the special notation apply?
137: if (index + roman[j].value >= dig.value) {
138: buf.append(roman[j].digit).append(dig.digit);
139: index -= dig.value - roman[j].value;
140: }
141: pos++;
142: }
143: return buf.toString();
144: }
145:
146: /**
147: * Changes an int into a lower case roman number.
148: * @param index the original number
149: * @return the roman number (lower case)
150: */
151: public static final String getLowerCaseString(int index) {
152: return getString(index);
153: }
154:
155: /**
156: * Changes an int into an upper case roman number.
157: * @param index the original number
158: * @return the roman number (lower case)
159: */
160: public static final String getUpperCaseString(int index) {
161: return getString(index).toUpperCase();
162: }
163:
164: /**
165: * Changes an int into a roman number.
166: * @param index the original number
167: * @return the roman number (lower case)
168: */
169: public static final String getString(int index, boolean lowercase) {
170: if (lowercase) {
171: return getLowerCaseString(index);
172: } else {
173: return getUpperCaseString(index);
174: }
175: }
176:
177: /**
178: * Test this class using this main method.
179: */
180: public static void main(String[] args) {
181: for (int i = 1; i < 2000; i++) {
182: System.out.println(getString(i));
183: }
184: }
185: }
|