Skip to content

Instantly share code, notes, and snippets.

@Scuilion
Created October 27, 2015 04:50
Show Gist options
  • Save Scuilion/2f36e2a729db79be5118 to your computer and use it in GitHub Desktop.
Save Scuilion/2f36e2a729db79be5118 to your computer and use it in GitHub Desktop.
Java 8 Fun with filters
public class Utils {
public static List<IArtist> cull(List<IArtist> artists, List<GenreType> include, List<GenreType> exclude) {
List<GenreType> optExclude = Optional.ofNullable(exclude).orElse(new ArrayList<GenreType>());
List<GenreType> optInclude = Optional.ofNullable(include).orElse(new ArrayList<GenreType>());
return artists.stream()
.filter(isMatch(optInclude))
.filter(isMatch(optExclude).negate())
.collect(Collectors.toList());
}
public static Predicate<IArtist> isMatch(List<GenreType> matcher) {
return p -> !Collections.disjoint(p.getGenreTypes(), matcher);
}
}
public class TestCull {
@Test
public void testNullGenreType() {
List<IArtist> artists = new ArrayList<>();
artists.add(new Artist("name1", Arrays.asList(GenreType.POP, GenreType.ROCK)));
artists.add(new Artist("name2", Arrays.asList(GenreType.POP)));
artists.add(new Artist("name3", Arrays.asList(GenreType.RAP)));
//artists.add(new Artist("name4"));
//System.out.println(artists.get(3).getGenreTypes().contains(GenreType.RAP));
List<GenreType> include = new ArrayList<>();
include.add(GenreType.POP);
List<IArtist> culled = Utils.cull(artists, include, null );
assertEquals(2, culled.size());
assertEquals("name1", culled.get(0).getName());
assertEquals("name2", culled.get(1).getName());
}
@Test
public void testIncludeOnly() {
List<IArtist> artists = new ArrayList<>();
artists.add(new Artist("name1", Arrays.asList(GenreType.POP, GenreType.ROCK)));
artists.add(new Artist("name2", Arrays.asList(GenreType.POP)));
artists.add(new Artist("name3", Arrays.asList(GenreType.RAP)));
//artists.add(new Artist("name4"));
//System.out.println(artists.get(3).getGenreTypes().contains(GenreType.RAP));
List<GenreType> include = new ArrayList<>();
include.add(GenreType.POP);
List<GenreType> exclude = new ArrayList<>();
List<IArtist> culled = Utils.cull(artists, include, exclude);
assertEquals(2, culled.size());
assertEquals("name1", culled.get(0).getName());
assertEquals("name2", culled.get(1).getName());
}
@Test
public void testIncludeAndExcludeOnly() {
List<IArtist> artists = new ArrayList<>();
artists.add(new Artist("name1", Arrays.asList(GenreType.POP, GenreType.ROCK)));
artists.add(new Artist("name2", Arrays.asList(GenreType.POP, GenreType.RAP)));
artists.add(new Artist("name3", Arrays.asList(GenreType.RAP)));
List<GenreType> include = new ArrayList<>();
include.add(GenreType.POP);
List<GenreType> exclude = new ArrayList<>();
exclude.add(GenreType.RAP);
List<IArtist> culled = Utils.cull(artists, include, exclude);
assertEquals(1, culled.size());
assertEquals("name1", culled.get(0).getName());
}
@Test
public void excludeOnly() {
List<IArtist> artists = new ArrayList<>();
artists.add(new Artist("name1", Arrays.asList(GenreType.POP, GenreType.ROCK)));
artists.add(new Artist("name2", Arrays.asList(GenreType.POP, GenreType.RAP)));
artists.add(new Artist("name3", Arrays.asList(GenreType.RAP)));
List<GenreType> include = new ArrayList<>();
List<GenreType> exclude = new ArrayList<>();
exclude.add(GenreType.RAP);
List<IArtist> culled = Utils.cull(artists, include, exclude);
assertEquals(0, culled.size());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment