Package: ComparableServices

ComparableServices

nameinstructionbranchcomplexitylinemethod
ComparableServices()
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%
greaterThan(Comparable, Comparable)
M: 10 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
greaterThanEqual(Comparable, Comparable)
M: 10 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lessThan(Comparable, Comparable)
M: 10 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lessThanEqual(Comparable, Comparable)
M: 10 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
safeCompare(Comparable, Comparable)
M: 19 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 8 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: import org.eclipse.acceleo.annotations.api.documentation.ServiceProvider;
17:
18: //@formatter:off
19: @ServiceProvider(
20:         value = "Services available for Comparables"
21: )
22: //@formatter:on
23: @SuppressWarnings({"checkstyle:javadocmethod", "checkstyle:javadoctype" })
24: public class ComparableServices {
25:
26:         /**
27:          * {@link Comparable#compareTo(Object) Compares} <code>a</code> to <code>b</code> and takes care of
28:          * <code>null</code>.
29:          *
30:          * @param a
31:          * first {@link Comparable} can be <code>null</code>
32:          * @param b
33:          * second {@link Comparable} can be <code>null</code>
34:          * @return {@link Comparable#compareTo(Object) Compares} <code>a</code> to <code>b</code> and takes care
35:          * of <code>null</code>
36:          * @param <T>
37:          * the kind of {@link Comparable}
38:          */
39:         private <T extends Comparable<? super T>> int safeCompare(T a, T b) {
40:                 final int result;
41:
42:•                if (a == null) {
43:•                        if (b == null) {
44:                                 result = 0;
45:                         } else {
46:                                 result = -b.compareTo(a);
47:                         }
48:                 } else {
49:                         result = a.compareTo(b);
50:                 }
51:
52:                 return result;
53:         }
54:
55:         // @formatter:off
56:         @Documentation(
57:                 value = "Compares \"a\" to \"b\" and return \"true\" if \"a\" is less than \"b\".",
58:          params = {
59:                         @Param(name = "a", value = "The first comparable (can be null)"),
60:                         @Param(name = "b", value = "The second comparable (can be null)")
61:                 },
62:                 result = "\"true\" \"a\" is less than \"b\", \"false\" otherwise.",
63:                 examples = {
64:                                 @Example(expression = "'Hello'.lessThan('Hello')", result = "false"),
65:                                 @Example(expression = "'Hello'.lessThan('World')", result = "true")
66:                 }
67:         )
68:         // @formatter:on
69:         public <T extends Comparable<? super T>> Boolean lessThan(T a, T b) {
70:•                return Boolean.valueOf(safeCompare(a, b) < 0);
71:         }
72:
73:         // @formatter:off
74:         @Documentation(
75:                 value = "Compares \"a\" to \"b\" and return \"true\" if \"a\" is less than or equal to \"b\".",
76:          params = {
77:                         @Param(name = "a", value = "The first comparable (can be null)"),
78:                         @Param(name = "b", value = "The second comparable (can be null)")
79:                 },
80:                 result = "\"true\" \"a\" is less than or equal to \"b\", \"false\" otherwise.",
81:                 examples = {
82:                         @Example(expression = "'Hello'.lessThanEqual('Hello')", result = "true"),
83:                         @Example(expression = "'Hello'.lessThanEqual('World')", result = "true")
84:                 }
85:         )
86:         // @formatter:on
87:         public <T extends Comparable<? super T>> Boolean lessThanEqual(T a, T b) {
88:•                return Boolean.valueOf(safeCompare(a, b) <= 0);
89:         }
90:
91:         // @formatter:off
92:         @Documentation(
93:                 value = "Compares \"a\" to \"b\" and return \"true\" if \"a\" is greater than \"b\".",
94:          params = {
95:                         @Param(name = "a", value = "The first comparable (can be null)"),
96:                         @Param(name = "b", value = "The second comparable (can be null)")
97:                 },
98:                 result = "\"true\" \"a\" is greater than \"b\", \"false\" otherwise.",
99:                 examples = {
100:                         @Example(expression = "'Hello'.greaterThan('Abc')", result = "true"),
101:                         @Example(expression = "'Hello'.greaterThan('Hello')", result = "false")
102:                 }
103:         )
104:         // @formatter:on
105:         public <T extends Comparable<? super T>> Boolean greaterThan(T a, T b) {
106:•                return Boolean.valueOf(safeCompare(a, b) > 0);
107:         }
108:
109:         // @formatter:off
110:         @Documentation(
111:                 value = "Compares \"a\" to \"b\" and return \"true\" if \"a\" is greater than or equal to \"b\".",
112:          params = {
113:                         @Param(name = "a", value = "The first comparable (can be null)"),
114:                         @Param(name = "b", value = "The second comparable (can be null)")
115:                 },
116:                 result = "\"true\" \"a\" is greater than or equal to \"b\", \"false\" otherwise.",
117:                 examples = {
118:                         @Example(expression = "'Hello'.greaterThanEqual('Abc')", result = "true"),
119:                         @Example(expression = "'Hello'.greaterThanEqual('Hello')", result = "true")
120:                 }
121:         )
122:         // @formatter:on
123:         public <T extends Comparable<? super T>> Boolean greaterThanEqual(T a, T b) {
124:•                return Boolean.valueOf(safeCompare(a, b) >= 0);
125:         }
126:
127: }