Java Reference
In-Depth Information
Exercises
getAllMusicians
that returns a
Stream
of the artists performing and, in the case of
groups, any musicians who are members of those groups. For example, if
getMusi-
cians
returns
The Beatles
, then you should return
The Beatles
along with Lennon,
McCartney, and so on.
Example 4-25. An interface denoting the concept of a musical performance
/** A Performance by some musicians - e.g., an Album or Gig. */
public
public interface
interface
Performance
Performance
{
public
public
String
getName
();
public
public
Stream
<
Artist
>
getMusicians
();
}
2. Based on the resolution rules described earlier, can you ever override
equals
or
hashCode
in a
default
method?
of artists. Your assignment is to refactor the
getArtist
method in order to return an
Optional<Artist>
. It contains an element if the index is within range and is an
empty
Optional
otherwise. Remember that you also need to refactor the
getArtistName
method, and it should retain the same behavior.
Example 4-26. The Artists domain class, which represents more than one Artist
public
public class
class
Artists
Artists
{
private
private
List
<
Artist
>
artists
;
public
public
Artists
(
List
<
Artist
>
artists
) {
this
this
.
artists
=
artists
;
}
public
public
Artist
getArtist
(
int
int
index
) {
iif
(
index
<
0
||
index
>=
artists
.
size
()) {
indexException
(
index
);
}
return
return
artists
.
get
(
index
);
}