View Javadoc
1   package org.apache.maven.doxia.module.rtf;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  /**
23   * @version $Id: RomanNumerals.java 1438269 2013-01-24 23:47:50Z olamy $
24   */
25  class RomanNumerals
26  {
27      private static final int[] NUMBERS = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
28  
29      private static final String[] UPPER_CASE_LETTERS =
30          {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
31  
32      private static final String[] LOWER_CASE_LETTERS =
33          {"m", "cm", "d", "cd", "c", "xc", "l", "xl", "x", "ix", "v", "iv", "i"};
34  
35      // -----------------------------------------------------------------------
36  
37      static String toString( int n )
38      {
39          return toString( n, false );
40      }
41  
42      static String toString( int n, boolean lowerCase )
43      {
44          StringBuilder roman = new StringBuilder();
45          String[] letters = lowerCase ? LOWER_CASE_LETTERS : UPPER_CASE_LETTERS;
46  
47          for ( int i = 0; i < NUMBERS.length; ++i )
48          {
49              while ( n >= NUMBERS[i] )
50              {
51                  roman.append( letters[i] );
52                  n -= NUMBERS[i];
53              }
54          }
55  
56          return roman.toString();
57      }
58  }