From 243762ba32edac434de47b6d101f73e482758494 Mon Sep 17 00:00:00 2001 From: REDNBLACK Date: Tue, 3 May 2016 17:01:17 +0300 Subject: [PATCH] Refactoring using lombok --- core/build.gradle | 9 +++ .../main/java/org/f0w/k2i/core/Client.java | 13 +-- .../k2i/core/comparator/MovieComparator.java | 3 +- .../title/ContainsTitleComparator.java | 3 +- .../title/EndsWithTitleComparator.java | 3 +- .../title/EqualsTitleComparator.java | 3 +- .../title/SmartTitleComparator.java | 5 +- .../title/StartsWithTitleComparator.java | 3 +- .../year/DeviationYearComparator.java | 7 +- .../comparator/year/EqualsYearComparator.java | 3 +- .../core/exchange/AbstractExchangeable.java | 12 +-- .../f0w/k2i/core/exchange/Exchangeable.java | 5 +- .../core/exchange/MovieAuthStringFetcher.java | 10 ++- .../k2i/core/exchange/MovieRatingChanger.java | 38 +++------ .../core/exchange/MovieWatchlistAssigner.java | 14 ++-- .../exchange/finder/BasicMovieFinder.java | 26 +++--- .../exchange/finder/MixedMovieFinder.java | 21 ++--- .../finder/strategy/ExchangeStrategy.java | 7 +- .../finder/strategy/HTMLExchangeStrategy.java | 17 ++-- .../finder/strategy/JSONExchangeStrategy.java | 17 ++-- .../finder/strategy/XMLExchangeStrategy.java | 17 ++-- .../core/handler/AddToWatchlistHandler.java | 2 +- .../core/handler/ConnectionCheckHandler.java | 7 +- .../f0w/k2i/core/handler/MovieHandler.java | 50 +++++------- .../f0w/k2i/core/handler/ParseIDHandler.java | 5 +- .../k2i/core/handler/SetRatingHandler.java | 28 ++++--- .../f0w/k2i/core/model/entity/BaseEntity.java | 28 +------ .../k2i/core/model/entity/ImportProgress.java | 79 ++---------------- .../k2i/core/model/entity/KinopoiskFile.java | 51 ++---------- .../org/f0w/k2i/core/model/entity/Movie.java | 81 ++----------------- .../model/service/ImportProgressService.java | 3 +- .../java/org/f0w/k2i/core/util/HttpUtils.java | 6 +- .../org/f0w/k2i/core/util/MovieUtils.java | 46 +++++------ .../src/test/java/org/f0w/k2i/TestHelper.java | 2 + .../MovieComparatorFactoryTest.java | 31 +++---- .../core/event/ImportFinishedEventTest.java | 12 +-- .../finder/MovieFinderFactoryTest.java | 25 +++--- .../MockImportProgressRepositoryImpl.java | 3 +- .../mock/MockKinopoiskFileRepositoryImpl.java | 3 +- .../org/f0w/k2i/core/util/IOUtilsTest.java | 5 +- 40 files changed, 252 insertions(+), 451 deletions(-) diff --git a/core/build.gradle b/core/build.gradle index 9cfa57e..f3f67dc 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -1,5 +1,14 @@ +plugins { + id 'io.franzbecker.gradle-lombok' version '1.6' +} + version = "0.7.7" +lombok { + version = "1.16.8" + sha256 = "fe32b29b7b33eb2b19866cbc5345c59c9984a6036cc396856a04c0d09f99ea68" +} + ext { guiceVersion = "4.0" hibernateVersion = "5.1.0.Final" diff --git a/core/src/main/java/org/f0w/k2i/core/Client.java b/core/src/main/java/org/f0w/k2i/core/Client.java index 04e4657..a3ad4ca 100644 --- a/core/src/main/java/org/f0w/k2i/core/Client.java +++ b/core/src/main/java/org/f0w/k2i/core/Client.java @@ -6,6 +6,7 @@ import com.google.inject.Stage; import com.google.inject.persist.PersistService; import com.typesafe.config.Config; +import lombok.NonNull; import org.f0w.k2i.core.event.ImportFinishedEvent; import org.f0w.k2i.core.event.ImportProgressAdvancedEvent; import org.f0w.k2i.core.event.ImportStartedEvent; @@ -21,7 +22,7 @@ import java.util.Collections; import java.util.List; -import static java.util.Objects.requireNonNull; +import static org.f0w.k2i.core.util.IOUtils.checkFile; /** * Main facade implemented as {@link Runnable} for working with core module. @@ -33,15 +34,15 @@ public final class Client implements Runnable { private final ImportProgressService importProgressService; private final PersistService persistService; - public Client(Path filePath, Config config) { + public Client(@NonNull Path filePath, @NonNull Config config) { this(filePath, config, false); } - public Client(Path filePath, Config config, boolean cleanRun) { + public Client(@NonNull Path filePath, @NonNull Config config, boolean cleanRun) { this(filePath, config, cleanRun, Collections.emptyList()); } - public Client(Path filePath, Config config, boolean cleanRun, List listeners) { + public Client(@NonNull Path filePath, @NonNull Config config, boolean cleanRun, @NonNull List listeners) { Injector injector = Guice.createInjector( Stage.PRODUCTION, new ConfigurationProvider(config), @@ -55,10 +56,10 @@ public Client(Path filePath, Config config, boolean cleanRun, List listeners) handlerType = injector.getInstance(MovieHandler.Type.class); handlerChain = injector.getInstance(MovieHandler.class); importProgressService = injector.getInstance(ImportProgressService.class) - .initialize(filePath, cleanRun); + .initialize(checkFile(filePath), cleanRun); eventBus = new EventBus(); - requireNonNull(listeners).forEach(eventBus::register); + listeners.forEach(eventBus::register); } @Override diff --git a/core/src/main/java/org/f0w/k2i/core/comparator/MovieComparator.java b/core/src/main/java/org/f0w/k2i/core/comparator/MovieComparator.java index 45d6525..cae925d 100644 --- a/core/src/main/java/org/f0w/k2i/core/comparator/MovieComparator.java +++ b/core/src/main/java/org/f0w/k2i/core/comparator/MovieComparator.java @@ -1,5 +1,6 @@ package org.f0w.k2i.core.comparator; +import lombok.NonNull; import org.f0w.k2i.core.model.entity.Movie; /** @@ -14,7 +15,7 @@ public interface MovieComparator { * @param movie2 Second movie * @return Are two movie objects equals to each other */ - boolean areEqual(Movie movie1, Movie movie2); + boolean areEqual(@NonNull Movie movie1, @NonNull Movie movie2); /** * Available MovieComparator types diff --git a/core/src/main/java/org/f0w/k2i/core/comparator/title/ContainsTitleComparator.java b/core/src/main/java/org/f0w/k2i/core/comparator/title/ContainsTitleComparator.java index 2fa3b58..84b8508 100644 --- a/core/src/main/java/org/f0w/k2i/core/comparator/title/ContainsTitleComparator.java +++ b/core/src/main/java/org/f0w/k2i/core/comparator/title/ContainsTitleComparator.java @@ -1,5 +1,6 @@ package org.f0w.k2i.core.comparator.title; +import lombok.NonNull; import org.f0w.k2i.core.comparator.AbstractMovieComparator; import org.f0w.k2i.core.model.entity.Movie; @@ -13,7 +14,7 @@ public final class ContainsTitleComparator extends AbstractMovieComparator { * {@inheritDoc} */ @Override - public boolean areEqual(Movie movie1, Movie movie2) { + public boolean areEqual(@NonNull Movie movie1, @NonNull Movie movie2) { String title1 = movie1.getTitle(); String title2 = movie2.getTitle(); diff --git a/core/src/main/java/org/f0w/k2i/core/comparator/title/EndsWithTitleComparator.java b/core/src/main/java/org/f0w/k2i/core/comparator/title/EndsWithTitleComparator.java index e856b6d..710df94 100644 --- a/core/src/main/java/org/f0w/k2i/core/comparator/title/EndsWithTitleComparator.java +++ b/core/src/main/java/org/f0w/k2i/core/comparator/title/EndsWithTitleComparator.java @@ -1,5 +1,6 @@ package org.f0w.k2i.core.comparator.title; +import lombok.NonNull; import org.f0w.k2i.core.comparator.AbstractMovieComparator; import org.f0w.k2i.core.model.entity.Movie; @@ -11,7 +12,7 @@ public final class EndsWithTitleComparator extends AbstractMovieComparator { * {@inheritDoc} */ @Override - public boolean areEqual(Movie movie1, Movie movie2) { + public boolean areEqual(@NonNull Movie movie1, @NonNull Movie movie2) { String title1 = movie1.getTitle().toLowerCase(); String title2 = movie2.getTitle().toLowerCase(); diff --git a/core/src/main/java/org/f0w/k2i/core/comparator/title/EqualsTitleComparator.java b/core/src/main/java/org/f0w/k2i/core/comparator/title/EqualsTitleComparator.java index 007c989..1733d56 100644 --- a/core/src/main/java/org/f0w/k2i/core/comparator/title/EqualsTitleComparator.java +++ b/core/src/main/java/org/f0w/k2i/core/comparator/title/EqualsTitleComparator.java @@ -1,5 +1,6 @@ package org.f0w.k2i.core.comparator.title; +import lombok.NonNull; import org.f0w.k2i.core.comparator.AbstractMovieComparator; import org.f0w.k2i.core.model.entity.Movie; @@ -11,7 +12,7 @@ public final class EqualsTitleComparator extends AbstractMovieComparator { * {@inheritDoc} */ @Override - public boolean areEqual(Movie movie1, Movie movie2) { + public boolean areEqual(@NonNull Movie movie1, @NonNull Movie movie2) { String title1 = movie1.getTitle(); String title2 = movie2.getTitle(); diff --git a/core/src/main/java/org/f0w/k2i/core/comparator/title/SmartTitleComparator.java b/core/src/main/java/org/f0w/k2i/core/comparator/title/SmartTitleComparator.java index f1480e4..b73935c 100644 --- a/core/src/main/java/org/f0w/k2i/core/comparator/title/SmartTitleComparator.java +++ b/core/src/main/java/org/f0w/k2i/core/comparator/title/SmartTitleComparator.java @@ -1,6 +1,7 @@ package org.f0w.k2i.core.comparator.title; import com.google.common.collect.ImmutableList; +import lombok.NonNull; import org.f0w.k2i.core.comparator.AbstractMovieComparator; import org.f0w.k2i.core.model.entity.Movie; import org.f0w.k2i.core.util.string.NumericToWord; @@ -113,7 +114,7 @@ public final class SmartTitleComparator extends AbstractMovieComparator { * {@inheritDoc} */ @Override - public boolean areEqual(Movie movie1, Movie movie2) { + public boolean areEqual(@NonNull Movie movie1, @NonNull Movie movie2) { for (StringModifier m1 : modifiers) { for (StringModifier m2 : modifiers) { String m1Title = m1.modify(movie1.getTitle()); @@ -139,6 +140,6 @@ public boolean areEqual(Movie movie1, Movie movie2) { @FunctionalInterface private interface StringModifier { - String modify(String string); + String modify(@NonNull String string); } } \ No newline at end of file diff --git a/core/src/main/java/org/f0w/k2i/core/comparator/title/StartsWithTitleComparator.java b/core/src/main/java/org/f0w/k2i/core/comparator/title/StartsWithTitleComparator.java index 788d0ce..17134fc 100644 --- a/core/src/main/java/org/f0w/k2i/core/comparator/title/StartsWithTitleComparator.java +++ b/core/src/main/java/org/f0w/k2i/core/comparator/title/StartsWithTitleComparator.java @@ -1,5 +1,6 @@ package org.f0w.k2i.core.comparator.title; +import lombok.NonNull; import org.f0w.k2i.core.comparator.AbstractMovieComparator; import org.f0w.k2i.core.model.entity.Movie; @@ -11,7 +12,7 @@ public final class StartsWithTitleComparator extends AbstractMovieComparator { * {@inheritDoc} */ @Override - public boolean areEqual(Movie movie1, Movie movie2) { + public boolean areEqual(@NonNull Movie movie1, @NonNull Movie movie2) { String title1 = movie1.getTitle().toLowerCase(); String title2 = movie2.getTitle().toLowerCase(); diff --git a/core/src/main/java/org/f0w/k2i/core/comparator/year/DeviationYearComparator.java b/core/src/main/java/org/f0w/k2i/core/comparator/year/DeviationYearComparator.java index e5b459f..0ec3a31 100644 --- a/core/src/main/java/org/f0w/k2i/core/comparator/year/DeviationYearComparator.java +++ b/core/src/main/java/org/f0w/k2i/core/comparator/year/DeviationYearComparator.java @@ -1,8 +1,8 @@ package org.f0w.k2i.core.comparator.year; import com.google.common.collect.Range; -import com.google.inject.Inject; import com.typesafe.config.Config; +import lombok.NonNull; import org.f0w.k2i.core.comparator.AbstractMovieComparator; import org.f0w.k2i.core.model.entity.Movie; @@ -12,8 +12,7 @@ public final class DeviationYearComparator extends AbstractMovieComparator { private final Config config; - @Inject - public DeviationYearComparator(Config config) { + public DeviationYearComparator(@NonNull Config config) { this.config = config; } @@ -21,7 +20,7 @@ public DeviationYearComparator(Config config) { * {@inheritDoc} */ @Override - public boolean areEqual(Movie movie1, Movie movie2) { + public boolean areEqual(@NonNull Movie movie1, @NonNull Movie movie2) { int yearDeviation = config.getInt("year_deviation"); int movie2Year = movie2.getYear(); diff --git a/core/src/main/java/org/f0w/k2i/core/comparator/year/EqualsYearComparator.java b/core/src/main/java/org/f0w/k2i/core/comparator/year/EqualsYearComparator.java index e537a03..6d2290a 100644 --- a/core/src/main/java/org/f0w/k2i/core/comparator/year/EqualsYearComparator.java +++ b/core/src/main/java/org/f0w/k2i/core/comparator/year/EqualsYearComparator.java @@ -1,5 +1,6 @@ package org.f0w.k2i.core.comparator.year; +import lombok.NonNull; import org.f0w.k2i.core.comparator.AbstractMovieComparator; import org.f0w.k2i.core.model.entity.Movie; @@ -11,7 +12,7 @@ public final class EqualsYearComparator extends AbstractMovieComparator { * {@inheritDoc} */ @Override - public boolean areEqual(Movie movie1, Movie movie2) { + public boolean areEqual(@NonNull Movie movie1, @NonNull Movie movie2) { boolean result = movie1.getYear() == movie2.getYear(); LOG.debug( diff --git a/core/src/main/java/org/f0w/k2i/core/exchange/AbstractExchangeable.java b/core/src/main/java/org/f0w/k2i/core/exchange/AbstractExchangeable.java index 95f6fed..4cc6b4a 100644 --- a/core/src/main/java/org/f0w/k2i/core/exchange/AbstractExchangeable.java +++ b/core/src/main/java/org/f0w/k2i/core/exchange/AbstractExchangeable.java @@ -1,21 +1,17 @@ package org.f0w.k2i.core.exchange; +import lombok.NonNull; +import lombok.Setter; import org.jsoup.Connection; import java.util.Optional; public abstract class AbstractExchangeable implements Exchangeable { + @Setter + @NonNull protected Connection.Response response; - /** - * {@inheritDoc} - */ - @Override - public void setResponse(Connection.Response response) { - this.response = response; - } - /** * {@inheritDoc} */ diff --git a/core/src/main/java/org/f0w/k2i/core/exchange/Exchangeable.java b/core/src/main/java/org/f0w/k2i/core/exchange/Exchangeable.java index f9d322a..ff86a29 100644 --- a/core/src/main/java/org/f0w/k2i/core/exchange/Exchangeable.java +++ b/core/src/main/java/org/f0w/k2i/core/exchange/Exchangeable.java @@ -1,5 +1,6 @@ package org.f0w.k2i.core.exchange; +import lombok.NonNull; import org.jsoup.Connection; import java.io.IOException; @@ -12,14 +13,14 @@ * @param Output */ public interface Exchangeable { - void sendRequest(IN param) throws IOException; + void sendRequest(@NonNull IN param) throws IOException; /** * Sets the response * * @param response Response to set */ - void setResponse(Connection.Response response); + void setResponse(@NonNull Connection.Response response); /** * @return Raw response from server diff --git a/core/src/main/java/org/f0w/k2i/core/exchange/MovieAuthStringFetcher.java b/core/src/main/java/org/f0w/k2i/core/exchange/MovieAuthStringFetcher.java index 35c9faf..3f2302d 100644 --- a/core/src/main/java/org/f0w/k2i/core/exchange/MovieAuthStringFetcher.java +++ b/core/src/main/java/org/f0w/k2i/core/exchange/MovieAuthStringFetcher.java @@ -2,6 +2,8 @@ import com.google.inject.Inject; import com.typesafe.config.Config; +import lombok.NonNull; +import lombok.val; import org.f0w.k2i.core.model.entity.Movie; import org.jsoup.Connection; import org.jsoup.Jsoup; @@ -27,15 +29,15 @@ public MovieAuthStringFetcher(Config config) { * @throws IOException If an I/O error occurs */ @Override - public void sendRequest(Movie movie) throws IOException { - final String moviePageLink = "http://www.imdb.com/title/"; + public void sendRequest(@NonNull Movie movie) throws IOException { + val moviePageLink = "http://www.imdb.com/title/"; - Connection request = Jsoup.connect(moviePageLink + movie.getImdbId()) + Connection client = Jsoup.connect(moviePageLink + movie.getImdbId()) .userAgent(config.getString("user_agent")) .timeout(config.getInt("timeout")) .cookie("id", config.getString("auth")); - setResponse(request.execute()); + setResponse(client.execute()); } /** diff --git a/core/src/main/java/org/f0w/k2i/core/exchange/MovieRatingChanger.java b/core/src/main/java/org/f0w/k2i/core/exchange/MovieRatingChanger.java index 645f4c1..d89d94c 100644 --- a/core/src/main/java/org/f0w/k2i/core/exchange/MovieRatingChanger.java +++ b/core/src/main/java/org/f0w/k2i/core/exchange/MovieRatingChanger.java @@ -3,20 +3,25 @@ import com.google.common.collect.ImmutableMap; import com.google.inject.Inject; import com.typesafe.config.Config; +import lombok.Getter; +import lombok.NonNull; +import lombok.Setter; +import lombok.val; import org.f0w.k2i.core.model.entity.Movie; import org.jsoup.Connection; import org.jsoup.Jsoup; import java.io.IOException; -import java.util.Map; - -import static java.util.Objects.requireNonNull; /** * Changes Movie rating on IMDB. */ public final class MovieRatingChanger extends JSONPostExchangeable { private final Config config; + + @Getter + @Setter + @NonNull private String authString; @Inject @@ -24,24 +29,6 @@ public MovieRatingChanger(Config config) { this.config = config; } - /** - * Get the authString value. - * - * @return {@link this#authString} - */ - private String getAuthString() { - return authString; - } - - /** - * Set the {@link this#authString) to a not null value. - * - * @param authString - */ - public void setAuthString(final String authString) { - this.authString = requireNonNull(authString); - } - /** * Sends POST request and changes Movie rating, * using using {@link MovieAuthStringFetcher#getProcessedResponse()} authorization string @@ -52,9 +39,8 @@ public void setAuthString(final String authString) { */ @Override public void sendRequest(Movie movie) throws IOException { - final String movieRatingChangeLink = "http://www.imdb.com/ratings/_ajax/title"; - - Map postData = new ImmutableMap.Builder() + val movieRatingChangeLink = "http://www.imdb.com/ratings/_ajax/title"; + val postData = new ImmutableMap.Builder() .put("tconst", movie.getImdbId()) .put("rating", String.valueOf(movie.getRating())) .put("auth", getAuthString()) @@ -64,7 +50,7 @@ public void sendRequest(Movie movie) throws IOException { .put("subpageType", "main") .build(); - Connection request = Jsoup.connect(movieRatingChangeLink) + Connection client = Jsoup.connect(movieRatingChangeLink) .method(Connection.Method.POST) .userAgent(config.getString("user_agent")) .timeout(config.getInt("timeout")) @@ -72,6 +58,6 @@ public void sendRequest(Movie movie) throws IOException { .ignoreContentType(true) .data(postData); - setResponse(request.execute()); + setResponse(client.execute()); } } diff --git a/core/src/main/java/org/f0w/k2i/core/exchange/MovieWatchlistAssigner.java b/core/src/main/java/org/f0w/k2i/core/exchange/MovieWatchlistAssigner.java index ba93d6f..a48d946 100644 --- a/core/src/main/java/org/f0w/k2i/core/exchange/MovieWatchlistAssigner.java +++ b/core/src/main/java/org/f0w/k2i/core/exchange/MovieWatchlistAssigner.java @@ -3,12 +3,13 @@ import com.google.common.collect.ImmutableMap; import com.google.inject.Inject; import com.typesafe.config.Config; +import lombok.NonNull; +import lombok.val; import org.f0w.k2i.core.model.entity.Movie; import org.jsoup.Connection; import org.jsoup.Jsoup; import java.io.IOException; -import java.util.Map; /** * Adds Movie to IMDB list. @@ -28,16 +29,15 @@ public MovieWatchlistAssigner(Config config) { * @throws IOException If an I/O error occurs */ @Override - public void sendRequest(Movie movie) throws IOException { - final String movieAddToWatchlistLink = "http://www.imdb.com/list/_ajax/edit"; - - Map postData = new ImmutableMap.Builder() + public void sendRequest(@NonNull Movie movie) throws IOException { + val movieAddToWatchlistLink = "http://www.imdb.com/list/_ajax/edit"; + val postData = new ImmutableMap.Builder() .put("const", movie.getImdbId()) .put("list_id", config.getString("list")) .put("ref_tag", "title") .build(); - Connection request = Jsoup.connect(movieAddToWatchlistLink) + Connection client = Jsoup.connect(movieAddToWatchlistLink) .method(Connection.Method.POST) .userAgent(config.getString("user_agent")) .timeout(config.getInt("timeout")) @@ -45,6 +45,6 @@ public void sendRequest(Movie movie) throws IOException { .ignoreContentType(true) .data(postData); - setResponse(request.execute()); + setResponse(client.execute()); } } diff --git a/core/src/main/java/org/f0w/k2i/core/exchange/finder/BasicMovieFinder.java b/core/src/main/java/org/f0w/k2i/core/exchange/finder/BasicMovieFinder.java index b9c0459..ebc9ef6 100644 --- a/core/src/main/java/org/f0w/k2i/core/exchange/finder/BasicMovieFinder.java +++ b/core/src/main/java/org/f0w/k2i/core/exchange/finder/BasicMovieFinder.java @@ -1,26 +1,28 @@ package org.f0w.k2i.core.exchange.finder; import com.typesafe.config.Config; +import lombok.NonNull; +import lombok.extern.slf4j.Slf4j; +import lombok.val; import org.f0w.k2i.core.exchange.AbstractExchangeable; import org.f0w.k2i.core.exchange.finder.strategy.ExchangeStrategy; import org.f0w.k2i.core.model.entity.Movie; import org.jsoup.Connection; import org.jsoup.Jsoup; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.io.IOException; -import java.net.URL; import java.util.Deque; import java.util.LinkedList; +@Slf4j public final class BasicMovieFinder extends AbstractExchangeable> implements MovieFinder { - protected static final Logger LOG = LoggerFactory.getLogger(BasicMovieFinder.class); - + @NonNull private final Type type; + @NonNull private final ExchangeStrategy exchangeStrategy; + @NonNull private final Config config; public BasicMovieFinder(Type type, ExchangeStrategy exchangeStrategy, Config config) { @@ -36,20 +38,20 @@ public BasicMovieFinder(Type type, ExchangeStrategy exchangeStrategy, Config con * @throws IOException If an I/O error occurs */ @Override - public void sendRequest(Movie movie) throws IOException { - final URL movieSearchLink = exchangeStrategy.buildSearchURL(movie); + public void sendRequest(@NonNull Movie movie) throws IOException { + val movieSearchLink = exchangeStrategy.buildSearchURL(movie); - Connection request = Jsoup.connect(movieSearchLink.toString()) + Connection client = Jsoup.connect(movieSearchLink.toString()) .userAgent(config.getString("user_agent")) .timeout(config.getInt("timeout")); - LOG.debug( - "Sending request, to url: {}, with headers: {}", request.request().url(), request.request().headers() + log.debug( + "Sending request, to url: {}, with headers: {}", client.request().url(), client.request().headers() ); - setResponse(request.execute()); + setResponse(client.execute()); - LOG.debug("Got response, status code: {}, headers: {}", response.statusCode(), response.headers()); + log.debug("Got response, status code: {}, headers: {}", response.statusCode(), response.headers()); } /** diff --git a/core/src/main/java/org/f0w/k2i/core/exchange/finder/MixedMovieFinder.java b/core/src/main/java/org/f0w/k2i/core/exchange/finder/MixedMovieFinder.java index d10fa3f..4e867aa 100644 --- a/core/src/main/java/org/f0w/k2i/core/exchange/finder/MixedMovieFinder.java +++ b/core/src/main/java/org/f0w/k2i/core/exchange/finder/MixedMovieFinder.java @@ -2,10 +2,10 @@ import com.google.common.collect.ForwardingDeque; import com.google.common.collect.ImmutableList; +import lombok.NonNull; +import lombok.extern.slf4j.Slf4j; import org.f0w.k2i.core.model.entity.Movie; import org.jsoup.Connection; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.Deque; @@ -13,11 +13,14 @@ import java.util.List; import java.util.Optional; +@Slf4j public final class MixedMovieFinder implements MovieFinder { - private static final Logger LOG = LoggerFactory.getLogger(MixedMovieFinder.class); - + @NonNull private final Type type; + + @NonNull private final List originalFinders; + private Deque findersDeque; private Movie movie; @@ -27,7 +30,7 @@ public MixedMovieFinder(Type type, List movieFinders) { } @Override - public void sendRequest(Movie movie) throws IOException { + public void sendRequest(@NonNull Movie movie) throws IOException { this.movie = movie; findersDeque = new LinkedList<>(originalFinders); } @@ -82,19 +85,19 @@ public boolean isEmpty() { boolean isEmpty = super.isEmpty(); if (isEmpty && !findersDeque.isEmpty()) { - LOG.debug("Movies deque is empty, start loading..."); + log.debug("Movies deque is empty, start loading..."); try { MovieFinder finder = findersDeque.poll(); - LOG.debug("Loading using finder type: {}", finder.getType()); + log.debug("Loading using finder type: {}", finder.getType()); finder.sendRequest(MixedMovieFinder.this.movie); Deque movies = finder.getProcessedResponse(); super.addAll(movies); - LOG.debug("Successfully loaded: {}", movies); + log.debug("Successfully loaded: {}", movies); } catch (IOException ignore) { - LOG.debug("Loading error, trying next finder..."); + log.debug("Loading error, trying next finder..."); } return isEmpty(); diff --git a/core/src/main/java/org/f0w/k2i/core/exchange/finder/strategy/ExchangeStrategy.java b/core/src/main/java/org/f0w/k2i/core/exchange/finder/strategy/ExchangeStrategy.java index 6defb24..0eb51f6 100644 --- a/core/src/main/java/org/f0w/k2i/core/exchange/finder/strategy/ExchangeStrategy.java +++ b/core/src/main/java/org/f0w/k2i/core/exchange/finder/strategy/ExchangeStrategy.java @@ -1,5 +1,6 @@ package org.f0w.k2i.core.exchange.finder.strategy; +import lombok.NonNull; import org.f0w.k2i.core.model.entity.Movie; import java.net.URL; @@ -18,18 +19,16 @@ public interface ExchangeStrategy { * * @param movie Movie which fields to use * @return Movie search URL - * @throws NullPointerException If movie is null */ - URL buildSearchURL(final Movie movie); + URL buildSearchURL(@NonNull final Movie movie); /** * Parses data, implementation specific to each Format * * @param data Data * @return List of movies if data not an empty string, or {@link Collections#emptyList()} otherwise. - * @throws NullPointerException If data is null */ - List parseSearchResult(final String data); + List parseSearchResult(@NonNull final String data); /** * Generic interface for parsing single movie from collection. diff --git a/core/src/main/java/org/f0w/k2i/core/exchange/finder/strategy/HTMLExchangeStrategy.java b/core/src/main/java/org/f0w/k2i/core/exchange/finder/strategy/HTMLExchangeStrategy.java index 221ee4c..6eb0719 100644 --- a/core/src/main/java/org/f0w/k2i/core/exchange/finder/strategy/HTMLExchangeStrategy.java +++ b/core/src/main/java/org/f0w/k2i/core/exchange/finder/strategy/HTMLExchangeStrategy.java @@ -1,6 +1,8 @@ package org.f0w.k2i.core.exchange.finder.strategy; import com.google.common.collect.ImmutableMap; +import lombok.NonNull; +import lombok.val; import org.f0w.k2i.core.model.entity.Movie; import org.f0w.k2i.core.util.HttpUtils; import org.jsoup.Jsoup; @@ -9,24 +11,19 @@ import java.net.URL; import java.util.List; -import java.util.Map; import java.util.Optional; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; -import static java.util.Objects.requireNonNull; - public final class HTMLExchangeStrategy implements ExchangeStrategy { /** * {@inheritDoc} */ @Override - public URL buildSearchURL(final Movie movie) { - requireNonNull(movie); - - String searchLink = "http://www.imdb.com/find"; - Map queryParams = new ImmutableMap.Builder() + public URL buildSearchURL(@NonNull final Movie movie) { + val searchLink = "http://www.imdb.com/find"; + val queryParams = new ImmutableMap.Builder() .put("q", movie.getTitle()) .put("s", "tt") //.put("exact", "true") @@ -40,8 +37,8 @@ public URL buildSearchURL(final Movie movie) { * {@inheritDoc} */ @Override - public List parseSearchResult(final String data) { - Document document = Jsoup.parse(requireNonNull(data)); + public List parseSearchResult(@NonNull final String data) { + Document document = Jsoup.parse(data); HTMLMovieParser parser = new HTMLMovieParser(); return document.select("table.findList tr td.result_text") diff --git a/core/src/main/java/org/f0w/k2i/core/exchange/finder/strategy/JSONExchangeStrategy.java b/core/src/main/java/org/f0w/k2i/core/exchange/finder/strategy/JSONExchangeStrategy.java index bdec524..7f071b3 100644 --- a/core/src/main/java/org/f0w/k2i/core/exchange/finder/strategy/JSONExchangeStrategy.java +++ b/core/src/main/java/org/f0w/k2i/core/exchange/finder/strategy/JSONExchangeStrategy.java @@ -4,29 +4,26 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import lombok.NonNull; +import lombok.val; import org.f0w.k2i.core.model.entity.Movie; import org.f0w.k2i.core.util.HttpUtils; import java.net.URL; import java.util.Collections; import java.util.List; -import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; import java.util.stream.StreamSupport; -import static java.util.Objects.requireNonNull; - public final class JSONExchangeStrategy implements ExchangeStrategy { /** * {@inheritDoc} */ @Override - public URL buildSearchURL(final Movie movie) { - requireNonNull(movie); - - String searchLink = "http://www.imdb.com/xml/find"; - Map queryParams = new ImmutableMap.Builder() + public URL buildSearchURL(@NonNull final Movie movie) { + val searchLink = "http://www.imdb.com/xml/find"; + val queryParams = new ImmutableMap.Builder() .put("q", movie.getTitle()) .put("tt", "on") .put("nr", "1") @@ -40,8 +37,8 @@ public URL buildSearchURL(final Movie movie) { * {@inheritDoc} */ @Override - public List parseSearchResult(final String data) { - if ("".equals(requireNonNull(data))) { + public List parseSearchResult(@NonNull final String data) { + if ("".equals(data)) { return Collections.emptyList(); } diff --git a/core/src/main/java/org/f0w/k2i/core/exchange/finder/strategy/XMLExchangeStrategy.java b/core/src/main/java/org/f0w/k2i/core/exchange/finder/strategy/XMLExchangeStrategy.java index 98a5c6b..bbec22d 100644 --- a/core/src/main/java/org/f0w/k2i/core/exchange/finder/strategy/XMLExchangeStrategy.java +++ b/core/src/main/java/org/f0w/k2i/core/exchange/finder/strategy/XMLExchangeStrategy.java @@ -1,6 +1,8 @@ package org.f0w.k2i.core.exchange.finder.strategy; import com.google.common.collect.ImmutableMap; +import lombok.NonNull; +import lombok.val; import org.f0w.k2i.core.model.entity.Movie; import org.f0w.k2i.core.util.HttpUtils; import org.jsoup.Jsoup; @@ -9,22 +11,17 @@ import java.net.URL; import java.util.List; -import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; -import static java.util.Objects.requireNonNull; - public final class XMLExchangeStrategy implements ExchangeStrategy { /** * {@inheritDoc} */ @Override - public URL buildSearchURL(final Movie movie) { - requireNonNull(movie); - - String searchLink = "http://www.imdb.com/xml/find"; - Map queryParams = new ImmutableMap.Builder() + public URL buildSearchURL(@NonNull final Movie movie) { + val searchLink = "http://www.imdb.com/xml/find"; + val queryParams = new ImmutableMap.Builder() .put("q", movie.getTitle()) .put("tt", "on") .put("nr", "1") @@ -37,8 +34,8 @@ public URL buildSearchURL(final Movie movie) { * {@inheritDoc} */ @Override - public List parseSearchResult(final String data) { - Document document = Jsoup.parse(requireNonNull(data)); + public List parseSearchResult(@NonNull final String data) { + Document document = Jsoup.parse(data); XMLMovieParser parser = new XMLMovieParser(); return document.getElementsByTag("ImdbEntity") diff --git a/core/src/main/java/org/f0w/k2i/core/handler/AddToWatchlistHandler.java b/core/src/main/java/org/f0w/k2i/core/handler/AddToWatchlistHandler.java index 97887c8..607e476 100644 --- a/core/src/main/java/org/f0w/k2i/core/handler/AddToWatchlistHandler.java +++ b/core/src/main/java/org/f0w/k2i/core/handler/AddToWatchlistHandler.java @@ -55,7 +55,7 @@ protected void handleMovie(ImportProgress importProgress, List errors) { } catch (IOException e) { LOG.info("Error adding movie to watchlist: {}", e); - errors.add(new Error(importProgress, e.getMessage())); + errors.add(new Error(importProgress.getMovie(), e.getMessage())); } } } diff --git a/core/src/main/java/org/f0w/k2i/core/handler/ConnectionCheckHandler.java b/core/src/main/java/org/f0w/k2i/core/handler/ConnectionCheckHandler.java index 343eebe..017217c 100644 --- a/core/src/main/java/org/f0w/k2i/core/handler/ConnectionCheckHandler.java +++ b/core/src/main/java/org/f0w/k2i/core/handler/ConnectionCheckHandler.java @@ -2,6 +2,7 @@ import com.google.inject.Inject; import com.typesafe.config.Config; +import lombok.val; import org.f0w.k2i.core.model.entity.ImportProgress; import org.f0w.k2i.core.util.HttpUtils; @@ -23,9 +24,9 @@ public ConnectionCheckHandler(Config config) { */ @Override protected void handleMovie(ImportProgress importProgress, List errors) { - final String url = "www.imdb.com"; - final int port = 80; - final int timeout = config.getInt("timeout"); + val url = "www.imdb.com"; + val port = 80; + val timeout = config.getInt("timeout"); while (!HttpUtils.isReachable(url, port, timeout)) { LOG.info("IMDB url is not reachable, retrying..."); diff --git a/core/src/main/java/org/f0w/k2i/core/handler/MovieHandler.java b/core/src/main/java/org/f0w/k2i/core/handler/MovieHandler.java index 16254d3..1a2d15c 100644 --- a/core/src/main/java/org/f0w/k2i/core/handler/MovieHandler.java +++ b/core/src/main/java/org/f0w/k2i/core/handler/MovieHandler.java @@ -1,5 +1,7 @@ package org.f0w.k2i.core.handler; +import lombok.NonNull; +import lombok.Value; import org.f0w.k2i.core.model.entity.ImportProgress; import org.f0w.k2i.core.model.entity.Movie; import org.slf4j.Logger; @@ -7,7 +9,6 @@ import java.util.EnumSet; import java.util.List; -import java.util.Objects; import java.util.Set; /** @@ -15,7 +16,11 @@ */ public abstract class MovieHandler { protected static final Logger LOG = LoggerFactory.getLogger(MovieHandler.class); + + @NonNull private Set types; + + @NonNull private MovieHandler next; /** @@ -62,7 +67,7 @@ public MovieHandler setNext(MovieHandler handler) { * @param errors List of errors * @param type Type of MovieHandler */ - public void handle(ImportProgress importProgress, List errors, Type type) { + public void handle(@NonNull ImportProgress importProgress, @NonNull List errors, @NonNull Type type) { if (types.contains(type) || isCombinedType(type)) { handleMovie(importProgress, errors); } @@ -72,6 +77,12 @@ public void handle(ImportProgress importProgress, List errors, Type type) } } + /** + * Checks that given type is COMBINED + * + * @param type Type to check + * @return is combined type + */ private boolean isCombinedType(Type type) { return types.contains(Type.COMBINED) || Type.COMBINED.equals(type); } @@ -96,35 +107,12 @@ public enum Type { /** * MovieHandler error */ - public static class Error { - private final Movie movie; - private final String message; - - public Error(ImportProgress importProgress, String message) { - this.movie = importProgress.getMovie(); - this.message = message; - } + @Value + public static final class Error { + @NonNull + private Movie movie; - public Movie getMovie() { - return movie; - } - - public String getMessage() { - return message; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - Error error = (Error) o; - return Objects.equals(getMovie(), error.getMovie()) && - Objects.equals(getMessage(), error.getMessage()); - } - - @Override - public int hashCode() { - return Objects.hash(getMovie(), getMessage()); - } + @NonNull + private String message; } } diff --git a/core/src/main/java/org/f0w/k2i/core/handler/ParseIDHandler.java b/core/src/main/java/org/f0w/k2i/core/handler/ParseIDHandler.java index 252ed62..cdbe4e2 100644 --- a/core/src/main/java/org/f0w/k2i/core/handler/ParseIDHandler.java +++ b/core/src/main/java/org/f0w/k2i/core/handler/ParseIDHandler.java @@ -1,6 +1,7 @@ package org.f0w.k2i.core.handler; import com.google.inject.Inject; +import lombok.NonNull; import org.f0w.k2i.core.comparator.MovieComparator; import org.f0w.k2i.core.exchange.finder.MovieFinder; import org.f0w.k2i.core.model.entity.ImportProgress; @@ -52,7 +53,7 @@ protected void handleMovie(ImportProgress importProgress, List errors) { } catch (IOException e) { LOG.info("Can't prepare movie: {}", e); - errors.add(new Error(importProgress, e.getMessage())); + errors.add(new Error(importProgress.getMovie(), e.getMessage())); } } @@ -63,7 +64,7 @@ protected void handleMovie(ImportProgress importProgress, List errors) { * @param movies Deque in which perform search * @return Optional of found matching movie */ - private Optional findMatchingMovie(Movie movie, Deque movies) { + private Optional findMatchingMovie(@NonNull final Movie movie, @NonNull final Deque movies) { while (!movies.isEmpty()) { Movie imdbMovie = movies.poll(); diff --git a/core/src/main/java/org/f0w/k2i/core/handler/SetRatingHandler.java b/core/src/main/java/org/f0w/k2i/core/handler/SetRatingHandler.java index 9ed0a41..f793dc6 100644 --- a/core/src/main/java/org/f0w/k2i/core/handler/SetRatingHandler.java +++ b/core/src/main/java/org/f0w/k2i/core/handler/SetRatingHandler.java @@ -1,6 +1,7 @@ package org.f0w.k2i.core.handler; import com.google.inject.Inject; +import lombok.NonNull; import org.f0w.k2i.core.exchange.MovieAuthStringFetcher; import org.f0w.k2i.core.exchange.MovieRatingChanger; import org.f0w.k2i.core.model.entity.ImportProgress; @@ -9,6 +10,7 @@ import java.io.IOException; import java.net.HttpURLConnection; import java.util.List; +import java.util.Optional; import static org.f0w.k2i.core.util.MovieUtils.isEmptyIMDBId; import static org.f0w.k2i.core.util.MovieUtils.isEmptyRating; @@ -50,15 +52,7 @@ protected void handleMovie(ImportProgress importProgress, List errors) { return; } - fetcher.sendRequest(movie); - - final String authString = fetcher.getProcessedResponse(); - - if (authString == null) { - throw new IOException("Movie authorisation string is empty!"); - } - - changer.setAuthString(authString); + changer.setAuthString(fetchAuthorisationString(movie)); changer.sendRequest(movie); Integer statusCode = changer.getProcessedResponse(); @@ -73,7 +67,21 @@ protected void handleMovie(ImportProgress importProgress, List errors) { } catch (IOException e) { LOG.info("Error setting rating of movie: {}", e); - errors.add(new Error(importProgress, e.getMessage())); + errors.add(new Error(importProgress.getMovie(), e.getMessage())); } } + + /** + * Fetch the authorisation string for movie. + * + * @param movie For which movie + * @return Authorisation string + * @throws IOException If an I/O error occurs or string is null + */ + private String fetchAuthorisationString(@NonNull final Movie movie) throws IOException { + fetcher.sendRequest(movie); + + return Optional.ofNullable(fetcher.getProcessedResponse()) + .orElseThrow(() -> new IOException("Movie authorisation string is empty!")); + } } diff --git a/core/src/main/java/org/f0w/k2i/core/model/entity/BaseEntity.java b/core/src/main/java/org/f0w/k2i/core/model/entity/BaseEntity.java index bbd069b..7a92fcf 100644 --- a/core/src/main/java/org/f0w/k2i/core/model/entity/BaseEntity.java +++ b/core/src/main/java/org/f0w/k2i/core/model/entity/BaseEntity.java @@ -1,41 +1,17 @@ package org.f0w.k2i.core.model.entity; +import lombok.Data; import org.hibernate.annotations.GenericGenerator; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.MappedSuperclass; -import java.util.Objects; @MappedSuperclass +@Data public class BaseEntity { @Id @GeneratedValue(generator = "increment") @GenericGenerator(name = "increment", strategy = "increment") private Long id; - - protected BaseEntity() { - // Required by HPA - } - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - BaseEntity that = (BaseEntity) o; - return Objects.equals(getId(), that.getId()); - } - - @Override - public int hashCode() { - return Objects.hash(getId()); - } } diff --git a/core/src/main/java/org/f0w/k2i/core/model/entity/ImportProgress.java b/core/src/main/java/org/f0w/k2i/core/model/entity/ImportProgress.java index 98719fc..f7aa403 100644 --- a/core/src/main/java/org/f0w/k2i/core/model/entity/ImportProgress.java +++ b/core/src/main/java/org/f0w/k2i/core/model/entity/ImportProgress.java @@ -1,12 +1,16 @@ package org.f0w.k2i.core.model.entity; -import com.google.common.base.MoreObjects; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; import javax.persistence.*; -import java.util.Objects; @Entity @Table(name = "IMPORT_PROGRESS", uniqueConstraints = @UniqueConstraint(columnNames = {"KINOPOISK_FILE_ID", "MOVIE_ID"})) +@Data +@NoArgsConstructor +@AllArgsConstructor public class ImportProgress extends BaseEntity { @ManyToOne @JoinColumn(name = "KINOPOISK_FILE_ID", nullable = false) @@ -21,75 +25,4 @@ public class ImportProgress extends BaseEntity { @Column(name = "RATED", nullable = false) private boolean rated; - - protected ImportProgress() { - // Required by JPA - } - - public ImportProgress(KinopoiskFile kinopoiskFile, Movie movie, boolean imported, boolean rated) { - setKinopoiskFile(kinopoiskFile); - setMovie(movie); - setImported(imported); - setRated(rated); - } - - public Movie getMovie() { - return movie; - } - - public void setMovie(Movie movie) { - this.movie = movie; - } - - public KinopoiskFile getKinopoiskFile() { - return kinopoiskFile; - } - - public void setKinopoiskFile(KinopoiskFile kinopoiskFile) { - this.kinopoiskFile = kinopoiskFile; - } - - public boolean isImported() { - return imported; - } - - public void setImported(boolean imported) { - this.imported = imported; - } - - public boolean isRated() { - return rated; - } - - public void setRated(boolean rated) { - this.rated = rated; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - ImportProgress that = (ImportProgress) o; - return isImported() == that.isImported() && - isRated() == that.isRated() && - Objects.equals(getKinopoiskFile(), that.getKinopoiskFile()) && - Objects.equals(getMovie(), that.getMovie()); - } - - @Override - public int hashCode() { - return Objects.hash(getKinopoiskFile(), getMovie(), isImported(), isRated()); - } - - @Override - public String toString() { - return MoreObjects.toStringHelper(this) - .omitNullValues() - .add("id", getId()) - .add("kinopoiskFile", getKinopoiskFile()) - .add("movie", getMovie()) - .add("imported", isImported()) - .add("rated", isRated()) - .toString(); - } } diff --git a/core/src/main/java/org/f0w/k2i/core/model/entity/KinopoiskFile.java b/core/src/main/java/org/f0w/k2i/core/model/entity/KinopoiskFile.java index c2dde2e..b094011 100644 --- a/core/src/main/java/org/f0w/k2i/core/model/entity/KinopoiskFile.java +++ b/core/src/main/java/org/f0w/k2i/core/model/entity/KinopoiskFile.java @@ -1,56 +1,21 @@ package org.f0w.k2i.core.model.entity; -import com.google.common.base.MoreObjects; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.NonNull; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Table; -import java.util.Objects; - -import static java.util.Objects.requireNonNull; @Entity @Table(name = "KINOPOISK_FILE") +@Data +@NoArgsConstructor +@AllArgsConstructor public class KinopoiskFile extends BaseEntity { @Column(name = "HASH_CODE", unique = true, nullable = false) + @NonNull private String hashCode; - - protected KinopoiskFile() { - // Required by JPA - } - - public KinopoiskFile(String hashCode) { - setHashCode(hashCode); - } - - public String getHashCode() { - return hashCode; - } - - public void setHashCode(String checksum) { - this.hashCode = requireNonNull(checksum); - } - - @Override - public boolean equals(Object obj) { - if (obj == null || getClass() != obj.getClass()) { - return false; - } - - return Objects.equals(getHashCode(), ((KinopoiskFile) obj).getHashCode()); - } - - @Override - public int hashCode() { - return Objects.hash(getHashCode()); - } - - @Override - public String toString() { - return MoreObjects.toStringHelper(this) - .omitNullValues() - .add("id", getId()) - .add("hashCode", getHashCode()) - .toString(); - } } diff --git a/core/src/main/java/org/f0w/k2i/core/model/entity/Movie.java b/core/src/main/java/org/f0w/k2i/core/model/entity/Movie.java index e74a3f8..6905885 100644 --- a/core/src/main/java/org/f0w/k2i/core/model/entity/Movie.java +++ b/core/src/main/java/org/f0w/k2i/core/model/entity/Movie.java @@ -1,19 +1,21 @@ package org.f0w.k2i.core.model.entity; -import com.google.common.base.MoreObjects; +import lombok.*; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Table; import javax.persistence.UniqueConstraint; -import java.util.Objects; - -import static java.util.Objects.requireNonNull; @Entity @Table(name = "MOVIES", uniqueConstraints = @UniqueConstraint(columnNames = {"TITLE", "YEAR"})) +@Data +@NoArgsConstructor +@AllArgsConstructor +@EqualsAndHashCode(callSuper = false, of = {"title", "year"}) public class Movie extends BaseEntity { @Column(name = "TITLE", nullable = false) + @NonNull private String title; @Column(name = "YEAR", nullable = false) @@ -25,17 +27,6 @@ public class Movie extends BaseEntity { @Column(name = "IMDB_ID") private String imdbId; - protected Movie() { - // Required by JPA - } - - public Movie(String title, int year, Integer rating, String imdbId) { - setTitle(title); - setYear(year); - setRating(rating); - setImdbId(imdbId); - } - public Movie(String title, int year) { this(title, year, null, null); } @@ -47,64 +38,4 @@ public Movie(String title, int year, Integer rating) { public Movie(String title, int year, String imdbId) { this(title, year, null, imdbId); } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = requireNonNull(title); - } - - public int getYear() { - return year; - } - - public void setYear(int year) { - this.year = year; - } - - public Integer getRating() { - return rating; - } - - public void setRating(Integer rating) { - this.rating = rating; - } - - public String getImdbId() { - return imdbId; - } - - public void setImdbId(String imdbId) { - this.imdbId = imdbId; - } - - @Override - public boolean equals(Object obj) { - if (obj == null || getClass() != obj.getClass()) { - return false; - } - - Movie other = (Movie) obj; - return Objects.equals(getTitle(), other.getTitle()) - && Objects.equals(getYear(), other.getYear()); - } - - @Override - public int hashCode() { - return Objects.hash(getTitle(), getYear()); - } - - @Override - public String toString() { - return MoreObjects.toStringHelper(this) - .omitNullValues() - .add("id", getId()) - .add("title", getTitle()) - .add("year", getYear()) - .add("rating", getRating()) - .add("imdb_id", getImdbId()) - .toString(); - } } diff --git a/core/src/main/java/org/f0w/k2i/core/model/service/ImportProgressService.java b/core/src/main/java/org/f0w/k2i/core/model/service/ImportProgressService.java index 7ecbd62..c9e0f9e 100644 --- a/core/src/main/java/org/f0w/k2i/core/model/service/ImportProgressService.java +++ b/core/src/main/java/org/f0w/k2i/core/model/service/ImportProgressService.java @@ -4,6 +4,7 @@ import com.google.common.io.Files; import com.google.inject.Inject; import com.google.inject.persist.Transactional; +import lombok.val; import org.f0w.k2i.core.handler.MovieHandler; import org.f0w.k2i.core.model.entity.ImportProgress; import org.f0w.k2i.core.model.entity.KinopoiskFile; @@ -44,7 +45,7 @@ public ImportProgressService( public ImportProgressService initialize(Path filePath, boolean cleanRun) { this.filePath = checkFile(filePath); - String fileHashCode = ExceptionUtils.uncheck(() -> Files.hash(filePath.toFile(), Hashing.sha256()).toString()); + val fileHashCode = ExceptionUtils.uncheck(() -> Files.hash(filePath.toFile(), Hashing.sha256()).toString()); if (cleanRun) { removeExistingFileData(fileHashCode); diff --git a/core/src/main/java/org/f0w/k2i/core/util/HttpUtils.java b/core/src/main/java/org/f0w/k2i/core/util/HttpUtils.java index a4c7384..0041413 100644 --- a/core/src/main/java/org/f0w/k2i/core/util/HttpUtils.java +++ b/core/src/main/java/org/f0w/k2i/core/util/HttpUtils.java @@ -2,6 +2,7 @@ import com.google.common.base.Joiner; import com.google.common.net.UrlEscapers; +import lombok.val; import java.io.IOException; import java.net.InetSocketAddress; @@ -27,7 +28,7 @@ private HttpUtils() { * @param timeout Timeout in ms * @return Reachable or not */ - public static boolean isReachable(String hostName, int port, int timeout) { + public static boolean isReachable(final String hostName, int port, int timeout) { try (Socket socket = new Socket()) { socket.connect(new InetSocketAddress(hostName, port), timeout); return true; @@ -42,9 +43,10 @@ public static boolean isReachable(String hostName, int port, int timeout) { * @param hostName Hostname component of URL * @param query Query component of URL * @return URL + * @throws IllegalArgumentException If URL is invalid */ public static URL buildURL(final String hostName, final Map query) { - Map escapedQuery = query.entrySet() + val escapedQuery = query.entrySet() .stream() .collect(Collectors.toMap( Map.Entry::getKey, diff --git a/core/src/main/java/org/f0w/k2i/core/util/MovieUtils.java b/core/src/main/java/org/f0w/k2i/core/util/MovieUtils.java index 688dd35..6ed270b 100644 --- a/core/src/main/java/org/f0w/k2i/core/util/MovieUtils.java +++ b/core/src/main/java/org/f0w/k2i/core/util/MovieUtils.java @@ -1,5 +1,6 @@ package org.f0w.k2i.core.util; +import lombok.val; import org.apache.commons.lang3.StringUtils; import org.f0w.k2i.core.model.entity.Movie; import org.f0w.k2i.core.util.exception.KinopoiskToIMDBException; @@ -12,7 +13,6 @@ import java.nio.file.Path; import java.util.Arrays; import java.util.List; -import java.util.Map; import java.util.stream.Collectors; import static org.f0w.k2i.core.util.exception.ExceptionUtils.uncheck; @@ -30,8 +30,8 @@ private MovieUtils() { * @param title String to parseSearchResult * @return Parsed title */ - public static String parseTitle(String title) { - String resultTitle = StringUtils.replaceEachRepeatedly( + public static String parseTitle(final String title) { + val resultTitle = StringUtils.replaceEachRepeatedly( String.valueOf(title).trim(), new String[]{"«", "»"}, new String[]{"", ""} @@ -51,8 +51,8 @@ public static String parseTitle(String title) { * @param fallback Fallback string * @return Parsed title or defaultTitle */ - public static String parseTitle(String title, String fallback) { - String resultTitle = parseTitle(title); + public static String parseTitle(final String title, final String fallback) { + val resultTitle = parseTitle(title); if ("null".equals(resultTitle)) { return parseTitle(fallback); @@ -67,11 +67,11 @@ public static String parseTitle(String title, String fallback) { * @param yearString String to parseSearchResult * @return Parsed year or 0 */ - public static int parseYear(String yearString) { - final int yearLength = 4; + public static int parseYear(final String yearString) { + val yearLength = 4; try { - String resultYear = String.valueOf(yearString).trim().substring(0, yearLength); + val resultYear = String.valueOf(yearString).trim().substring(0, yearLength); return Integer.parseInt(resultYear); } catch (NumberFormatException | IndexOutOfBoundsException e) { @@ -85,8 +85,8 @@ public static int parseYear(String yearString) { * @param imdbId String to parseSearchResult * @return Parsed IMDB ID or null */ - public static String parseIMDBId(String imdbId) { - String resultImdbId = String.valueOf(imdbId).trim(); + public static String parseIMDBId(final String imdbId) { + val resultImdbId = String.valueOf(imdbId).trim(); if (!resultImdbId.startsWith("tt") || resultImdbId.length() < 3) { return null; @@ -101,16 +101,16 @@ public static String parseIMDBId(String imdbId) { * @param rating String to parseSearchResult * @return Parsed rating or null */ - public static Integer parseRating(String rating) { - String resultRating = String.valueOf(rating).trim(); - List zeroValues = Arrays.asList("", "null", "zero", "0"); + public static Integer parseRating(final String rating) { + val resultRating = String.valueOf(rating).trim(); + val zeroValues = Arrays.asList("", "null", "zero", "0"); if (zeroValues.contains(resultRating)) { return null; } try { - Integer result = Integer.parseInt(resultRating); + val result = Integer.parseInt(resultRating); return result <= 10 ? result : null; } catch (NumberFormatException ignore) { @@ -124,7 +124,7 @@ public static Integer parseRating(String rating) { * @param title String to check * @return Is title empty */ - public static boolean isEmptyTitle(String title) { + public static boolean isEmptyTitle(final String title) { return "null".equals(title); } @@ -134,7 +134,7 @@ public static boolean isEmptyTitle(String title) { * @param year Integer to check * @return Is year empty */ - public static boolean isEmptyYear(int year) { + public static boolean isEmptyYear(final int year) { return year == 0; } @@ -144,7 +144,7 @@ public static boolean isEmptyYear(int year) { * @param imdbId String to check * @return Is IMDB ID null */ - public static boolean isEmptyIMDBId(String imdbId) { + public static boolean isEmptyIMDBId(final String imdbId) { return imdbId == null; } @@ -154,7 +154,7 @@ public static boolean isEmptyIMDBId(String imdbId) { * @param rating String to check * @return Is rating null */ - public static boolean isEmptyRating(Integer rating) { + public static boolean isEmptyRating(final Integer rating) { return rating == null; } @@ -165,21 +165,21 @@ public static boolean isEmptyRating(Integer rating) { * @return List of parsed movies * @throws KinopoiskToIMDBException If an I/O error occurs */ - public static List parseMovies(Path filePath) { - String data = uncheck(() -> new String(Files.readAllBytes(filePath), Charset.forName("windows-1251"))); + public static List parseMovies(final Path filePath) { + val data = uncheck(() -> new String(Files.readAllBytes(filePath), Charset.forName("windows-1251"))); Elements content = Jsoup.parse(data).select("table tr"); if (content.isEmpty()) { throw new KinopoiskToIMDBException(String.format("File '%s' is not valid or empty!", filePath)); } - List header = content.remove(0) + val header = content.remove(0) .getElementsByTag("td") .stream() .map(Element::text) .collect(Collectors.toList()); - List> elements = content.stream() + val rows = content.stream() .map(e -> e.getElementsByTag("td") .stream() .map(Element::text) @@ -188,7 +188,7 @@ public static List parseMovies(Path filePath) { .map(e -> CollectionUtils.combineLists(header, e)) .collect(Collectors.toList()); - return elements.stream() + return rows.stream() .map(m -> new Movie( parseTitle(m.get("оригинальное название"), m.get("русскоязычное название")), parseYear(m.get("год")), diff --git a/core/src/test/java/org/f0w/k2i/TestHelper.java b/core/src/test/java/org/f0w/k2i/TestHelper.java index 0f98830..9c9fdfb 100644 --- a/core/src/test/java/org/f0w/k2i/TestHelper.java +++ b/core/src/test/java/org/f0w/k2i/TestHelper.java @@ -6,6 +6,7 @@ public final class TestHelper { /** * Checks that constructor of the given class is private + * * @param clazz Class to check * @return Is private constructor * @throws Exception @@ -19,6 +20,7 @@ public static boolean isConstructorPrivate(Class clazz) throws Exception { /** * Calls the private constructor + * * @param clazz Class which constructor to call * @throws Exception * @see Constructor#setAccessible(boolean) diff --git a/core/src/test/java/org/f0w/k2i/core/comparator/MovieComparatorFactoryTest.java b/core/src/test/java/org/f0w/k2i/core/comparator/MovieComparatorFactoryTest.java index 271c918..7596cfb 100644 --- a/core/src/test/java/org/f0w/k2i/core/comparator/MovieComparatorFactoryTest.java +++ b/core/src/test/java/org/f0w/k2i/core/comparator/MovieComparatorFactoryTest.java @@ -2,37 +2,40 @@ import com.google.common.collect.ImmutableMap; import com.typesafe.config.ConfigFactory; +import lombok.val; import org.f0w.k2i.core.comparator.title.*; import org.f0w.k2i.core.comparator.year.DeviationYearComparator; import org.f0w.k2i.core.comparator.year.EqualsYearComparator; import org.junit.Test; -import java.util.Map; - import static org.f0w.k2i.core.comparator.MovieComparator.Type; import static org.f0w.k2i.core.comparator.MovieComparator.Type.*; import static org.junit.Assert.assertTrue; public class MovieComparatorFactoryTest { - private static final MovieComparatorFactory FACTORY = new MovieComparatorFactory(ConfigFactory.load()); + private final MovieComparatorFactory factory = new MovieComparatorFactory(ConfigFactory.load()); @Test public void testMake() throws Exception { - final Map> classMap = - new ImmutableMap.Builder>() - .put(YEAR_EQUALS, EqualsYearComparator.class) - .put(YEAR_DEVIATION, DeviationYearComparator.class) - .put(TITLE_CONTAINS, ContainsTitleComparator.class) - .put(TITLE_ENDS, EndsWithTitleComparator.class) - .put(TITLE_STARTS, StartsWithTitleComparator.class) - .put(TITLE_EQUALS, EqualsTitleComparator.class) - .put(TITLE_SMART, SmartTitleComparator.class) - .build(); + val classMap = new ImmutableMap.Builder>() + .put(YEAR_EQUALS, EqualsYearComparator.class) + .put(YEAR_DEVIATION, DeviationYearComparator.class) + .put(TITLE_CONTAINS, ContainsTitleComparator.class) + .put(TITLE_ENDS, EndsWithTitleComparator.class) + .put(TITLE_STARTS, StartsWithTitleComparator.class) + .put(TITLE_EQUALS, EqualsTitleComparator.class) + .put(TITLE_SMART, SmartTitleComparator.class) + .build(); classMap.forEach((type, clazz) -> { - MovieComparator instance = FACTORY.make(type); + MovieComparator instance = factory.make(type); assertTrue(clazz.isInstance(instance)); }); } + + @Test(expected = NullPointerException.class) + public void testMakeWithNull() throws Exception { + factory.make(null); + } } \ No newline at end of file diff --git a/core/src/test/java/org/f0w/k2i/core/event/ImportFinishedEventTest.java b/core/src/test/java/org/f0w/k2i/core/event/ImportFinishedEventTest.java index ef9f23f..5f1221a 100644 --- a/core/src/test/java/org/f0w/k2i/core/event/ImportFinishedEventTest.java +++ b/core/src/test/java/org/f0w/k2i/core/event/ImportFinishedEventTest.java @@ -1,6 +1,6 @@ package org.f0w.k2i.core.event; -import org.f0w.k2i.ImportProgressTestData; +import org.f0w.k2i.MovieTestData; import org.f0w.k2i.core.handler.MovieHandler; import org.junit.Test; @@ -13,14 +13,8 @@ public class ImportFinishedEventTest { @Test public void testConstructorInitialization() { List expected = Arrays.asList( - new MovieHandler.Error( - ImportProgressTestData.IMPORT_PROGRESS_1, - "Error 1" - ), - new MovieHandler.Error( - ImportProgressTestData.IMPORT_PROGRESS_2, - "Error 2" - ) + new MovieHandler.Error(MovieTestData.MOVIE_1, "Error 1"), + new MovieHandler.Error(MovieTestData.MOVIE_2, "Error 2") ); ImportFinishedEvent event = new ImportFinishedEvent(expected); diff --git a/core/src/test/java/org/f0w/k2i/core/exchange/finder/MovieFinderFactoryTest.java b/core/src/test/java/org/f0w/k2i/core/exchange/finder/MovieFinderFactoryTest.java index bd780fb..0e24d67 100644 --- a/core/src/test/java/org/f0w/k2i/core/exchange/finder/MovieFinderFactoryTest.java +++ b/core/src/test/java/org/f0w/k2i/core/exchange/finder/MovieFinderFactoryTest.java @@ -2,32 +2,35 @@ import com.google.common.collect.ImmutableMap; import com.typesafe.config.ConfigFactory; +import lombok.val; import org.junit.Test; -import java.util.Map; - import static org.f0w.k2i.core.exchange.finder.MovieFinder.Type; import static org.f0w.k2i.core.exchange.finder.MovieFinder.Type.*; import static org.junit.Assert.assertTrue; public class MovieFinderFactoryTest { - private static final MovieFinderFactory FACTORY = new MovieFinderFactory(ConfigFactory.load()); + private final MovieFinderFactory factory = new MovieFinderFactory(ConfigFactory.load()); @Test public void testMake() throws Exception { - final Map> classMap = - new ImmutableMap.Builder>() - .put(MIXED, MixedMovieFinder.class) - .put(XML, BasicMovieFinder.class) - .put(JSON, BasicMovieFinder.class) - .put(HTML, BasicMovieFinder.class) - .build(); + val classMap = new ImmutableMap.Builder>() + .put(MIXED, MixedMovieFinder.class) + .put(XML, BasicMovieFinder.class) + .put(JSON, BasicMovieFinder.class) + .put(HTML, BasicMovieFinder.class) + .build(); classMap.forEach((type, clazz) -> { - MovieFinder instance = FACTORY.make(type); + MovieFinder instance = factory.make(type); assertTrue(clazz.isInstance(instance)); assertTrue(instance.getType().equals(type)); }); } + + @Test(expected = NullPointerException.class) + public void testMakeWithNull() throws Exception { + factory.make(null); + } } \ No newline at end of file diff --git a/core/src/test/java/org/f0w/k2i/core/model/repository/mock/MockImportProgressRepositoryImpl.java b/core/src/test/java/org/f0w/k2i/core/model/repository/mock/MockImportProgressRepositoryImpl.java index 40631fd..8f964b1 100644 --- a/core/src/test/java/org/f0w/k2i/core/model/repository/mock/MockImportProgressRepositoryImpl.java +++ b/core/src/test/java/org/f0w/k2i/core/model/repository/mock/MockImportProgressRepositoryImpl.java @@ -9,8 +9,7 @@ import java.util.stream.Collectors; public class MockImportProgressRepositoryImpl extends BaseMockRepository - implements ImportProgressRepository -{ + implements ImportProgressRepository { @Override public void saveAll(KinopoiskFile kinopoiskFile, List movies) { movies.forEach(m -> save(new ImportProgress(kinopoiskFile, m, false, false))); diff --git a/core/src/test/java/org/f0w/k2i/core/model/repository/mock/MockKinopoiskFileRepositoryImpl.java b/core/src/test/java/org/f0w/k2i/core/model/repository/mock/MockKinopoiskFileRepositoryImpl.java index 447c8d7..2a7b124 100644 --- a/core/src/test/java/org/f0w/k2i/core/model/repository/mock/MockKinopoiskFileRepositoryImpl.java +++ b/core/src/test/java/org/f0w/k2i/core/model/repository/mock/MockKinopoiskFileRepositoryImpl.java @@ -4,8 +4,7 @@ import org.f0w.k2i.core.model.repository.KinopoiskFileRepository; public class MockKinopoiskFileRepositoryImpl extends BaseMockRepository - implements KinopoiskFileRepository -{ + implements KinopoiskFileRepository { @Override public KinopoiskFile findByHashCode(String hashCode) { return storage.values() diff --git a/core/src/test/java/org/f0w/k2i/core/util/IOUtilsTest.java b/core/src/test/java/org/f0w/k2i/core/util/IOUtilsTest.java index ce62a71..c373aff 100644 --- a/core/src/test/java/org/f0w/k2i/core/util/IOUtilsTest.java +++ b/core/src/test/java/org/f0w/k2i/core/util/IOUtilsTest.java @@ -19,13 +19,12 @@ import static org.junit.Assert.assertTrue; public class IOUtilsTest { + @Rule + public ExpectedException expected = ExpectedException.none(); private FileSystem fileSystem; private Path temporaryFile; private Path temporaryDirectory; - @Rule - public ExpectedException expected = ExpectedException.none(); - @Before public void setUp() throws Exception { fileSystem = Jimfs.newFileSystem();