All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.docx4j.model.listnumbering.NumberFormatRomanAbstract Maven / Gradle / Ivy

Go to download

docx4j is a library which helps you to work with the Office Open XML file format as used in docx documents, pptx presentations, and xlsx spreadsheets.

There is a newer version: 11.4.11
Show newest version
package org.docx4j.model.listnumbering;

//File   : gui/componenents/calculators/Roman.java
//Description: A static method for converting binary integers to Roman numbers.
//Illustrates: Static inner value class, StringBuffer, throw exceptions.
//Author : Fred Swartz - 2006-12-29 - Placed in public domain

//JH comment - under the MIT license, according to the footer at
// http://leepoint.net/notes-java/examples/components/romanNumerals/romanNumeral.html

/////////////////////////////////////////////////////////////////// class Roman
public abstract class NumberFormatRomanAbstract extends NumberFormat {
 //================================================================ constant
 // This could be alternatively be done with parallel arrays.
 // Another alternative would be Pair
  RomanValue[] ROMAN_VALUE_TABLE; 
 
 //============================================================== int2roman
 public String format(int n) {
     if (n >= 4000  || n < 1) {
         throw new NumberFormatException("Numbers must be in range 1-3999");
     }
     StringBuffer result = new StringBuffer(10);
     
     //... Start with largest value, and work toward smallest.
     for (RomanValue equiv : ROMAN_VALUE_TABLE) {
         //... Remove as many of this value as possible (maybe none).
         while (n >= equiv.intVal) {
             n -= equiv.intVal;            // Subtract value.
             result.append(equiv.romVal);  // Add roman equivalent.
         }
     }
     return result.toString();
 }
 
 ///////////////////////////////////////////////////////// inner value class
 protected static class RomanValue {
     //============================================================== fields
     //... No need to make this fields private because they are
     //    used only in this private value class.
     int    intVal;     // Integer value.
     String romVal;     // Equivalent roman numeral.
     
     //========================================================= constructor
     RomanValue(int dec, String rom) {
         this.intVal = dec;
         this.romVal = rom;
     }
 }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy