1 50 package com.lowagie.text.factories; 51 54 public class RomanNumberFactory { 55 58 private static class RomanDigit { 59 60 61 public char digit; 62 63 64 public int value; 65 66 67 public boolean pre; 68 69 75 RomanDigit(char digit, int value, boolean pre) { 76 this.digit = digit; 77 this.value = value; 78 this.pre = pre; 79 } 80 } 81 82 85 private static final RomanDigit[] roman = { 86 new RomanDigit('m', 1000, false), 87 new RomanDigit('d', 500, false), 88 new RomanDigit('c', 100, true), 89 new RomanDigit('l', 50, false), 90 new RomanDigit('x', 10, true), 91 new RomanDigit('v', 5, false), 92 new RomanDigit('i', 1, true) 93 }; 94 95 100 public static final String getString(int index) { 101 StringBuffer buf = new StringBuffer (); 102 103 if (index < 0) { 105 buf.append('-'); 106 index = -index; 107 } 108 109 if (index > 3000) { 111 buf.append('|'); 112 buf.append(getString(index / 1000)); 113 buf.append('|'); 114 index = index - (index / 1000) * 1000; 116 } 117 118 int pos = 0; 120 while (true) { 121 RomanDigit dig = roman[pos]; 123 while (index >= dig.value) { 125 buf.append(dig.digit); 126 index -= dig.value; 127 } 128 if (index <= 0) { 130 break; 131 } 132 int j = pos; 134 while (!roman[++j].pre); 135 136 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 151 public static final String getLowerCaseString(int index) { 152 return getString(index); 153 } 154 155 160 public static final String getUpperCaseString(int index) { 161 return getString(index).toUpperCase(); 162 } 163 164 169 public static final String getString(int index, boolean lowercase) { 170 if (lowercase) { 171 return getLowerCaseString(index); 172 } 173 else { 174 return getUpperCaseString(index); 175 } 176 } 177 178 181 public static void main(String [] args) { 182 for (int i = 1; i < 2000; i++) { 183 System.out.println(getString(i)); 184 } 185 } 186 } | Popular Tags |