From 8b9fa1f4f9d9df7212e6e440dc42dd10f32e0596 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 26 Aug 2015 18:43:57 +0200 Subject: [PATCH] Return failed state if no comic is found --- .../rhynodge/filters/ComicSiteFilter.java | 33 ++++++++++++---------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/main/java/net/pterodactylus/rhynodge/filters/ComicSiteFilter.java b/src/main/java/net/pterodactylus/rhynodge/filters/ComicSiteFilter.java index 0b52045..1d9e6f7 100644 --- a/src/main/java/net/pterodactylus/rhynodge/filters/ComicSiteFilter.java +++ b/src/main/java/net/pterodactylus/rhynodge/filters/ComicSiteFilter.java @@ -28,6 +28,7 @@ import net.pterodactylus.rhynodge.State; import net.pterodactylus.rhynodge.states.ComicState; import net.pterodactylus.rhynodge.states.ComicState.Comic; import net.pterodactylus.rhynodge.states.ComicState.Strip; +import net.pterodactylus.rhynodge.states.FailedState; import net.pterodactylus.rhynodge.states.HtmlState; import com.google.common.base.Optional; @@ -47,7 +48,6 @@ public abstract class ComicSiteFilter implements Filter { /* initialize states: */ HtmlState htmlState = (HtmlState) state; - ComicState comicState = new ComicState(); /* extract comics. */ Optional title = extractTitle(htmlState.document()); @@ -55,22 +55,25 @@ public abstract class ComicSiteFilter implements Filter { List imageComments = extractImageComments(htmlState.document()); /* store comic, if found, into state. */ - if (title.isPresent() && !imageUrls.isEmpty()) { - Comic comic = new Comic(title.get()); - int imageCounter = 0; - for (String imageUrl : imageUrls) { - String imageComment = (imageCounter < imageComments.size()) ? imageComments.get(imageCounter) : ""; - try { - URI stripUri = new URI(htmlState.uri()).resolve(imageUrl); - Strip strip = new Strip(stripUri.toString(), imageComment); - imageCounter++; - comic.add(strip); - } catch (URISyntaxException use1) { - throw new IllegalStateException(String.format("Could not resolve image URL “%s” against base URL “%s”.", imageUrl, htmlState.uri()), use1); - } + if (!title.isPresent() || imageUrls.isEmpty()) { + return new FailedState(); + } + + ComicState comicState = new ComicState(); + Comic comic = new Comic(title.get()); + int imageCounter = 0; + for (String imageUrl : imageUrls) { + String imageComment = (imageCounter < imageComments.size()) ? imageComments.get(imageCounter) : ""; + try { + URI stripUri = new URI(htmlState.uri()).resolve(imageUrl); + Strip strip = new Strip(stripUri.toString(), imageComment); + imageCounter++; + comic.add(strip); + } catch (URISyntaxException use1) { + throw new IllegalStateException(String.format("Could not resolve image URL “%s” against base URL “%s”.", imageUrl, htmlState.uri()), use1); } - comicState.add(comic); } + comicState.add(comic); return comicState; } -- 2.7.4