Source Code Cross Referenced for RomanTransformer.java in  » Database-ORM » MMBase » org » mmbase » util » transformers » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database ORM » MMBase » org.mmbase.util.transformers 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:
003:        This software is OSI Certified Open Source Software.
004:        OSI Certified is a certification mark of the Open Source Initiative.
005:
006:        The license (Mozilla version 1.0) can be read at the MMBase site.
007:        See http://www.MMBase.org/license
008:
009:         */
010:        package org.mmbase.util.transformers;
011:
012:        import java.util.regex.*;
013:
014:        /**
015:         * Static utilities to deal with roman numbers, and non static functions to transform strings
016:         * representing decimal numbers to roman numbers and back.
017:         *
018:         * @author Michiel Meeuwissen
019:         * @since MMBase-1.8
020:         * @version $Id: RomanTransformer.java,v 1.6 2007/08/04 08:09:14 michiel Exp $
021:         */
022:
023:        public class RomanTransformer extends StringTransformer {
024:
025:            public static final Pattern NUMERIC = Pattern.compile("\\d+");
026:            public static final Pattern ROMAN = Pattern
027:                    .compile("(?i)[ivxlcdm]+");
028:
029:            /**
030:             * Constants for roman numbers
031:             */
032:            public static final int I = 1, V = 5, X = 10, L = 50, C = 100,
033:                    D = 500, M = 1000;
034:
035:            /**
036:             * Converts one of the letters from the roman number system to an int.
037:             * @return <code>0</code> if could not be converted
038:             */
039:            public static int romanToDecimal(char r) {
040:                if (r == 'i')
041:                    return I;
042:                if (r == 'v')
043:                    return V;
044:                if (r == 'x')
045:                    return X;
046:                if (r == 'l')
047:                    return L;
048:                if (r == 'c')
049:                    return C;
050:                if (r == 'd')
051:                    return D;
052:                if (r == 'm')
053:                    return M;
054:                return 0;
055:            }
056:
057:            /**
058:             * Converts an integer to one the letters of the roman number system, or ' ' if no such number.
059:             * @see #decimalToRoman(int)
060:             */
061:
062:            public static char decimalToRomanDigit(int i) {
063:                switch (i) {
064:                case M:
065:                    return 'm';
066:                case D:
067:                    return 'd';
068:                case C:
069:                    return 'c';
070:                case L:
071:                    return 'l';
072:                case X:
073:                    return 'x';
074:                case V:
075:                    return 'v';
076:                case I:
077:                    return 'i';
078:                default:
079:                    return ' ';
080:                }
081:            }
082:
083:            /**
084:             * Converts roman number to int.
085:             */
086:            public static int romanToDecimal(String roman) {
087:                roman = roman.toLowerCase();
088:                int tot = 0;
089:                int mode = I;
090:                for (int i = roman.length() - 1; i >= 0; i--) {
091:                    int value = romanToDecimal(roman.charAt(i));
092:
093:                    if (value > mode)
094:                        mode = value;
095:                    if (value < mode) {
096:                        tot -= value;
097:                    } else {
098:                        tot += value;
099:                    }
100:                }
101:
102:                return tot;
103:            }
104:
105:            /**
106:             * Converts int to roman number (if bigger than 0, smaller then 4000), other wise return the
107:             * integer as a string.
108:             */
109:            public static String decimalToRoman(int value) {
110:                if (value < 1 || value > 3999) {
111:                    // throw new IllegalArgumentException("Only natural numbers smaller than 4000 can be
112:                    // presented as a roman number");
113:                    return "" + value;
114:                }
115:                final StringBuilder buf = new StringBuilder();
116:                int mode = M;
117:                while (value > 0) {
118:                    while (value < mode)
119:                        mode /= 10;
120:                    if (value >= 9 * mode && mode < M) {
121:                        buf.append(decimalToRomanDigit(mode));
122:                        buf.append(decimalToRomanDigit(mode * 10));
123:                        value -= 9 * mode;
124:                        continue;
125:                    }
126:                    if (value >= 4 * mode && mode < M) {
127:                        if (value < 5 * mode) {
128:                            buf.append(decimalToRomanDigit(mode));
129:                            value += mode;
130:                        }
131:                        buf.append(decimalToRomanDigit(5 * mode));
132:                        value -= 5 * mode;
133:                    }
134:                    while (value >= mode) {
135:                        buf.append(decimalToRomanDigit(mode));
136:                        value -= mode;
137:                    }
138:                }
139:                return buf.toString();
140:            }
141:
142:            // javadoc inherited
143:            public String transform(String r) {
144:                try {
145:                    int i = Integer.parseInt(r);
146:                    return decimalToRoman(i);
147:                } catch (Exception e) {
148:                    return r;
149:                }
150:            }
151:
152:            // javadoc inherited
153:            public String transformBack(String r) {
154:                return "" + romanToDecimal(r);
155:            }
156:
157:            public String toString() {
158:                return "ROMAN";
159:            }
160:
161:            /**
162:             * Just to test
163:             */
164:            public static void main(String argv[]) {
165:                if (argv.length == 0) {
166:                    System.out.println("Use roman or decimal argument");
167:                    return;
168:                }
169:                if (argv.length == 1) {
170:                    if (NUMERIC.matcher(argv[0]).matches()) {
171:                        System.out.println(decimalToRoman(Integer
172:                                .parseInt(argv[0])));
173:                    } else {
174:                        System.out.println(romanToDecimal(argv[0]));
175:                    }
176:                }
177:                if (argv.length == 2) {
178:                    int start = NUMERIC.matcher(argv[0]).matches() ? Integer
179:                            .parseInt(argv[0]) : romanToDecimal(argv[0]);
180:                    int end = NUMERIC.matcher(argv[1]).matches() ? Integer
181:                            .parseInt(argv[1]) : romanToDecimal(argv[1]);
182:                    for (int i = start; i <= end; i++) {
183:                        System.out.println(decimalToRoman(i));
184:                    }
185:                }
186:            }
187:
188:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.