vuoi
o PayPal
tutte le volte che vuoi
Optional<String> longestSong();
/**
* @return the longest album
*/
Optional<String> longestAlbum();
}
/***********************************************************************************/
/**
* Classe da salvare in un file MusicGroupImpl.java
*/
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.Set;
import java.util.stream.Stream;
import static java.util.stream.Collectors.*;
public class MusicGroupImpl implements MusicGroup {
private final Map<String, Integer> albums = new HashMap<>();
private final Set<Song> songs = new HashSet<>();
@Override
public void addAlbum(final String albumName, final int year) {
this.albums.put(albumName, year);
}
@Override
public void addSong(final String songName,
final Optional<String> albumName, final double duration) {
if (albumName.isPresent() && !this.albums.containsKey(albumName.get())) {
throw new IllegalArgumentException("invalid album name");
}
this.songs.add(new Song(songName, albumName, duration));
}
@Override
public Stream<String> orderedSongNames() {
return this.songs.stream().map(s -> s.getSongName()).sorted();
}
@Override
public Stream<String> albumNames() {
return this.albums.entrySet().stream().map(a -> a.getKey());
}
@Override
public Stream<String> albumInYear(final int year) {
return this.albums.entrySet().stream()
.filter(entry -> entry.getValue() == year)
.map(entry -> entry.getKey());
}
@Override
public int countSongs(final String albumName) {
return (int) this.songs
.stream()
.filter(song -> song.albumName.filter(
album -> album.equals(albumName)).isPresent()).count();
}
@Override
public int countSongsInNoAlbum() {
return (int) this.songs.stream()
.filter(song -> !song.albumName.isPresent()).count();
}
@Override
public OptionalDouble averageDurationOfSongs(final String albumName) {
return this.songs
.stream()
.filter(song -> song.albumName.filter(
album -> album.equals(albumName)).isPresent())
.mapToDouble(song -> song.duration).average();
}
@Override
/**
*
* @throws NoSuchElementException quando non ci sono canzoni
*/
public Optional<String> longestSong() {
return Optional.of(this.songs
.stream()
.max((s1, s2) -> Double.compare(s1.duration, s2.duration))
.orElseThrow( () -> new NoSuchElementException("There are no songs"))
.getSongName());
}
@Override
public Optional<String> longestAlbum() {
final Map<Optional<String>, Double> songsByAlbum = this.songs.stream()
.collect( groupingBy( Song::getAlbumName,
collectingAndThen(
summingDouble(Song::getDuration), x -> x)));
return songsByAlbum
.entrySet()
.stream()
.max((e1, e2) -> Double.compare(e1.getValue(), e2.getValue()))
.orElseThrow( () -> new NoSuchElementException("There are no albums or
there are no songs in the albums"))
.getKey();
}
private static final class Song {
private final String songName;
private final Optional<String> albumName;
private final double duration;
private int hash;
private Song(final String name, final Optional<String> album,
final double len) {
super();
this.songName = name;
this.albumName = album;
this.duration = len;
}
public String getSongName() {
return songName;
}
public Optional<String> getAlbumName() {
return albumName;
}
public double getDuration() {
return duration;
}
@Override
public int hashCode() {
if (hash == 0) {
hash = songName.hashCode() ^ albumName.hashCode()
^ Double.hashCode(duration);
}
return hash;
}
@Override
public boolean equals(final Object obj) {
if (obj instanceof Song) {
final Song other = (Song) obj;
return albumName.equals(other.albumName)
&& songName.equals(other.songName)
&& duration == other.duration;
}
return false;
}
@Override
public String toString() {
return "Song [songName=" + songName + ", albumName=" + albumName
+ ", duration=" + duration + "]";
}
}
}
/*************************************************************************************/
/* Classe di testing dell’implementazione da salvare in un file TestMusicGroup.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
import static java.util.stream.Collectors.toList;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
/*
* CHECKSTYLE:OFF
*
* This comment shuts down checkstyle: in a test suite, magic numbers are tolerated.
*/
public class TestMusicGroup {
private static final String UNTITLED = "untitled";
private static final String III = "III";
private MusicGroup lz;
@Before
public void setUp() {
lz = new MusicGroupImpl();
lz.addAlbum("I", 1969);
lz.addAlbum("II", 1969);
lz.addAlbum(III, 1970);
lz.addAlbum(UNTITLED, 1971);
lz.addSong("Dazed and Confused", Optional.of("I"), 6.5);
lz.addSong("I Can't Quit You Baby", Optional.of("I"), 4.6);
lz.addSong("Whole Lotta Love", Optional.of("II"), 5.5);
lz.addSong("Ramble On", Optional.of("II"), 4.6);
lz.addSong("Immigrant Song", Optional.of(III), 2.4);
lz.addSong("That's the Way", Optional.of(III), 5.4);
lz.addSong("Black Dog", Optional.of("untitled"), 4.9);
lz.addSong("When the Levee Breaks", Optional.of("untitled"), 7.1);
lz.addSong("Travelling Riverside Blues", Optional.empty(), 5.2);
}
@Test
public void testAlbumNames() {
final List<String> result = new ArrayList<>();
result.add("II");
result.add(UNTITLED);
result.add("III");
result.add("I");
final List<String> actual = lz.albumNames().collect(toList());
assertTrue(actual.containsAll(result));
assertTrue(lz.albumNames().collect(toList()).containsAll(result));
}
@Test
public void testOrderedSongNames() {