Skip to content

Package: Assert

Assert

nameinstructionbranchcomplexitylinemethod
Assert(Object)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
check()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
create(Object)
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%
notNull()
M: 0 C: 10
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
ofClass(Class)
M: 0 C: 42
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 7
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2015 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Alexandra Buzila - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.common.spi.asserts;
15:
16: /**
17: * Class for checking the validity of a provided object.
18: *
19: * @param <T> the type of the object the <code>Assert</code> class verifies.
20: * @author Alexandra Buzila
21: * @since 1.6
22: *
23: */
24: public final class Assert<T> {
25:
26:         private final T object;
27:
28:         private Assert(T object) {
29:                 this.object = object;
30:         }
31:
32:         /**
33:          * @param object the object that will be checked
34:          * @param <T> the type of the object the <code>Assert</code> class verifies.
35:          * @return a new instance of the <code>Assert</code> class.
36:          */
37:         public static <T> Assert<T> create(T object) {
38:                 return new Assert<T>(object);
39:         }
40:
41:         /**
42:          * Checks if the object is null, in which case an {@link IllegalArgumentException} is thrown.
43:          *
44:          * @return the <code>Assert</code> instance, when no exception is thrown.
45:          */
46:         public Assert<T> notNull() {
47:•                if (object == null) {
48:                         throw new IllegalArgumentException("Object must not be null."); //$NON-NLS-1$
49:                 }
50:                 return this;
51:         }
52:
53:         /**
54:          * Checks if the object has a different type than the Class provided as parameter, in which case an
55:          * {@link IllegalArgumentException} is thrown.
56:          *
57:          * @param clazz the <code>Class</code> against which the type of the object is checked.
58:          * @return the <code>Assert</code> instance, when no exception is thrown.
59:          */
60:         public Assert<?> ofClass(Class<?> clazz) {
61:•                if (clazz == null) {
62:                         throw new IllegalArgumentException("Class must not be null."); //$NON-NLS-1$
63:                 }
64:•                if (!clazz.isInstance(object)) {
65:                         final String message = String.format(
66:•                                "%1$s is not of type %2$s.", object == null ? object : object.getClass().getName(), clazz.getName()); //$NON-NLS-1$
67:                         throw new IllegalArgumentException(message);
68:                 }
69:                 return this;
70:         }
71:
72:         /**
73:          *
74:          * Usage example: <br>
75:          * <code>Assert.create(Object object).notNull().ofClass(Class clazz).check()</code>.
76:          *
77:          * @return the object for which the <code>Assert</code> is created.
78:          *
79:          *
80:          */
81:         public T check() {
82:                 return object;
83:         }
84:
85: }