Source:
public void write(ChannelGroup<?> entity, FieldWriter writer, OutputContext ctxt) throws IOException { final ImmutableMultimap.Builder<Id, ChannelGroupMembership> builder = ImmutableMultimap.builder(); Iterable<? extends ChannelGroupMembership> availableChannels = entity.getChannelsAvailable( LocalDate.now()); List<Id> orderedIds = StreamSupport.stream(availableChannels.spliterator(), false) .map(cm -> cm.getChannel().getId()) .distinct() .collect(Collectors.toList()); Ordering<Id> idOrdering = Ordering.explicit(orderedIds); for (ChannelGroupMembership channelGroupMembership : availableChannels) { builder.put(channelGroupMembership.getChannel().getId(), channelGroupMembership); } ImmutableMultimap<Id, ChannelGroupMembership> channelGroupMemberships = builder.build(); String genre = ctxt.getRequest().getParameter(Attributes.CHANNEL_GROUP_CHANNEL_GENRES.externalName()); Iterable<Channel> channels = Futures.get( Futures.transform( this.channelResolver.resolveIds(channelGroupMemberships.keySet()), (Resolved<Channel> channelResolved) -> { return StreamSupport.stream(channelResolved.getResources().spliterator(), false) .filter(channel -> channel.getAdvertiseFrom().isBeforeNow() || channel.getAdvertiseFrom().isEqualNow()) .sorted((o1, o2) -> idOrdering.compare(o1.getId(), o2.getId())) .collect(Collectors.toList()); } ), 1, TimeUnit.MINUTES, IOException.class ); if (!Strings.isNullOrEmpty(genre)) { final ImmutableSet<String> genres = ImmutableSet.copyOf(Splitter.on(',').split(genre)); channels = Iterables.filter(channels, new Predicate<Channel>() { @Override public boolean apply(Channel input) { return !Sets.intersection(input.getGenres(), genres).isEmpty(); } }); } ImmutableSet.Builder<ChannelWithChannelGroupMembership> resultBuilder = ImmutableSet.builder(); for (Channel channel : channels) { for (ChannelGroupMembership channelGroupMembership : channelGroupMemberships.get(channel.getId())) { resultBuilder.add( new ChannelWithChannelGroupMembership( channel, channelGroupMembership ) ); } } ImmutableSet<ChannelWithChannelGroupMembership> result = resultBuilder.build(); writer.writeList(channelWriter, result, ctxt); }

Target:
public void write(ChannelGroup<?> entity, FieldWriter writer, OutputContext ctxt) throws IOException { final ImmutableMultimap.Builder<Id, ChannelGroupMembership> builder = ImmutableMultimap.builder(); Iterable<? extends ChannelGroupMembership> availableChannels = entity.getChannelsAvailable( LocalDate.now()); List<Id> orderedIds = StreamSupport.stream(availableChannels.spliterator(), false) .map(cm -> cm.getChannel().getId()) .distinct() .collect(Collectors.toList()); Ordering<Id> idOrdering = Ordering.explicit(orderedIds); for (ChannelGroupMembership channelGroupMembership : availableChannels) { builder.put(channelGroupMembership.getChannel().getId(), channelGroupMembership); } ImmutableMultimap<Id, ChannelGroupMembership> channelGroupMemberships = builder.build(); String genre = ctxt.getRequest().getParameter(Attributes.CHANNEL_GROUP_CHANNEL_GENRES.externalName()); Iterable<Channel> channels = Futures.get( Futures.transform( this.channelResolver.resolveIds(channelGroupMemberships.keySet()), (Resolved<Channel> channelResolved) -> { return StreamSupport.stream(channelResolved.getResources().spliterator(), false) .filter(channel -> channel.getAdvertiseFrom() == null || channel.getAdvertiseFrom().isBeforeNow() || channel.getAdvertiseFrom().isEqualNow()) .sorted((o1, o2) -> idOrdering.compare(o1.getId(), o2.getId())) .collect(Collectors.toList()); } ), 1, TimeUnit.MINUTES, IOException.class ); if (!Strings.isNullOrEmpty(genre)) { final ImmutableSet<String> genres = ImmutableSet.copyOf(Splitter.on(',').split(genre)); channels = Iterables.filter(channels, new Predicate<Channel>() { @Override public boolean apply(Channel input) { return !Sets.intersection(input.getGenres(), genres).isEmpty(); } }); } ImmutableSet.Builder<ChannelWithChannelGroupMembership> resultBuilder = ImmutableSet.builder(); for (Channel channel : channels) { for (ChannelGroupMembership channelGroupMembership : channelGroupMemberships.get(channel.getId())) { resultBuilder.add( new ChannelWithChannelGroupMembership( channel, channelGroupMembership ) ); } } ImmutableSet<ChannelWithChannelGroupMembership> result = resultBuilder.build(); writer.writeList(channelWriter, result, ctxt); }