Copyright header update, 2015 edition
[deliverable/tracecompass.git] / org.eclipse.tracecompass.common.core / src / org / eclipse / tracecompass / common / core / NonNullUtils.java
CommitLineData
cd3fbf5c 1/*******************************************************************************
ed902a2b 2 * Copyright (c) 2014, 2015 Ericsson
cd3fbf5c
AM
3 *
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
8 *
9 * Contributors:
10 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
12
13package org.eclipse.tracecompass.common.core;
14
15import org.eclipse.jdt.annotation.NonNull;
16import org.eclipse.jdt.annotation.Nullable;
17
18/**
19 * Utility methods to handle {@link org.eclipse.jdt.annotation.NonNull}
20 * annotations.
21 *
22 * @author Alexandre Montplaisir
23 */
24public final class NonNullUtils {
25
26 private NonNullUtils() {}
27
28 /**
3caabe33
GB
29 * Returns a non-null {@link String} for a potentially null object. This
30 * method calls {@link Object#toString()} if the object is not null, or
31 * returns an empty string otherwise.
cd3fbf5c 32 *
3caabe33
GB
33 * @param obj
34 * A {@link Nullable} object that we want converted to a string
cd3fbf5c
AM
35 * @return The non-null string
36 */
3caabe33
GB
37 public static String nullToEmptyString(@Nullable Object obj) {
38 if (obj == null) {
cd3fbf5c
AM
39 return ""; //$NON-NLS-1$
40 }
3caabe33
GB
41 String str = obj.toString();
42 return (str == null ? "" : str); //$NON-NLS-1$
cd3fbf5c
AM
43 }
44
45 /**
46 * Convert a non-annotated object reference to a {@link NonNull} one.
47 *
48 * If the reference is actually null, a {@link NullPointerException} is
49 * thrown. This is usually more desirable than letting an unwanted null
50 * reference go through and fail much later.
51 *
52 * @param obj
53 * The object that is supposed to be non-null
54 * @return A {@link NonNull} reference to the same object
55 * @throws NullPointerException
56 * If the reference was actually null
57 */
58 public static <T> T checkNotNull(@Nullable T obj) {
59 if (obj == null) {
60 throw new NullPointerException();
61 }
62 return obj;
63 }
64}
This page took 0.025852 seconds and 5 git commands to generate.