Mam metodę, która ustawia dane za pomocą wzorca Builder w DTO obiektu Movie
. Problem polega na tym, że ta metoda jest nieco skomplikowana, a IntelliJ wyświetla ostrzeżenie o zbyt dużym skomplikowaniu.
private static Movie.Builder initMovieDto(final MovieEntity movieEntity) {
final Movie.Builder builder = (Movie.Builder) new Movie.Builder(
movieEntity.getTitle(),
movieEntity.getType()
)
.withId(movieEntity.getId().toString());
movieEntity.getRating().ifPresent(builder::withRating);
movieEntity.getRating().ifPresent(ratings -> builder.withNumberOfRating(movieEntity.getRatings().size()));
final Optional<String> locatedTitle = movieEntity.getOtherTitles().stream()
.filter(ot -> ot.getStatus() == DataStatus.ACCEPTED)
.map(ServiceUtils::toOtherTitleDto)
.filter(ot -> ot.getCountry() != null
&& ot.getCountry().getCode().equals(Locale.getDefault().getCountry()))
.map(OtherTitle::getTitle)
.findFirst();
locatedTitle.ifPresent(builder::withTitleLocated);
final Supplier<Stream<ReleaseDate>> releaseDateStream = () -> movieEntity.getReleaseDates().stream()
.filter(ot -> ot.getStatus() == DataStatus.ACCEPTED)
.map(ServiceUtils::toReleaseDateDto);
Optional<ReleaseDate> releaseDate = releaseDateStream.get()
.filter(rd -> rd.getCountry().getCode().equals(Locale.getDefault().getCountry()))
.findFirst();
if(releaseDate.isPresent()) {
builder.withReleaseDate(releaseDate.get());
} else {
releaseDate = releaseDateStream.get()
.filter(rd -> movieEntity.getCountries().stream()
.filter(c -> c.getStatus() == DataStatus.ACCEPTED)
.map(ServiceUtils::toCountryDto)
.collect(Collectors.toList()).stream()
.map(Country::getCountry)
.collect(Collectors.toList())
.contains(rd.getCountry()))
.findFirst();
if(releaseDate.isPresent()) {
builder.withReleaseDate(releaseDate.get());
} else {
releaseDate = releaseDateStream.get().findFirst();
builder.withReleaseDate(releaseDate.orElse(null));
}
}
builder.withCountries(movieEntity.getCountries().stream().filter(c -> c.getStatus() == DataStatus.ACCEPTED).map(MovieCountryEntity::getCountry).collect(Collectors.toList()));
builder.withLanguages(movieEntity.getLanguages().stream().filter(l -> l.getStatus() == DataStatus.ACCEPTED).map(MovieLanguageEntity::getLanguage).collect(Collectors.toList()));
builder.withGenres(movieEntity.getGenres().stream().filter(g -> g.getStatus() == DataStatus.ACCEPTED).map(MovieGenreEntity::getGenre).collect(Collectors.toList()));
builder.withBoxofficeCumulative(movieEntity.getBoxOffices().stream().filter(bo -> bo.getStatus() == DataStatus.ACCEPTED).map(MovieBoxOfficeEntity::getBoxOffice).reduce(BigDecimal::add).orElse(null));
builder.withOutline(movieEntity.getOutlines().stream().filter(o -> o.getStatus() == DataStatus.ACCEPTED).map(MovieOutlineEntity::getOutline).findFirst().orElse(null));
builder.withSummary(movieEntity.getSummaries().stream().filter(s -> s.getStatus() == DataStatus.ACCEPTED).map(MovieSummaryEntity::getSummary).findFirst().orElse(null));
return builder;
}
Intellij pokazuje mi ostrzeżenie
Method 'initMovieDto' is too complex to analyze by data flow algorithm less... (Ctrl+F1)
This inspection analyzes method control and data flow to report possible conditions that are always true or false, expressions whose value is statically proven to be constant, and situations that can lead to nullability contract violations.
Co gorsza. W przyszłości jak rozwinę obiekt o nowe informacje, to ta metoda będzie jeszcze bardzie rozwinięta.
Czy macie pomysł jak zoptymalizować tą metodę? W tej metodzie muszę ustawić tak dużo danych w obiekcie DTO Movie
.