RomanNumberFactory.java

1
/*
2
 * $Id: RomanNumberFactory.java 3373 2008-05-12 16:21:24Z xlv $
3
 *
4
 * Copyright 2007 by Bruno Lowagie.
5
 *
6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7
 * (the "License"); you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
9
 *
10
 * Software distributed under the License is distributed on an "AS IS" basis,
11
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
 * for the specific language governing rights and limitations under the License.
13
 *
14
 * The Original Code is 'iText, a free JAVA-PDF library'.
15
 *
16
 * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
17
 * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
18
 * All Rights Reserved.
19
 * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
20
 * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
21
 *
22
 * Contributor(s): all the names of the contributors are added in the source code
23
 * where applicable.
24
 *
25
 * Alternatively, the contents of this file may be used under the terms of the
26
 * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
27
 * provisions of LGPL are applicable instead of those above.  If you wish to
28
 * allow use of your version of this file only under the terms of the LGPL
29
 * License and not to allow others to use your version of this file under
30
 * the MPL, indicate your decision by deleting the provisions above and
31
 * replace them with the notice and other provisions required by the LGPL.
32
 * If you do not delete the provisions above, a recipient may use your version
33
 * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
34
 *
35
 * This library is free software; you can redistribute it and/or modify it
36
 * under the terms of the MPL as stated above or under the terms of the GNU
37
 * Library General Public License as published by the Free Software Foundation;
38
 * either version 2 of the License, or any later version.
39
 *
40
 * This library is distributed in the hope that it will be useful, but WITHOUT
41
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
42
 * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
43
 * details.
44
 *
45
 * If you didn't download this code from the following link, you should check if
46
 * you aren't using an obsolete version:
47
 * http://www.lowagie.com/iText/
48
 */
49
package com.lowagie.text.factories;
50
/**
51
 * This class can produce String combinations representing a roman number.
52
 */
53
public class RomanNumberFactory {
54
    /**
55
     * Helper class for Roman Digits
56
     */
57
    private static class RomanDigit {
58
59
        /** part of a roman number */
60
        public char digit;
61
62
        /** value of the roman digit */
63
        public int value;
64
65
        /** can the digit be used as a prefix */
66
        public boolean pre;
67
68
        /**
69
         * Constructs a roman digit
70
         * @param digit the roman digit
71
         * @param value the value
72
         * @param pre can it be used as a prefix
73
         */
74
        RomanDigit(char digit, int value, boolean pre) {
75
            this.digit = digit;
76
            this.value = value;
77
            this.pre = pre;
78
        }
79
    }
80
81
    /**
82
     * Array with Roman digits.
83
     */
84
    private static final RomanDigit[] roman = {
85
        new RomanDigit('m', 1000, false),
86
        new RomanDigit('d', 500, false),
87
        new RomanDigit('c', 100, true),
88
        new RomanDigit('l', 50, false),
89
        new RomanDigit('x', 10, true),
90
        new RomanDigit('v', 5, false),
91
        new RomanDigit('i', 1, true)
92
    };
93
94
    /**
95
     * Changes an int into a lower case roman number.
96
     * @param index the original number
97
     * @return the roman number (lower case)
98
     */
99
    public static String getString(int index) {
100
        StringBuilder buf = new StringBuilder();
101
102
        // lower than 0 ? Add minus
103 2 1. getString : changed conditional boundary → NO_COVERAGE
2. getString : negated conditional → NO_COVERAGE
        if (index < 0) {
104
            buf.append('-');
105 1 1. getString : removed negation → NO_COVERAGE
            index = -index;
106
        }
107
108
        // greater than 3000
109 2 1. getString : changed conditional boundary → NO_COVERAGE
2. getString : negated conditional → NO_COVERAGE
        if (index > 3000) {
110
            buf.append('|');
111 1 1. getString : Replaced integer division with multiplication → NO_COVERAGE
            buf.append(getString(index / 1000));
112
            buf.append('|');
113
            // remainder
114 3 1. getString : Replaced integer division with multiplication → NO_COVERAGE
2. getString : Replaced integer multiplication with division → NO_COVERAGE
3. getString : Replaced integer subtraction with addition → NO_COVERAGE
            index = index - (index / 1000) * 1000;
115
        }
116
117
        // number between 1 and 3000
118
        int pos = 0;
119
        while (true) {
120
            // loop over the array with values for m-d-c-l-x-v-i
121
            RomanDigit dig = roman[pos];
122
            // adding as many digits as we can
123 2 1. getString : changed conditional boundary → NO_COVERAGE
2. getString : negated conditional → NO_COVERAGE
            while (index >= dig.value) {
124
                buf.append(dig.digit);
125 1 1. getString : Replaced integer subtraction with addition → NO_COVERAGE
                index -= dig.value;
126
            }
127
            // we have the complete number
128 2 1. getString : changed conditional boundary → NO_COVERAGE
2. getString : negated conditional → NO_COVERAGE
            if (index <= 0) {
129
                break;
130
            }
131
            // look for the next digit that can be used in a special way
132
            int j = pos;
133 2 1. getString : Changed increment from 1 to -1 → NO_COVERAGE
2. getString : negated conditional → NO_COVERAGE
            while (!roman[++j].pre);
134
135
            // does the special notation apply?
136 3 1. getString : changed conditional boundary → NO_COVERAGE
2. getString : Replaced integer addition with subtraction → NO_COVERAGE
3. getString : negated conditional → NO_COVERAGE
            if (index + roman[j].value >= dig.value) {
137
                buf.append(roman[j].digit).append(dig.digit);
138 2 1. getString : Replaced integer subtraction with addition → NO_COVERAGE
2. getString : Replaced integer subtraction with addition → NO_COVERAGE
                index -= dig.value - roman[j].value;
139
            }
140 1 1. getString : Changed increment from 1 to -1 → NO_COVERAGE
            pos++;
141
        }
142 1 1. getString : mutated return of Object value for com/lowagie/text/factories/RomanNumberFactory::getString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return buf.toString();
143
    }
144
145
    /**
146
     * Changes an int into a lower case roman number.
147
     * @param index the original number
148
     * @return the roman number (lower case)
149
     */
150
    public static String getLowerCaseString(int index) {
151 1 1. getLowerCaseString : mutated return of Object value for com/lowagie/text/factories/RomanNumberFactory::getLowerCaseString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return getString(index);
152
    }
153
154
    /**
155
     * Changes an int into an upper case roman number.
156
     * @param index the original number
157
     * @return the roman number (lower case)
158
     */
159
    public static String getUpperCaseString(int index) {
160 1 1. getUpperCaseString : mutated return of Object value for com/lowagie/text/factories/RomanNumberFactory::getUpperCaseString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return getString(index).toUpperCase();
161
    }
162
163
    /**
164
     * Changes an int into a roman number.
165
     * @param index the original number
166
     * @return the roman number (lower case)
167
     */
168
    public static String getString(int index, boolean lowercase) {
169 1 1. getString : negated conditional → NO_COVERAGE
        if (lowercase) {
170 1 1. getString : mutated return of Object value for com/lowagie/text/factories/RomanNumberFactory::getString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
            return getLowerCaseString(index);
171
        }
172
        else {
173 1 1. getString : mutated return of Object value for com/lowagie/text/factories/RomanNumberFactory::getString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
            return getUpperCaseString(index);
174
        }
175
    }
176
}

Mutations

103

1.1
Location : getString
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : getString
Killed by : none
negated conditional → NO_COVERAGE

105

1.1
Location : getString
Killed by : none
removed negation → NO_COVERAGE

109

1.1
Location : getString
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : getString
Killed by : none
negated conditional → NO_COVERAGE

111

1.1
Location : getString
Killed by : none
Replaced integer division with multiplication → NO_COVERAGE

114

1.1
Location : getString
Killed by : none
Replaced integer division with multiplication → NO_COVERAGE

2.2
Location : getString
Killed by : none
Replaced integer multiplication with division → NO_COVERAGE

3.3
Location : getString
Killed by : none
Replaced integer subtraction with addition → NO_COVERAGE

123

1.1
Location : getString
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : getString
Killed by : none
negated conditional → NO_COVERAGE

125

1.1
Location : getString
Killed by : none
Replaced integer subtraction with addition → NO_COVERAGE

128

1.1
Location : getString
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : getString
Killed by : none
negated conditional → NO_COVERAGE

133

1.1
Location : getString
Killed by : none
Changed increment from 1 to -1 → NO_COVERAGE

2.2
Location : getString
Killed by : none
negated conditional → NO_COVERAGE

136

1.1
Location : getString
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : getString
Killed by : none
Replaced integer addition with subtraction → NO_COVERAGE

3.3
Location : getString
Killed by : none
negated conditional → NO_COVERAGE

138

1.1
Location : getString
Killed by : none
Replaced integer subtraction with addition → NO_COVERAGE

2.2
Location : getString
Killed by : none
Replaced integer subtraction with addition → NO_COVERAGE

140

1.1
Location : getString
Killed by : none
Changed increment from 1 to -1 → NO_COVERAGE

142

1.1
Location : getString
Killed by : none
mutated return of Object value for com/lowagie/text/factories/RomanNumberFactory::getString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

151

1.1
Location : getLowerCaseString
Killed by : none
mutated return of Object value for com/lowagie/text/factories/RomanNumberFactory::getLowerCaseString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

160

1.1
Location : getUpperCaseString
Killed by : none
mutated return of Object value for com/lowagie/text/factories/RomanNumberFactory::getUpperCaseString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

169

1.1
Location : getString
Killed by : none
negated conditional → NO_COVERAGE

170

1.1
Location : getString
Killed by : none
mutated return of Object value for com/lowagie/text/factories/RomanNumberFactory::getString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

173

1.1
Location : getString
Killed by : none
mutated return of Object value for com/lowagie/text/factories/RomanNumberFactory::getString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.4.2