@Test public void groupsArtistsByIndexLetter() { Map<Character, String> letterIndex = new HashMap<>(); letterIndex.put('A', "A-Y"); letterIndex.put('Z', "Z"); letterIndex.put('#', "#"); List<Artist> artists = new ArrayList<>(); artists.add(new Artist("Arkana")); artists.add(new Artist("Billie Idol")); artists.add(new Artist("Cher")); for (Artist artist : artists) { artist.setSortName(artist.getName()); } Map<String, List<Artist>> artistIndex = artistIndexService.createArtistIndex(letterIndex, artists); Assert.assertEquals(2, artistIndex.keySet().size()); Assert.assertTrue(artistIndex.containsKey("A-Y")); Assert.assertTrue(artistIndex.containsKey("#")); List<Artist> bcArtists = artistIndex.get("#"); Assert.assertEquals(2, bcArtists.size()); Assert.assertTrue(bcArtists.get(0).getName().equals("Billie Idol")); Assert.assertTrue(bcArtists.get(1).getName().equals("Cher")); }
protected Map<String, List<Artist>> createArtistIndex(Map<Character, String> letterIndex, List<Artist> artists) { Map<String, List<Artist>> map = new LinkedHashMap<>(); for (String index : letterIndex.values()) { map.put(index, new ArrayList<Artist>()); } for (Artist artist : artists) { char firstLetter = toUpperCase(artist.getSortName().charAt(0)); if (!letterIndex.containsKey(firstLetter)) { firstLetter = HASH_CHAR; } map.get(letterIndex.get(firstLetter)).add(artist); } for (Iterator<String> it = map.keySet().iterator(); it.hasNext();) { if (map.get(it.next()).isEmpty()) { it.remove(); } } return map; }
ArtistIndexService { protected Map<String, List<Artist>> createArtistIndex(Map<Character, String> letterIndex, List<Artist> artists) { Map<String, List<Artist>> map = new LinkedHashMap<>(); for (String index : letterIndex.values()) { map.put(index, new ArrayList<Artist>()); } for (Artist artist : artists) { char firstLetter = toUpperCase(artist.getSortName().charAt(0)); if (!letterIndex.containsKey(firstLetter)) { firstLetter = HASH_CHAR; } map.get(letterIndex.get(firstLetter)).add(artist); } for (Iterator<String> it = map.keySet().iterator(); it.hasNext();) { if (map.get(it.next()).isEmpty()) { it.remove(); } } return map; } }
ArtistIndexService { protected Map<String, List<Artist>> createArtistIndex(Map<Character, String> letterIndex, List<Artist> artists) { Map<String, List<Artist>> map = new LinkedHashMap<>(); for (String index : letterIndex.values()) { map.put(index, new ArrayList<Artist>()); } for (Artist artist : artists) { char firstLetter = toUpperCase(artist.getSortName().charAt(0)); if (!letterIndex.containsKey(firstLetter)) { firstLetter = HASH_CHAR; } map.get(letterIndex.get(firstLetter)).add(artist); } for (Iterator<String> it = map.keySet().iterator(); it.hasNext();) { if (map.get(it.next()).isEmpty()) { it.remove(); } } return map; }  }
ArtistIndexService { protected Map<String, List<Artist>> createArtistIndex(Map<Character, String> letterIndex, List<Artist> artists) { Map<String, List<Artist>> map = new LinkedHashMap<>(); for (String index : letterIndex.values()) { map.put(index, new ArrayList<Artist>()); } for (Artist artist : artists) { char firstLetter = toUpperCase(artist.getSortName().charAt(0)); if (!letterIndex.containsKey(firstLetter)) { firstLetter = HASH_CHAR; } map.get(letterIndex.get(firstLetter)).add(artist); } for (Iterator<String> it = map.keySet().iterator(); it.hasNext();) { if (map.get(it.next()).isEmpty()) { it.remove(); } } return map; }  Map<String, List<Artist>> getIndexedArtists(List<Artist> artists); SortedMap<String, List<Artist>> getIndexedArtists(List<Integer> indexes, String indexLetter, List<Artist> artists); void setSettingsService(SettingsService settingsService); }
ArtistIndexService { protected Map<String, List<Artist>> createArtistIndex(Map<Character, String> letterIndex, List<Artist> artists) { Map<String, List<Artist>> map = new LinkedHashMap<>(); for (String index : letterIndex.values()) { map.put(index, new ArrayList<Artist>()); } for (Artist artist : artists) { char firstLetter = toUpperCase(artist.getSortName().charAt(0)); if (!letterIndex.containsKey(firstLetter)) { firstLetter = HASH_CHAR; } map.get(letterIndex.get(firstLetter)).add(artist); } for (Iterator<String> it = map.keySet().iterator(); it.hasNext();) { if (map.get(it.next()).isEmpty()) { it.remove(); } } return map; }  Map<String, List<Artist>> getIndexedArtists(List<Artist> artists); SortedMap<String, List<Artist>> getIndexedArtists(List<Integer> indexes, String indexLetter, List<Artist> artists); void setSettingsService(SettingsService settingsService);  }