From db3764229116f5c1355f8c35a4e1b430a6ce838f Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 5 Oct 2016 20:10:21 +0200 Subject: [PATCH] Add @Nonnull annotations to PartContainer --- .../net/pterodactylus/sone/text/PartContainer.java | 53 ++++------------------ .../pterodactylus/sone/text/PartContainerTest.java | 5 ++ 2 files changed, 13 insertions(+), 45 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/text/PartContainer.java b/src/main/java/net/pterodactylus/sone/text/PartContainer.java index d1c89b3..3444d19 100644 --- a/src/main/java/net/pterodactylus/sone/text/PartContainer.java +++ b/src/main/java/net/pterodactylus/sone/text/PartContainer.java @@ -23,6 +23,9 @@ import java.util.Deque; import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; +import java.util.Objects; + +import javax.annotation.Nonnull; /** * Part implementation that can contain an arbitrary amount of other parts. @@ -33,61 +36,27 @@ import java.util.NoSuchElementException; */ public class PartContainer implements Part, Iterable { - /** The parts to render. */ private final List parts = new ArrayList(); - // - // ACCESSORS - // - - /** - * Adds a part to render. - * - * @param part - * The part to add - */ - public void add(Part part) { - parts.add(part); + public void add(@Nonnull Part part) { + parts.add(Objects.requireNonNull(part)); } - /** - * Returns the part at the given index. - * - * @param index - * The index of the part - * @return The part - */ + @Nonnull public Part getPart(int index) { return parts.get(index); } - /** - * Removes the part at the given index. - * - * @param index - * The index of the part to remove - */ public void removePart(int index) { parts.remove(index); } - /** - * Returns the number of parts. - * - * @return The number of parts - */ public int size() { return parts.size(); } - // - // PART METHODS - // - - /** - * {@inheritDoc} - */ @Override + @Nonnull public String getText() { StringBuilder partText = new StringBuilder(); for (Part part : parts) { @@ -96,14 +65,8 @@ public class PartContainer implements Part, Iterable { return partText.toString(); } - // - // ITERABLE METHODS - // - - /** - * {@inheritDoc} - */ @Override + @Nonnull @SuppressWarnings("synthetic-access") public Iterator iterator() { return new Iterator() { diff --git a/src/test/java/net/pterodactylus/sone/text/PartContainerTest.java b/src/test/java/net/pterodactylus/sone/text/PartContainerTest.java index dfbaa21..036b2dc 100644 --- a/src/test/java/net/pterodactylus/sone/text/PartContainerTest.java +++ b/src/test/java/net/pterodactylus/sone/text/PartContainerTest.java @@ -24,6 +24,11 @@ public class PartContainerTest { assertThat(container.size(), is(0)); } + @Test(expected = NullPointerException.class) + public void canNotAddNullPart() { + container.add(null); + } + @Test public void containerWithSinglePartHasSizeOne() { container.add(mock(Part.class)); -- 2.7.4