Package: NumberServices

NumberServices

nameinstructionbranchcomplexitylinemethod
NumberServices()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
abs(Double)
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%
abs(Integer)
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%
add(Double, Double)
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%
add(Integer, Integer)
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%
div(Double, Double)
M: 18 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
div(Integer, Integer)
M: 17 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
divOp(Double, Double)
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%
divOp(Integer, Integer)
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%
floor(Double)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
floor(Integer)
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
max(Double, Double)
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%
max(Integer, Integer)
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%
min(Double, Double)
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%
min(Integer, Integer)
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%
mod(Double, Double)
M: 19 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
mod(Integer, Integer)
M: 17 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
mult(Double, Double)
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%
mult(Integer, Integer)
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%
round(Double)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
round(Integer)
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
sub(Double, Double)
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%
sub(Integer, Integer)
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%
unaryMin(Double)
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%
unaryMin(Integer)
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%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2015 Obeo.
3: * All rights reserved. This program and the accompanying materials
4: * are made available under the terms of the Eclipse Public License v1.0
5: * which accompanies this distribution, and is available at
6: * http://www.eclipse.org/legal/epl-v10.html
7: *
8: * Contributors:
9: * Obeo - initial API and implementation
10: *******************************************************************************/
11: package org.eclipse.acceleo.query.services;
12:
13: import org.eclipse.acceleo.annotations.api.documentation.Documentation;
14: import org.eclipse.acceleo.annotations.api.documentation.Example;
15: import org.eclipse.acceleo.annotations.api.documentation.Param;
16:
17: /**
18: * Services on {@link Number}.
19: *
20: * @author <a href="mailto:romain.guider@obeo.fr">Romain Guider</a>
21: */
22: @SuppressWarnings({"checkstyle:javadocmethod", "checkstyle:javadoctype" })
23: public class NumberServices {
24:
25:         /**
26:          * Can't divide by zero message.
27:          */
28:         private static final String CAN_T_DIVIDE_BY_ZERO = "Can't divide by zero.";
29:
30:         // @formatter:off
31:         @Documentation(
32:                 value = "Performs the negation of the specified argument.",
33:                 params = {
34:                         @Param(name = "self", value = "The argument to be negated.")
35:                 },
36:                 result = "The negation of the argument.",
37:                 examples = {
38:                         @Example(expression = "1.unaryMin()", result = "-1"),
39:                         @Example(expression = "-1.unaryMin()", result = "1")
40:                 },
41:                 comment = "You can use \"-expression\" for the same result."
42:         )
43:         // @formatter:on
44:         public Integer unaryMin(Integer self) {
45:                 return Integer.valueOf(-self.intValue());
46:         }
47:
48:         /**
49:          * Performs the addition of the specified arguments.
50:          *
51:          * @param a
52:          * the first operand
53:          * @param b
54:          * the second operand
55:          * @return the addition of the arguments.
56:          */
57:         public Integer add(Integer a, Integer b) {
58:                 return Integer.valueOf(a.intValue() + b.intValue());
59:         }
60:
61:         /**
62:          * Performs the substraction of the specified arguments.
63:          *
64:          * @param a
65:          * the first operand
66:          * @param b
67:          * the second operand
68:          * @return the substraction of the arguments.
69:          */
70:         public Integer sub(Integer a, Integer b) {
71:                 return Integer.valueOf(a.intValue() - b.intValue());
72:         }
73:
74:         /**
75:          * Performs the multiplication of the specified arguments.
76:          *
77:          * @param a
78:          * the first operand
79:          * @param b
80:          * the second operand
81:          * @return the multiplication of the arguments.
82:          */
83:
84:         public Integer mult(Integer a, Integer b) {
85:                 return Integer.valueOf(a.intValue() * b.intValue());
86:         }
87:
88:         /**
89:          * Performs the division of the specified arguments.
90:          *
91:          * @param a
92:          * the first operand
93:          * @param b
94:          * the second operand
95:          * @return the division of the arguments.
96:          */
97:
98:         public Integer divOp(Integer a, Integer b) {
99:                 return Integer.valueOf(a.intValue() / b.intValue());
100:         }
101:
102:         /**
103:          * Performs the addition of the specified arguments.
104:          *
105:          * @param a
106:          * the first operand
107:          * @param b
108:          * the second operand
109:          * @return the addition of the arguments.
110:          */
111:
112:         public Double add(Double a, Double b) {
113:                 return Double.valueOf(a.doubleValue() + b.doubleValue());
114:         }
115:
116:         /**
117:          * Performs the substraction of the specified arguments.
118:          *
119:          * @param a
120:          * the first operand
121:          * @param b
122:          * the second operand
123:          * @return the substraction of the arguments.
124:          */
125:
126:         public Double sub(Double a, Double b) {
127:                 return Double.valueOf(a.doubleValue() - b.doubleValue());
128:         }
129:
130:         /**
131:          * Performs the multiplication of the specified arguments.
132:          *
133:          * @param a
134:          * the first operand
135:          * @param b
136:          * the second operand
137:          * @return the multiplication of the arguments.
138:          */
139:
140:         public Double mult(Double a, Double b) {
141:                 return Double.valueOf(a.doubleValue() * b.doubleValue());
142:         }
143:
144:         /**
145:          * Performs the division of the specified arguments.
146:          *
147:          * @param a
148:          * the first operand
149:          * @param b
150:          * the second operand
151:          * @return the division of the arguments.
152:          */
153:         public Double divOp(Double a, Double b) {
154:                 return Double.valueOf(a.doubleValue() / b.doubleValue());
155:         }
156:
157:         // @formatter:off
158:         @Documentation(
159:                 value = "Performs the negation of the specified argument.",
160:                 params = {
161:                         @Param(name = "self", value = "The argument to be negated.")
162:                 },
163:                 result = "The negation of the argument.",
164:                 examples = {
165:                         @Example(expression = "3.14.unaryMin()", result = "-3.14"),
166:                         @Example(expression = "-3.14.unaryMin()", result = "3.14")
167:                 },
168:                 comment = "You can use \"-expression\" for the same result."
169:         )
170:         // @formatter:on
171:         public Double unaryMin(Double value) {
172:                 return Double.valueOf(-value.doubleValue());
173:         }
174:
175:         // @formatter:off
176:         @Documentation(
177:                 value = "Returns the absolute value of self, self if it is already a positive number.",
178:                 params = {
179:                         @Param(name = "self", value = "The current value.")
180:                 },
181:                 result = "The absolute value of self, self if it is already a positive number",
182:                 examples = {
183:                         @Example(expression = "-3.14.abs()", result = "3.14"),
184:                         @Example(expression = "3.14.abs()", result = "3.14")
185:                 }
186:         )
187:         // @formatter:on
188:         public Double abs(Double self) {
189:                 return Double.valueOf(Math.abs(self.doubleValue()));
190:         }
191:
192:         // @formatter:off
193:         @Documentation(
194:                 value = "Returns the absolute value of self, self if it is already a positive number.",
195:                 params = {
196:                         @Param(name = "self", value = "The current value.")
197:                 },
198:                 result = "The absolute value of self, self if it is already a positive number",
199:                 examples = {
200:                         @Example(expression = "-3.abs()", result = "3"),
201:                         @Example(expression = "3.abs()", result = "3")
202:                 }
203:         )
204:         // @formatter:on
205:         public Integer abs(Integer self) {
206:                 return Integer.valueOf(Math.abs(self.intValue()));
207:         }
208:
209:         // @formatter:off
210:         @Documentation(
211:                 value = "Returns the integer part of self.",
212:                 params = {
213:                         @Param(name = "self", value = "The current value.")
214:                 },
215:                 result = "The integer part of self.",
216:                 examples = {
217:                         @Example(expression = "3.14.floor()", result = "3"),
218:                         @Example(expression = "3.66.floor()", result = "3")
219:                 }
220:         )
221:         // @formatter:on
222:         public Integer floor(Double self) {
223:                 return Integer.valueOf((int)Math.floor(self.doubleValue()));
224:         }
225:
226:         // @formatter:off
227:         @Documentation(
228:                 value = "Returns self.",
229:                 params = {
230:                         @Param(name = "self", value = "The current value.")
231:                 },
232:                 result = "Self.",
233:                 examples = {
234:                         @Example(expression = "3.floor()", result = "3")
235:                 }
236:         )
237:         // @formatter:on
238:         public Integer floor(Integer self) {
239:                 return self;
240:         }
241:
242:         // @formatter:off
243:         @Documentation(
244:                 value = "Returns the greatest number between self and r.",
245:                 params = {
246:                                 @Param(name = "self", value = "The current value."),
247:                                 @Param(name = "r", value = "The other value.")
248:                 },
249:                 result = "The greatest number between self and r.",
250:                 examples = {
251:                         @Example(expression = "3.max(6)", result = "6"),
252:                         @Example(expression = "6.max(3)", result = "6")
253:                 }
254:         )
255:         // @formatter:on
256:         public Integer max(Integer self, Integer r) {
257:                 return Integer.valueOf(Math.max(self.intValue(), r.intValue()));
258:         }
259:
260:         // @formatter:off
261:         @Documentation(
262:                 value = "Returns the greatest number between self and r.",
263:                 params = {
264:                                 @Param(name = "self", value = "The current value."),
265:                                 @Param(name = "r", value = "The other value.")
266:                 },
267:                 result = "The greatest number between self and r.",
268:                 examples = {
269:                         @Example(expression = "3.14.max(6.7)", result = "6.7"),
270:                         @Example(expression = "6.7.max(3.14)", result = "6.7")
271:                 }
272:         )
273:         // @formatter:on
274:         public Double max(Double self, Double r) {
275:                 return Double.valueOf(Math.max(self.doubleValue(), r.doubleValue()));
276:         }
277:
278:         // @formatter:off
279:         @Documentation(
280:                 value = "Returns the lowest number between self and r.",
281:                 params = {
282:                                 @Param(name = "self", value = "The current value."),
283:                                 @Param(name = "r", value = "The other value.")
284:                 },
285:                 result = "The lowest number between self and r.",
286:                 examples = {
287:                         @Example(expression = "3.min(6)", result = "3"),
288:                         @Example(expression = "6.min(3)", result = "3")
289:                 }
290:         )
291:         // @formatter:on
292:         public Integer min(Integer self, Integer r) {
293:                 return Integer.valueOf(Math.min(self.intValue(), r.intValue()));
294:         }
295:
296:         // @formatter:off
297:         @Documentation(
298:                 value = "Returns the lowest number between self and r.",
299:                 params = {
300:                                 @Param(name = "self", value = "The current value."),
301:                                 @Param(name = "r", value = "The other value.")
302:                 },
303:                 result = "The lowest number between self and r.",
304:                 examples = {
305:                         @Example(expression = "3.14.min(6.7)", result = "3.14"),
306:                         @Example(expression = "6.7.min(3.14)", result = "3.14")
307:                 }
308:         )
309:         // @formatter:on
310:         public Double min(Double self, Double r) {
311:                 return Double.valueOf(Math.min(self.doubleValue(), r.doubleValue()));
312:         }
313:
314:         // @formatter:off
315:         @Documentation(
316:                 value = "Returns the nearest integer to self.",
317:                 params = {
318:                         @Param(name = "self", value = "The current value.")
319:                 },
320:                 result = "The nearest integer to self.",
321:                 examples = {
322:                         @Example(expression = "3.14.round()", result = "3"),
323:                         @Example(expression = "3.66.round()", result = "4")
324:                 }
325:         )
326:         // @formatter:on
327:         public Integer round(Double self) {
328:                 return Integer.valueOf((int)Math.round(self.doubleValue()));
329:         }
330:
331:         // @formatter:off
332:         @Documentation(
333:                 value = "Returns self.",
334:                 params = {
335:                         @Param(name = "self", value = "The current value.")
336:                 },
337:                 result = "Self.",
338:                 examples = {
339:                         @Example(expression = "3.round()", result = "3")
340:                 }
341:         )
342:         // @formatter:on
343:         public Integer round(Integer self) {
344:                 return self;
345:         }
346:
347:         // @formatter:off
348:         @Documentation(
349:                 value = "Returns the integer quotient of the division of self by i.",
350:                 params = {
351:                                 @Param(name = "self", value = "The current value."),
352:                                 @Param(name = "i", value = "The divider.")
353:                 },
354:                 result = "The integer quotient of the division of self by i.",
355:                 examples = {
356:                         @Example(expression = "6.9.div(3.1)", result = "2")
357:                 }
358:         )
359:         // @formatter:on
360:         public Integer div(Double self, Double i) {
361:                 /*
362:                  * 0d/0d doesn't fail in ArithmeticsException but rather returns Double#POSITIVE_INFINITY. We want the
363:                  * same failure for both operations, hence the explicit test.
364:                  */
365:•                if (i.equals(Double.valueOf(0))) {
366:                         throw new IllegalArgumentException(CAN_T_DIVIDE_BY_ZERO);
367:                 }
368:                 return Integer.valueOf((int)(self.doubleValue() / i.doubleValue()));
369:         }
370:
371:         // @formatter:off
372:         @Documentation(
373:                 value = "Returns the integer quotient of the division of self by i.",
374:                 params = {
375:                                 @Param(name = "self", value = "The current value."),
376:                                 @Param(name = "i", value = "The divider.")
377:                 },
378:                 result = "The integer quotient of the division of self by i.",
379:                 examples = {
380:                         @Example(expression = "7.div(3)", result = "2")
381:                 }
382:         )
383:         // @formatter:on
384:         public Integer div(Integer self, Integer i) {
385:                 // see comment in #div(Double, Double) for this test
386:•                if (i.equals(Integer.valueOf(0))) {
387:                         throw new IllegalArgumentException(CAN_T_DIVIDE_BY_ZERO);
388:                 }
389:                 return Integer.valueOf((int)(self.intValue() / i.intValue()));
390:         }
391:
392:         // @formatter:off
393:         @Documentation(
394:                 value = "Returns the integer remainder of the division of self by i.",
395:                 params = {
396:                                 @Param(name = "self", value = "The current value."),
397:                                 @Param(name = "i", value = "The divider.")
398:                 },
399:                 result = "The integer remainder of the division of self by i.",
400:                 examples = {
401:                         @Example(expression = "7.5.div(3.1)", result = "1")
402:                 }
403:         )
404:         // @formatter:on
405:         public Integer mod(Double self, Double i) {
406:                 /*
407:                  * As with division, mod operation will not fail in exception when using zero as divisor, but rather
408:                  * return Double#NaN. We want this operation to fail as does its version with Integer, hence the
409:                  * explicit test.
410:                  */
411:•                if (i.equals(Double.valueOf(0))) {
412:                         throw new IllegalArgumentException(CAN_T_DIVIDE_BY_ZERO);
413:                 }
414:                 return Integer.valueOf((int)Math.ceil(self.doubleValue() % i.doubleValue()));
415:         }
416:
417:         // @formatter:off
418:         @Documentation(
419:                 value = "Returns the integer remainder of the division of self by i.",
420:                 params = {
421:                                 @Param(name = "self", value = "The current value."),
422:                                 @Param(name = "i", value = "The divider.")
423:                 },
424:                 result = "The integer remainder of the division of self by i.",
425:                 examples = {
426:                         @Example(expression = "7.div(3)", result = "1")
427:                 }
428:         )
429:         // @formatter:on
430:         public Integer mod(Integer self, Integer i) {
431:                 // see comment in #mod(Double, Double) for this test
432:•                if (i.equals(Integer.valueOf(0))) {
433:                         throw new IllegalArgumentException(CAN_T_DIVIDE_BY_ZERO);
434:                 }
435:                 return Integer.valueOf((int)(self.intValue() % i.intValue()));
436:         }
437:
438: }