1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.common
.core
;
17 import org
.eclipse
.jdt
.annotation
.NonNull
;
18 import org
.eclipse
.jdt
.annotation
.Nullable
;
21 * Utility methods to handle {@link org.eclipse.jdt.annotation.NonNull}
24 * @author Alexandre Montplaisir
26 public final class NonNullUtils
{
28 private NonNullUtils() {
32 * Returns a non-null {@link String} for a potentially null object. This
33 * method calls {@link Object#toString()} if the object is not null, or
34 * returns an empty string otherwise.
37 * A {@link Nullable} object that we want converted to a string
38 * @return The non-null string
40 public static String
nullToEmptyString(@Nullable Object obj
) {
42 return ""; //$NON-NLS-1$
44 String str
= obj
.toString();
45 return (str
== null ?
"" : str
); //$NON-NLS-1$
49 * Checks equality with two nullable objects
52 * the first object to compare
54 * the second object to compare
55 * @return true if o1.equals(o2) or o1 == o2
58 public static boolean equalsNullable(final @Nullable Object o1
, final @Nullable Object o2
) {
68 // ------------------------------------------------------------------------
69 // checkNotNull() methods, to convert @Nullable references to @NonNull ones
70 // ------------------------------------------------------------------------
73 * Convert a non-annotated object reference to a {@link NonNull} one.
75 * If the reference is actually null, a {@link NullPointerException} is
76 * thrown. This is usually more desirable than letting an unwanted null
77 * reference go through and fail much later.
80 * The object that is supposed to be non-null
81 * @return A {@link NonNull} reference to the same object
82 * @throws NullPointerException
83 * If the reference was actually null
85 public static <T
> @NonNull T
checkNotNull(@Nullable T obj
) {
87 throw new NullPointerException();
93 * Convert a non-annotated [] array reference to a @NonNull one.
95 * Note that this method does not check the array contents itself, which can
96 * still contain null elements.
99 * The array whose reference should not be null
100 * @return A {@link NonNull} reference to the array
101 * @throws NullPointerException
102 * If the reference was actually null
105 public static <@Nullable T
> T
[] checkNotNull(T
@Nullable [] array
) {
107 throw new NullPointerException();
113 * Convert a non-annotated {@link Iterable} to a NonNull one.
115 * Note that, unlike {{@link #checkNotNull(Object)}}, this method does not
116 * check the contents itself, which can still contain null elements.
119 * The iterable whose reference should not be null
120 * @return A {@link NonNull} reference to the Iterable. The original class
122 * @throws NullPointerException
123 * If the reference was actually null
126 public static <@Nullable T
, C
extends Iterable
<T
>> C
checkNotNull(@Nullable C container
) {
127 if (container
== null) {
128 throw new NullPointerException();
134 * Convert a non-annotated {@link Map} to a NonNull one.
136 * Note that, unlike {{@link #checkNotNull(Object)}}, this method does not
137 * check the keys or values themselves, which can still contain null
141 * The map whose reference should not be null
142 * @return A {@link NonNull} reference to the Map
143 * @throws NullPointerException
144 * If the reference was actually null
147 public static <@Nullable K
, @Nullable V
, M
extends Map
<K
, V
>> M
checkNotNull(@Nullable M map
) {
149 throw new NullPointerException();
This page took 0.033913 seconds and 5 git commands to generate.