Expose property name when checking key components
[mdsal.git] / binding / yang-binding / src / main / java / org / opendaylight / yangtools / yang / binding / CodeHelpers.java
index 5d224d300a55f41f627bd58220a356eb1de20b48..1952e10cf536202d01721bf47df4aec894856825 100644 (file)
@@ -53,13 +53,28 @@ public final class CodeHelpers {
         checkArgument(expression, "expected one of: %s \n%but was: %s", options, value);
     }
 
+    /**
+     * A shortcut for {@code Preconditions.checkNotNull(value, "Key component \"%s\" must not be null", name)}.
+     *
+     * @param value Value itself
+     * @param name Name of the value
+     * @return Non-null value
+     * @throws NullPointerException if value is null
+     */
+    public static <T> @NonNull T requireKeyProp(final @Nullable T value, final @NonNull String name) {
+        if (value == null) {
+            throw new NullPointerException("Key component \"" + name + "\" may not be null");
+        }
+        return value;
+    }
+
     /**
      * A shortcut for {@code Objects.requireNonNull(value, "Supplied value may not be null")}.
      *
      * @param value Value itself
      * @throws NullPointerException if value is null
      */
-    public static void requireValue(@Nullable final Object value) {
+    public static void requireValue(final @Nullable Object value) {
         requireNonNull(value, "Supplied value may not be null");
     }