[java-metadata-aggregator] branch master updated: MDA-251 - Item#unwrap should be @Nonnull

Ian Young ian at iay.org.uk
Thu Jul 9 17:14:02 UTC 2020


This is an automated email from the git hooks/post-receive script.

iay pushed a commit to branch master
in repository java-metadata-aggregator.

View the commit online:
http://git.shibboleth.net/view/?p=java-metadata-aggregator.git;a=commit;h=832a60e8ca626c0513897757832a6dc2cdb7fd59

The following commit(s) were added to refs/heads/master by this push:
       new  832a60e   MDA-251 - Item#unwrap should be @Nonnull
832a60e is described below

commit 832a60e8ca626c0513897757832a6dc2cdb7fd59
Author: Ian Young <ian at iay.org.uk>
AuthorDate: Thu Jul 9 18:13:58 2020 +0100

    MDA-251 - Item#unwrap should be @Nonnull
    
    https://issues.shibboleth.net/jira/browse/MDA-251
---
 .../java/net/shibboleth/metadata/AbstractItem.java | 33 ++++++------
 .../main/java/net/shibboleth/metadata/Item.java    |  5 +-
 .../shibboleth/metadata/dom/DOMElementItem.java    | 58 +++++++++++++++++-----
 .../java/net/shibboleth/metadata/MockItem.java     |  2 +-
 .../java/net/shibboleth/metadata/MockItemTest.java | 15 ++++++
 .../metadata/dom/DOMElementItemTest.java           | 21 ++++++++
 6 files changed, 100 insertions(+), 34 deletions(-)

diff --git a/aggregator-pipeline/src/main/java/net/shibboleth/metadata/AbstractItem.java b/aggregator-pipeline/src/main/java/net/shibboleth/metadata/AbstractItem.java
index 55329f9..d58ce7e 100644
--- a/aggregator-pipeline/src/main/java/net/shibboleth/metadata/AbstractItem.java
+++ b/aggregator-pipeline/src/main/java/net/shibboleth/metadata/AbstractItem.java
@@ -18,11 +18,11 @@
 package net.shibboleth.metadata;
 
 import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
 import javax.annotation.concurrent.NotThreadSafe;
 
 import net.shibboleth.utilities.java.support.annotation.constraint.NonnullElements;
 import net.shibboleth.utilities.java.support.collection.ClassToInstanceMultiMap;
+import net.shibboleth.utilities.java.support.logic.Constraint;
 
 /**
  * Base implementation of an {@link Item}.
@@ -33,32 +33,29 @@ import net.shibboleth.utilities.java.support.collection.ClassToInstanceMultiMap;
 public abstract class AbstractItem<T> implements Item<T> {
 
     /** The actual data held by the item. */
-    @Nullable private T data;
+    @Nonnull private final T data;
 
-    /** Additional processing information associated with this Item. */
-    private final ClassToInstanceMultiMap<ItemMetadata> metadata;
+    /** Additional processing information associated with this {@code Item}. */
+    @Nonnull @NonnullElements private final ClassToInstanceMultiMap<ItemMetadata> metadata;
 
-    /** Constructor. */
-    protected AbstractItem() {
+    /**
+     * Constructor.
+     *
+     * @param newData data to wrap in the {@code Item}
+     */
+    protected AbstractItem(@Nonnull final T newData) {
+        Constraint.isNotNull(newData, "data to wrap can not be null");
         metadata = new ClassToInstanceMultiMap<>(true);
+        data = newData;
     }
 
-    /** {@inheritDoc} */
-    @Override @Nullable public T unwrap() {
+    @Override
+    @Nonnull public final T unwrap() {
         return data;
     }
 
-    /**
-     * Sets the data wrapped by this Item.
-     * 
-     * @param newData the data
-     */
-    protected void setData(@Nullable final T newData) {
-        data = newData;
-    }
-
     @Override
-    @Nonnull @NonnullElements public ClassToInstanceMultiMap<ItemMetadata> getItemMetadata() {
+    @Nonnull @NonnullElements public final ClassToInstanceMultiMap<ItemMetadata> getItemMetadata() {
         return metadata;
     }
 }
diff --git a/aggregator-pipeline/src/main/java/net/shibboleth/metadata/Item.java b/aggregator-pipeline/src/main/java/net/shibboleth/metadata/Item.java
index f57db32..2e03adf 100644
--- a/aggregator-pipeline/src/main/java/net/shibboleth/metadata/Item.java
+++ b/aggregator-pipeline/src/main/java/net/shibboleth/metadata/Item.java
@@ -18,7 +18,6 @@
 package net.shibboleth.metadata;
 
 import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
 
 import net.shibboleth.utilities.java.support.annotation.constraint.NonnullElements;
 import net.shibboleth.utilities.java.support.collection.ClassToInstanceMultiMap;
@@ -75,7 +74,7 @@ public interface Item<T> {
      * 
      * @return the wrapped item data
      */
-    @Nullable T unwrap();
+    @Nonnull T unwrap();
 
     /**
      * Gets all of the metadata attached to this Item.
@@ -88,7 +87,7 @@ public interface Item<T> {
      * Performs a copy of this Item. All member fields, except {@link ItemMetadata}, should be deep cloned.
      * {@link ItemMetadata} objects must be shared between the clone and the original.
      * 
-     * @return the clone of this element
+     * @return the clone of this {@code Item}
      */
     @Nonnull Item<T> copy();
 }
diff --git a/aggregator-pipeline/src/main/java/net/shibboleth/metadata/dom/DOMElementItem.java b/aggregator-pipeline/src/main/java/net/shibboleth/metadata/dom/DOMElementItem.java
index 623b6c1..c71a7d4 100644
--- a/aggregator-pipeline/src/main/java/net/shibboleth/metadata/dom/DOMElementItem.java
+++ b/aggregator-pipeline/src/main/java/net/shibboleth/metadata/dom/DOMElementItem.java
@@ -39,32 +39,66 @@ import net.shibboleth.utilities.java.support.xml.ElementSupport;
 public class DOMElementItem extends AbstractItem<Element> {
 
     /**
-     * Constructor. The document element of the given document becomes the {@link Element} value for this item.
+     * Constructor.
+     * 
+     * <p>
+     * The document element of the given document becomes the {@link Element} value for this item.
+     * </p>
      * 
      * @param document document whose document element becomes the value for this Item; may not be null and must have a
      *            document element
      */
     public DOMElementItem(@Nonnull final Document document) {
-        super();
+        super(processDocument(document));
+    }
 
-        Constraint.isNotNull(document, "DOM Document can not be null");
+    /**
+     * Constructor.
+     * 
+     * <p>
+     * A new {@link Document} is created and the given {@link Element} is deep-imported in to the new
+     * document via {@link Document#importNode(org.w3c.dom.Node, boolean)}. The resulting {@link Element} is set as
+     * the new document's root.
+     * </p>
+     * 
+     * @param element element that is copied to become the value of this Item
+     */
+    public DOMElementItem(@Nonnull final Element element) {
+        super(processElement(element));
+    }
 
+    /**
+     * Process a {@link Document} for wrapping by an {@code Item}.
+     *
+     * <p>
+     * The {@code Item} simply wraps the {@link Document}'s document element.
+     * </p>
+     *
+     * @param document {@link Document} to wrap
+     * @return processed element
+     */
+    @Nonnull private static Element processDocument(@Nonnull final Document document) {
+        Constraint.isNotNull(document, "DOM Document can not be null");
+        
         final Element docElement = document.getDocumentElement();
         Constraint.isNotNull(docElement, "DOM Document Element may not be null");
 
-        setData(document.getDocumentElement());
+        return docElement;
     }
 
     /**
-     * Constructor. A new {@link Document} is created and the given {@link Element} is deep-imported in to the new
-     * document via {@link Document#importNode(org.w3c.dom.Node, boolean)}, and the resultant {@link Element} is set as
+     * Process an {@link Element} for wrapping by an {@code Item}.
+     *
+     * <p>
+     * A new {@link Document} is created and the given {@link Element} is deep-imported in to the new
+     * document via {@link Document#importNode(org.w3c.dom.Node, boolean)}. The resulting {@link Element} is set as
      * the new document's root.
-     * 
-     * @param element element that is copied to become the value of this Item
+     * </p>
+     *
+     * @param element {@link Element} to process
+     * @return processed element
      */
-    public DOMElementItem(@Nonnull final Element element) {
-        super();
-
+    @Nonnull private static Element processElement(@Nonnull final Element element) {
         Constraint.isNotNull(element, "DOM Document Element may not be null");
 
         final DOMImplementation domImpl = element.getOwnerDocument().getImplementation();
@@ -72,7 +106,7 @@ public class DOMElementItem extends AbstractItem<Element> {
         final Element newDocumentRoot = (Element) newDocument.importNode(element, true);
         ElementSupport.setDocumentElement(newDocument, newDocumentRoot);
 
-        setData(newDocumentRoot);
+        return newDocumentRoot;
     }
 
     @Override
diff --git a/aggregator-pipeline/src/test/java/net/shibboleth/metadata/MockItem.java b/aggregator-pipeline/src/test/java/net/shibboleth/metadata/MockItem.java
index d59bc21..cc58622 100644
--- a/aggregator-pipeline/src/test/java/net/shibboleth/metadata/MockItem.java
+++ b/aggregator-pipeline/src/test/java/net/shibboleth/metadata/MockItem.java
@@ -31,7 +31,7 @@ public class MockItem extends AbstractItem<String> {
      * @param str data held by this item
      */
     public MockItem(String str) {
-        setData(str);
+        super(str);
     }
 
     @Override
diff --git a/aggregator-pipeline/src/test/java/net/shibboleth/metadata/MockItemTest.java b/aggregator-pipeline/src/test/java/net/shibboleth/metadata/MockItemTest.java
new file mode 100644
index 0000000..5090322
--- /dev/null
+++ b/aggregator-pipeline/src/test/java/net/shibboleth/metadata/MockItemTest.java
@@ -0,0 +1,15 @@
+
+package net.shibboleth.metadata;
+
+import org.testng.annotations.Test;
+
+import net.shibboleth.utilities.java.support.logic.ConstraintViolationException;
+
+public class MockItemTest {
+
+    @Test(expectedExceptions=ConstraintViolationException.class)
+    public void testNull() {
+        new MockItem(null);
+    }
+
+}
diff --git a/aggregator-pipeline/src/test/java/net/shibboleth/metadata/dom/DOMElementItemTest.java b/aggregator-pipeline/src/test/java/net/shibboleth/metadata/dom/DOMElementItemTest.java
new file mode 100644
index 0000000..6af56bb
--- /dev/null
+++ b/aggregator-pipeline/src/test/java/net/shibboleth/metadata/dom/DOMElementItemTest.java
@@ -0,0 +1,21 @@
+
+package net.shibboleth.metadata.dom;
+
+import org.testng.annotations.Test;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import net.shibboleth.utilities.java.support.logic.ConstraintViolationException;
+
+public class DOMElementItemTest {
+
+    @Test(expectedExceptions=ConstraintViolationException.class)
+    public void testNullDocument() {
+        new DOMElementItem((Document)null);
+    }
+
+    @Test(expectedExceptions=ConstraintViolationException.class)
+    public void testNullElement() {
+        new DOMElementItem((Element)null);
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list