Add DataObjectUtils 02/77102/6
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 19 Oct 2018 12:42:06 +0000 (14:42 +0200)
committerTom Pantelis <tompantelis@gmail.com>
Thu, 1 Nov 2018 13:59:12 +0000 (13:59 +0000)
Add DataObjectUtils.nullToEmpty(List), which is useful when dealing
with nullable lists being returned from binding DTOs. This also moves
mdsal.binding.spec.util to yang-binding, so the utilities are
available to all model users.

Change-Id: I681c49141b4e880448747a53d749065208f9aca1
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
12 files changed:
binding/mdsal-binding-spec-util/src/test/java/org/opendaylight/mdsal/binding/spec/reflect/BindingReflectionsTest.java
binding/mdsal-binding-spec-util/src/test/java/org/opendaylight/mdsal/binding/spec/reflect/FooChild.java [new file with mode: 0644]
binding/mdsal-binding-spec-util/src/test/java/org/opendaylight/mdsal/binding/spec/reflect/GroupingFoo.java [new file with mode: 0644]
binding/yang-binding/src/main/java/org/opendaylight/mdsal/binding/spec/util/DataObjectReadingUtil.java [moved from binding/mdsal-binding-spec-util/src/main/java/org/opendaylight/mdsal/binding/spec/util/DataObjectReadingUtil.java with 100% similarity]
binding/yang-binding/src/main/java/org/opendaylight/mdsal/binding/spec/util/DataObjectUtils.java [new file with mode: 0644]
binding/yang-binding/src/test/java/org/opendaylight/mdsal/binding/spec/util/DataObjectReadingUtilTest.java [moved from binding/mdsal-binding-spec-util/src/test/java/org/opendaylight/mdsal/binding/spec/util/DataObjectReadingUtilTest.java with 100% similarity]
binding/yang-binding/src/test/java/org/opendaylight/mdsal/binding/spec/util/DataObjectUtilsTest.java [new file with mode: 0644]
binding/yang-binding/src/test/java/org/opendaylight/mdsal/binding/spec/util/FooChild.java [moved from binding/mdsal-binding-spec-util/src/test/java/org/opendaylight/mdsal/binding/spec/util/FooChild.java with 100% similarity]
binding/yang-binding/src/test/java/org/opendaylight/mdsal/binding/spec/util/GroupingFoo.java [moved from binding/mdsal-binding-spec-util/src/test/java/org/opendaylight/mdsal/binding/spec/util/GroupingFoo.java with 100% similarity]
binding/yang-binding/src/test/java/org/opendaylight/mdsal/binding/spec/util/Node.java [moved from binding/mdsal-binding-spec-util/src/test/java/org/opendaylight/mdsal/binding/spec/util/Node.java with 100% similarity]
binding/yang-binding/src/test/java/org/opendaylight/mdsal/binding/spec/util/NodeKey.java [moved from binding/mdsal-binding-spec-util/src/test/java/org/opendaylight/mdsal/binding/spec/util/NodeKey.java with 100% similarity]
binding/yang-binding/src/test/java/org/opendaylight/mdsal/binding/spec/util/Nodes.java [moved from binding/mdsal-binding-spec-util/src/test/java/org/opendaylight/mdsal/binding/spec/util/Nodes.java with 100% similarity]

index 9f473a7758425b093eb7d5b289baa4613840f50d..bfeba408aab20f5fe966c791f67940dfa30aa0ad 100644 (file)
@@ -21,8 +21,6 @@ import java.lang.reflect.InvocationTargetException;
 import java.util.Collections;
 import java.util.List;
 import org.junit.Test;
-import org.opendaylight.mdsal.binding.spec.util.FooChild;
-import org.opendaylight.mdsal.binding.spec.util.GroupingFoo;
 import org.opendaylight.yangtools.yang.binding.Augmentation;
 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
 import org.opendaylight.yangtools.yang.binding.ChildOf;
diff --git a/binding/mdsal-binding-spec-util/src/test/java/org/opendaylight/mdsal/binding/spec/reflect/FooChild.java b/binding/mdsal-binding-spec-util/src/test/java/org/opendaylight/mdsal/binding/spec/reflect/FooChild.java
new file mode 100644 (file)
index 0000000..423fe82
--- /dev/null
@@ -0,0 +1,14 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.binding.spec.reflect;
+
+import org.opendaylight.yangtools.yang.binding.ChildOf;
+
+public interface FooChild extends ChildOf<GroupingFoo> {
+
+}
diff --git a/binding/mdsal-binding-spec-util/src/test/java/org/opendaylight/mdsal/binding/spec/reflect/GroupingFoo.java b/binding/mdsal-binding-spec-util/src/test/java/org/opendaylight/mdsal/binding/spec/reflect/GroupingFoo.java
new file mode 100644 (file)
index 0000000..b2915fa
--- /dev/null
@@ -0,0 +1,12 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.binding.spec.reflect;
+
+public interface GroupingFoo {
+
+}
diff --git a/binding/yang-binding/src/main/java/org/opendaylight/mdsal/binding/spec/util/DataObjectUtils.java b/binding/yang-binding/src/main/java/org/opendaylight/mdsal/binding/spec/util/DataObjectUtils.java
new file mode 100644 (file)
index 0000000..75edc69
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2018 Pantheon Technologies, s.r.o.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.binding.spec.util;
+
+import com.google.common.collect.ImmutableList;
+import java.util.List;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+
+/**
+ * Various utility methods for bridging generated interfaces and human-written code. Methods in this class should be
+ * statically imported into user code, so that the resulting code is expressive:
+ *
+ * <code>
+ * import static org.opendaylight.yangtools.yang.binding.DataObjectUtils.nullToEmpty;
+ *
+ * FooWithList foo;
+ * for (ListItem item : nulltoEmpty(foo.getList())) {
+ *     // ..
+ * }
+ * </code>
+ *
+ * @author Robert Varga
+ */
+@NonNullByDefault
+public final class DataObjectUtils {
+    private DataObjectUtils() {
+
+    }
+
+    /**
+     * Utility method for dealing with nullable Lists returned from Binding DTOs.
+     *
+     * @param input input list, may be null
+     * @return Input list or an empty list.
+     */
+    public static <T> List<T> nullToEmpty(final @Nullable List<T> input) {
+        return input != null ? input : ImmutableList.of();
+    }
+}
diff --git a/binding/yang-binding/src/test/java/org/opendaylight/mdsal/binding/spec/util/DataObjectUtilsTest.java b/binding/yang-binding/src/test/java/org/opendaylight/mdsal/binding/spec/util/DataObjectUtilsTest.java
new file mode 100644 (file)
index 0000000..b323115
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2018 Pantheon Technologies, s.r.o.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.binding.spec.util;
+import static org.junit.Assert.assertSame;
+import static org.opendaylight.mdsal.binding.spec.util.DataObjectUtils.nullToEmpty;
+
+import com.google.common.collect.ImmutableList;
+import java.util.Collections;
+import java.util.List;
+import org.junit.Test;
+
+public class DataObjectUtilsTest {
+    @Test
+    public void nullToEmptyNullTest() {
+        assertSame(ImmutableList.of(), nullToEmpty(null));
+    }
+
+    @Test
+    public void nullToEmptyNonNullTest() {
+        final List<Object> list = Collections.singletonList(null);
+        assertSame(list, nullToEmpty(list));
+    }
+}