
com.github.sebhoss.common.annotation.Nullsafe Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common-annotations Show documentation
Show all versions of common-annotations Show documentation
Collection of common annotations for Java projects.
The newest version!
/*
* Copyright © 2013 Sebastian Hoß
* This work is free. You can redistribute it and/or modify it under the
* terms of the Do What The Fuck You Want To Public License, Version 2,
* as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
*/
package com.github.sebhoss.common.annotation;
import javax.annotation.Nullable;
/**
* Utility classes which helps working with legacy (nullable) APIs.
*/
public final class Nullsafe {
/**
* @param reference
* A possible null
reference.
* @return Either the reference itself, or an {@link NullPointerException}, in case the reference was
* null
.
*/
public static T nullsafe(@Nullable final T reference) {
if (reference != null) {
return reference;
}
throw new NullPointerException();
}
/**
* @param reference
* A possible null
reference.
* @param message
* The exception message to throw.
* @return Either the reference itself, or an {@link NullPointerException}, in case the reference was
* null
.
*/
public static T nullsafe(@Nullable final T reference, final String message) {
if (reference != null) {
return reference;
}
throw new NullPointerException(message);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy