5.14 Record Classes

Exchanging plain data between programs is a common task. Plain data objects allow efficient aggregation of data and provide transparent access to it. In the literature, such data objects are referred to by different names; the acronym POJOs (Plain Old Java Objects) is common in the Java community.

A plain data class can always be implemented as a normal class, but this is tedious and repetitive, entailing boilerplate code and full weight of the OOP programming model. In Example 5.27, the class CD_v1 represents the data for a CD. A full-fledged implementation of this data class requires declaration of its instance fields that constitute the data, an appropriate constructor to initialize the data fields, get methods to allow access to the data, and overriding the pertinent methods from the Object class to make CD objects more meaningful and useful. (For details on overriding methods from the Object class, see Chapter 14, p. 741). Although most IDEs can generate the code necessary to implement such data objects, they lack the incremental validation required when changes are made to the data model of the normal class.

Example 5.27 The CD Class

Click here to view code image

package record.basics;
// The different genres in music.
public enum Genre {POP, JAZZ, OTHER}

Click here to view code image

package record.basics;
import java.time.Year;
import java.util.Objects;
/** A class that represents a CD. */
public class CD_v1 {
  // Instance fields:
  private final String artist;      // Name of the artist.
  private final String title;       // Title of the CD.
  private final int    noOfTracks;  // Number of tracks on the CD.
  private final Year   year;        // Year the CD was released.
  private final Genre  genre;       // Music genre of the CD.
  // Non-zero argument constructor:
  public CD_v1(String artist, String title, int noOfTracks,
               Year year, Genre genre) {
    this.artist     = artist;
    this.title      = title;
    this.noOfTracks = noOfTracks;
    this.year       = year;
    this.genre      = genre;
  }
  // Get methods:
  public String getArtist()     { return this.artist; }
  public String getTitle()      { return this.title; }
  public int    getNoOfTracks() { return this.noOfTracks; }
  public Year   getYear()       { return this.year; }
  public Genre  getGenre()      { return this.genre; }
  // Overridden methods from the Object class:
  @Override public String toString() {
    return String.format(“<%s, \”%s\”, %d, %s, %s>”,
        this.artist, this.title, this.noOfTracks, this.year, this.genre);
  }
  @Override public boolean equals(Object obj) {
    return (this == obj)
        || (obj instanceof CD_v1 other
            && this.artist.equals(other.artist)
            && this.title.equals(other.title)
            && this.noOfTracks == other.noOfTracks
            && this.year == other.year
            && this.genre == other.genre);
  }
  @Override public int hashCode() {
    return Objects.hash(this.artist, this.title, this.noOfTracks,
                        this.year, this.genre);
  }
}


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *