1 /*
2 * Licensed to the Hipparchus project under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The Hipparchus project licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.hipparchus.util;
18
19 import org.hipparchus.exception.LocalizedCoreFormats;
20 import org.hipparchus.exception.MathIllegalArgumentException;
21
22 /**
23 * Utility class to manage roman numerals.
24 * @since 4.1
25 */
26 public final class RomanNumeral {
27
28 /** Components in decreased value order. */
29 private static final Component[] COMPONENTS = {
30 new Component( "M", 1000),
31 new Component("CM", 900),
32 new Component( "D", 500),
33 new Component("CD", 400),
34 new Component( "C", 100),
35 new Component("XC", 90),
36 new Component( "L", 50),
37 new Component("XL", 40),
38 new Component( "X", 10),
39 new Component("IX", 9),
40 new Component( "V", 5),
41 new Component("IV", 4),
42 new Component( "I", 1)
43 };
44
45 /** Private constructor for a utility class. */
46 private RomanNumeral() {
47 // nothing to do
48 }
49
50 /** Convert an integer to roman numeral.
51 * <p>
52 * The classical subtractive form is generated here,
53 * i.e. 4 is represented as IV and not IIII.
54 * </p>
55 * @param n number to convert
56 * @return string representation as a roman numeral
57 */
58 public static String toRoman(final int n) {
59
60 checkRange(n);
61
62 // build the string representation from left to right
63 final StringBuilder builder = new StringBuilder();
64 int remaining = n;
65 int componentIndex = -1;
66 while (remaining > 0) {
67 Component c = COMPONENTS[++componentIndex];
68 while (remaining >= c.value) {
69 builder.append(c.roman);
70 remaining -= c.value;
71 }
72 }
73
74 return builder.toString();
75
76 }
77
78 /** Convert roman numeral to an integer.
79 * <p>
80 * We accept here the additive form (IIII instead of IV).
81 * </p>
82 * @param r roman numeral to convert
83 * @return value of the roman numeral
84 */
85 public static int parse(final String r) {
86
87 // special handling
88 if (r.isEmpty()) {
89 throw new MathIllegalArgumentException(LocalizedCoreFormats.INVALID_ROMAN_NUMERAL, r);
90 }
91
92 int n = 0;
93
94 // parse components
95 int stringIndex = 0;
96 int componentIndex = -1;
97 while (stringIndex < r.length() && componentIndex < COMPONENTS.length - 1) {
98 Component c = COMPONENTS[++componentIndex];
99 while (r.substring(stringIndex).startsWith(c.roman)) {
100 n += c.value;
101 stringIndex += c.roman.length();
102 if (c.roman.length() > 1) {
103 // subtractive components like IV cannot be repeated
104 break;
105 }
106 }
107 }
108
109 if (stringIndex < r.length()) {
110 // we were not able to parse the complete string
111 throw new MathIllegalArgumentException(LocalizedCoreFormats.INVALID_ROMAN_NUMERAL, r);
112 }
113
114 // a posteriori check
115 checkRange(n);
116
117 return n;
118
119 }
120
121 /** Check the range of a number.
122 * @param n number to check
123 */
124 private static void checkRange(final int n) {
125 if (n < 1 || n > 3999) {
126 throw new MathIllegalArgumentException(LocalizedCoreFormats.ROMAN_NUMERAL_RANGE, n);
127 }
128 }
129
130 /** Container for roman numerals. */
131 private static class Component {
132
133 /** Roman representation. */
134 private final String roman;
135
136 /** Integer value. */
137 private final int value;
138
139 /** Simple constructor.
140 * @param roman roman representation
141 * @param value integer value
142 */
143 private Component(final String roman, final int value) {
144 this.roman = roman;
145 this.value = value;
146 }
147
148 }
149
150 }