fix metadata
[null-analysis.git] / src / main / java / com / github / sebhoss / nullanalysis / Nullsafe.java
blobb233e3e7070f7c9f298459bb89f4ce8a744c5b3b
1 /*
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.
6 */
7 package com.github.sebhoss.nullanalysis;
9 import org.eclipse.jdt.annotation.NonNull;
10 import org.eclipse.jdt.annotation.Nullable;
12 /**
13 * Utility classes which helps working with legacy (nullable) APIs.
15 public final class Nullsafe {
17 private Nullsafe() {
18 // utility class
21 /**
22 * @param <T>
23 * The type of the reference
24 * @param reference
25 * A possible <code>null</code> reference.
26 * @return Either the reference itself, or an {@link NullPointerException}, in case the reference was
27 * <code>null</code>.
29 public static <T> @NonNull T nullsafe(@Nullable final T reference) {
30 if (reference != null) {
31 return reference;
34 throw new NullPointerException(); // NOPMD - we want to throw NPE here
37 /**
38 * @param <T>
39 * The type of the reference
40 * @param reference
41 * A possible <code>null</code> reference.
42 * @param message
43 * The exception message to throw.
44 * @return Either the reference itself, or an {@link NullPointerException}, in case the reference was
45 * <code>null</code>.
47 public static <T> @NonNull T nullsafe(@Nullable final T reference, final String message) {
48 if (reference != null) {
49 return reference;
52 throw new NullPointerException(message); // NOPMD - we want to throw NPE here