2 * Copyright © 2013 Sebastian Hoß <mail@shoss.de>
3 * This work is free. You can redistribute it and/or modify it under the
4 * terms of the Do What The Fuck You Want To Public License, Version 2,
5 * as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
7 package com
.github
.sebhoss
.nullanalysis
;
9 import org
.eclipse
.jdt
.annotation
.NonNull
;
10 import org
.eclipse
.jdt
.annotation
.Nullable
;
13 * Utility classes which helps working with legacy (nullable) APIs.
15 public final class Nullsafe
{
23 * The type of the reference
25 * A possible <code>null</code> reference.
26 * @return Either the reference itself, or an {@link NullPointerException}, in case the reference was
29 public static <T
> @NonNull T
nullsafe(@Nullable final T reference
) {
30 if (reference
!= null) {
34 throw new NullPointerException(); // NOPMD - we want to throw NPE here
39 * The type of the reference
41 * A possible <code>null</code> reference.
43 * The exception message to throw.
44 * @return Either the reference itself, or an {@link NullPointerException}, in case the reference was
47 public static <T
> @NonNull T
nullsafe(@Nullable final T reference
, final String message
) {
48 if (reference
!= null) {
52 throw new NullPointerException(message
); // NOPMD - we want to throw NPE here