Skip to content

Package: Fraction

Fraction

nameinstructionbranchcomplexitylinemethod
Fraction(BigInteger, BigInteger)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
add(Fraction)
M: 6 C: 18
75%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 3
60%
M: 0 C: 1
100%
compareTo(Fraction)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
create(BigInteger, BigInteger)
M: 0 C: 16
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
divide(Fraction)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
doubleValue()
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
equals(Object)
M: 2 C: 18
90%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 3
75%
M: 0 C: 1
100%
floatValue()
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getFractionStyle(String)
M: 0 C: 44
100%
M: 0 C: 10
100%
M: 0 C: 6
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
hashCode()
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
intValue()
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
inversion()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lessOrEqual(Fraction)
M: 0 C: 8
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
longValue()
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
multiply(Fraction)
M: 6 C: 12
67%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 2
50%
M: 0 C: 1
100%
normalize()
M: 0 C: 41
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
pow(BigInteger)
M: 0 C: 40
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
subtract(Fraction)
M: 6 C: 18
75%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 4
67%
M: 0 C: 1
100%
toString()
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
valueOf(Number)
M: 5 C: 4
44%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 1
33%
M: 0 C: 1
100%
valueOf(String)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: package util;
2:
3: import util.exceptions.FractionConstructionException;
4: import util.fractionStyles.Decimal;
5: import util.fractionStyles.FractionStyle;
6: import util.fractionStyles.Percent;
7: import util.fractionStyles.Science;
8: import util.fractionStyles.SingleNumber;
9: import util.fractionStyles.SlashSeparated;
10:
11: import java.math.BigDecimal;
12: import java.math.BigInteger;
13: import java.math.RoundingMode;
14:
15: /**
16: * Klasse, nie einen tatsächlichen Bruch repräsentiert.
17: */
18: @SuppressWarnings("serial")
19: public final class Fraction extends Number implements Comparable<Fraction> {
20:
21: private static final String REGEX_SCIENCE_FRACTION = "-?[0-9]+E-?[0-9]+";
22:         private static final String REGEX_DECIMAL_FRACTION = "-?[0-9]*[.,][0-9]+";
23:         private static final String REGEX_SINGLENUM_FRACTION = "-?[0-9]+\\.?";
24:         private static final String REGEX_SLASH_SEPERATED_FRACTION = "-?[0-9]*/-?[0-9]+";
25:         private static final String REGEX_FRACTION_SEPERATOR = "\\.|/|,|E";
26:         public static final Fraction ONE = Fraction.valueOf(1);
27:         public static final Fraction ZERO = Fraction.valueOf(0);
28:         public static final long HUNDRED = 100;
29:         private static final String REGEX_PERCENT_FRACTION = "\\d+%";
30: private static final int SCALE = 15;
31:
32: private final BigInteger enumerator;
33:         private final BigInteger denominator;
34:
35:         /**
36:          * Creates a new normalized Fraction.
37:          *
38:          * @param enumerator
39:          *                 enumerator of the Fraction
40:          * @param denominator
41:          *                 denominator of the Fraction
42:          *
43:          * @return normalized Fraction
44:          * @throws FractionConstructionException
45:          *                 , falls kein Bruch aus dem gegebenen Zaehler und Nenner erstellt werden kann. falls 0 im Nenner des Fractions ist.
46:          */
47:         public static Fraction create(final BigInteger enumerator, final BigInteger denominator)
48:                         throws FractionConstructionException {
49:•                if (denominator.equals(BigInteger.ZERO)) {
50:                         throw new FractionConstructionException("0 im Nenner!!!!!");
51:                 }
52:                 return new Fraction(enumerator, denominator).normalize();
53:         }
54:
55:         /**
56:          * normalizes the provided Fraction.
57:          *
58:          * @return a new normalized Fraction
59:          */
60:         private Fraction normalize() {
61:                 final BigInteger ggt = this.denominator.gcd(this.enumerator);
62:•                if (this.denominator.signum() == -1) {
63:                         return new Fraction(this.enumerator.divide(ggt).multiply(BigInteger.valueOf(-1)),
64:                                         this.denominator.divide(ggt).multiply(BigInteger.valueOf(-1)));
65:                 }
66:                 return new Fraction(this.enumerator.divide(ggt), this.denominator.divide(ggt));
67:         }
68:
69:         /**
70:          * creates a new normalized Fraction represented by the provided String.
71:          *
72:          * @param fraction
73:          *                 provided String
74:          *
75:          * @return normalized Fraction
76:          * @throws FractionConstructionException
77:          *                 , falls kein Bruch aus dem gegebenen Zaehler und Nenner erstellt werden kann.
78:          * @throws IllegalArgumentException
79:          *                 , wenn der eingegebene Bruch nicht einem der bekannten Formate entspricht.
80:          */
81:         public static Fraction valueOf(final String fraction)
82:                         throws FractionConstructionException, IllegalArgumentException {
83:                 return Fraction.getFractionStyle(fraction).parse(fraction);
84:         }
85:
86:         /**
87:          * Checks the provided String for the different Fraction-Styles that can be provided in a String.
88:          *
89:          * @param inputString
90:          *                 the provided String
91:          *
92:          * @return the correct FractionStyle
93:          */
94:         private static FractionStyle getFractionStyle(final String inputString) throws IllegalArgumentException {
95:•                if (inputString.matches(Fraction.REGEX_SLASH_SEPERATED_FRACTION)) {
96:                         return SlashSeparated.getInstance();
97:•                } else if (inputString.matches(Fraction.REGEX_SINGLENUM_FRACTION)) {
98:                         return SingleNumber.getInstance();
99:•                } else if (inputString.matches(Fraction.REGEX_DECIMAL_FRACTION)) {
100:                         return Decimal.getInstance();
101:•                } else if (inputString.matches(Fraction.REGEX_SCIENCE_FRACTION)) {
102:                         return Science.getInstance();
103:•                } else if (inputString.matches(Fraction.REGEX_PERCENT_FRACTION)) {
104:                         return Percent.getInstance();
105:                 } else {
106:                         throw new IllegalArgumentException("Die Eingabe " + inputString + " ist ungültig!");
107:                 }
108:         }
109:
110:         /**
111:          * returns the Fraction represented by the provided Number .
112:          *
113:          * @param value
114:          *                 provided number
115:          *
116:          * @return normalized Fraction
117:          */
118:         public static Fraction valueOf(final Number value) {
119:                 try {
120:                         return Fraction.valueOf(value.toString());
121:                 } catch (final FractionConstructionException e) {
122:                         throw new Error();
123:                 }
124:         }
125:
126:         /**
127:          * Constructor of the class.
128:          *
129:          * @param enumerator
130:          *                 enumerator of the Fraction
131:          * @param denominator
132:          *                 denominator of the Fraction
133:          */
134:         private Fraction(final BigInteger enumerator, final BigInteger denominator) {
135:                 this.enumerator = enumerator;
136:                 this.denominator = denominator;
137:         }
138:
139:         /**
140:          * Simple addition on Fraction.
141:          *
142:          * @param summand
143:          *                 summand added to this
144:          *
145:          * @return this + summand
146:          */
147:         public Fraction add(final Fraction summand) {
148:                 try {
149:                         return Fraction.create(
150:                                         this.enumerator.multiply(summand.denominator).add(this.denominator.multiply(summand.enumerator)),
151:                                         this.denominator.multiply(summand.denominator));
152:                 } catch (final FractionConstructionException e) {
153:                         throw new Error(e);
154:                 }
155:         }
156:
157:         /**
158:          * Simple multiplication on Fraction.
159:          *
160:          * @param factor
161:          *                 factor multiplied with this
162:          *
163:          * @return this * factor
164:          */
165:         public Fraction multiply(final Fraction factor) {
166:                 try {
167:                         return Fraction
168:                                         .create(this.enumerator.multiply(factor.enumerator), this.denominator.multiply(factor.denominator));
169:                 } catch (final FractionConstructionException e) {
170:                         throw new Error(e);
171:                 }
172:         }
173:
174:         /**
175:          * Simple substraction on Fraction .
176:          *
177:          * @param subtrahend
178:          *                 subtrahend to be substracted from this
179:          *
180:          * @return this - subtrahend
181:          */
182:         public Fraction subtract(final Fraction subtrahend) {
183:                 try {
184:                         return Fraction.create(
185:                                         this.enumerator.multiply(subtrahend.denominator)
186:                                                         .subtract(this.denominator.multiply(subtrahend.enumerator)),
187:                                         this.denominator.multiply(subtrahend.denominator));
188:                 } catch (final FractionConstructionException e) {
189:                         throw new Error(e);
190:                 }
191:         }
192:
193:         /**
194:          * Simple division on Fraction.
195:          *
196:          * @param divisor
197:          *                 divisor to be divided from this
198:          *
199:          * @return this / divisor
200:          * @throws FractionConstructionException
201:          *                 , falls kein Bruch aus dem gegebenen Zaehler und Nenner erstellt werden kann.
202:          */
203:         public Fraction divide(final Fraction divisor) throws FractionConstructionException {
204:                 return this.multiply(divisor.inversion());
205:         }
206:
207:         /**
208:          * @param exponent
209:          *                 the exponent to which this Fraction is to be raised
210:          *
211:          * @return a Fraction whose value is (thise<sup>xponent</sup>)
212:          * @throws FractionConstructionException
213:          *                 , falls kein Bruch aus dem gegebenen Zaehler und Nenner erstellt werden kann.
214:          */
215:         public Fraction pow(final BigInteger exponent) throws FractionConstructionException {
216:•                if (exponent.equals(BigInteger.ZERO)) {
217:                         return Fraction.valueOf(1);
218:                 } else {
219:                         final Fraction value;
220:•                        value = exponent.signum() < 0 ? this.inversion() : this;
221:
222:                         final Fraction divideAndConquer = value.pow(exponent.divide(BigInteger.valueOf(2)));
223:                         final Fraction preResult = divideAndConquer.multiply(divideAndConquer);
224:
225:•                        return exponent.mod(BigInteger.valueOf(2)).equals(BigInteger.ZERO) ? preResult : preResult.multiply(this);
226:                 }
227:         }
228:
229:         /**
230:          * @return Kehrwert des Bruches.
231:          * @throws FractionConstructionException
232:          *                 , falls kein Bruch aus dem gegebenen Zaehler und Nenner erstellt werden kann.
233:          */
234:         public Fraction inversion() throws FractionConstructionException {
235:                 return Fraction.create(this.denominator, this.enumerator);
236:         }
237:
238:
239:         @Override
240:         public boolean equals(final Object object) {
241:•                if (object instanceof Fraction) {
242:                         final Fraction arg = (Fraction) object;
243:                         return this.enumerator.multiply(arg.denominator).equals(arg.enumerator.multiply(this.denominator));
244:                 } else {
245:                         return false;
246:                 }
247:         }
248:
249:
250:         @Override
251:         public int hashCode() {
252:                 return this.normalize().enumerator.hashCode();
253:         }
254:
255:         @Override
256:         public String toString() {
257:                 return this.enumerator + "/" + this.denominator;
258:         }
259:
260:         @Override
261:         public int compareTo(final Fraction arg) {
262:                 return this.enumerator.multiply(arg.denominator).compareTo(arg.enumerator.multiply(this.denominator));
263:         }
264:
265:         /**
266:          * @param arg
267:          *                 anderer Bruch
268:          *
269:          * @return true, wenn der Bruch kleiner oder gleich dem anderen Bruch ist.
270:          */
271:         public boolean lessOrEqual(final Fraction arg) {
272:•                return this.compareTo(arg) <= 0;
273:         }
274:
275:         @Override
276:         public int intValue() {
277:                 return this.enumerator.divide(this.denominator).intValue();
278:         }
279:
280:         @Override
281:         public long longValue() {
282:                 return this.enumerator.divide(this.denominator).longValue();
283:         }
284:
285:         @Override
286:         public float floatValue() {
287:                 return new BigDecimal(this.enumerator).divide(new BigDecimal(this.denominator), SCALE, RoundingMode.HALF_UP).floatValue();
288:         }
289:
290:         @Override
291:         public double doubleValue() {
292:                 return new BigDecimal(this.enumerator).divide(new BigDecimal(this.denominator), SCALE, RoundingMode.HALF_UP).doubleValue();
293:         }
294: }