Add non-null getters for leaf objects
[mdsal.git] / binding / yang-binding / src / main / java / org / opendaylight / yangtools / yang / binding / CodeHelpers.java
index bf3afc2945fa9f34743bbdc24d98801ecc51ca45..cbdec28633bcb821483ff24f09c0038695c493fd 100644 (file)
@@ -18,6 +18,7 @@ import com.google.common.collect.ImmutableMap;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
+import java.util.NoSuchElementException;
 import java.util.Objects;
 import java.util.regex.Pattern;
 import org.eclipse.jdt.annotation.NonNull;
@@ -47,6 +48,22 @@ public final class CodeHelpers {
         checkArgument(expression, "expected one of: %s \n%but was: %s", options, value);
     }
 
+    /**
+     * Return value and check whether specified value is null and if so throws exception. This method supports
+     * require default getter methods.
+     *
+     * @param value Value itself
+     * @param name Name of the value
+     * @return Non-null value
+     * @throws NoSuchElementException if value is null
+     */
+    public static <T> @NonNull T require(final @Nullable T value, final @NonNull String name) {
+        if (value == null) {
+            throw new NoSuchElementException("Value of " + name + " is not present");
+        }
+        return value;
+    }
+
     /**
      * A shortcut for {@code Preconditions.checkNotNull(value, "Key component \"%s\" must not be null", name)}.
      *