Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better idiom for listing inner items of a 1-to-N-to-1 relationship?

Tags:

java

guava

I have the following model:

1 RepositoryDTO can have many ResourceDTOs, and in each ResourceDTO is exacly one TeamDTO.

So to get the TeamDTOs from the RepositoryDTO, I am doing the following:

RepositoryDTO repoDTO = ...
List<TeamDTO> teamsLinkedToRepo = getTeamsLinkedTo(repoDTO);

private List<TeamDTO> getTeamsLinkedTo(final RepositoryDTO repository) {
    final List<TeamDTO> teamsLinkedToRepository = new ArrayList<TeamDTO>();
    for (final ResourceDTO resourceDTO : repository.getResources()) {
      teamsLinkedToRepository.add(resourceDTO.getTeam());
    }
    return teamsLinkedToRepository;
}

I'm just wondering is there a better idiom for doing this, maybe using Google Guava?

like image 674
user2586917 Avatar asked Dec 06 '13 11:12

user2586917


1 Answers

Keep simple things simple.

We used Google Guava excessively in one of our projects. And while it was less code and faster to read, it became a nightmare when debugging. So I advice to not use it until you get an huge advantage from it (and simple for-loops won't get any better with it).

With pure Java it's good code. There is no need to improve it.

like image 172
Christian Strempfer Avatar answered Nov 15 '22 01:11

Christian Strempfer