All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.michaelthelin.spotify.model_objects.specification.SavedAlbum Maven / Gradle / Ivy

There is a newer version: 9.0.0-RC1
Show newest version
package se.michaelthelin.spotify.model_objects.specification;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.google.gson.JsonObject;
import se.michaelthelin.spotify.SpotifyApi;
import se.michaelthelin.spotify.model_objects.AbstractModelObject;

import java.text.ParseException;
import java.util.Date;
import java.util.logging.Level;

/**
 * Retrieve information about 
 * Saved Album objects by building instances from this class.
 */
@JsonDeserialize(builder = SavedAlbum.Builder.class)
public class SavedAlbum extends AbstractModelObject {
  private final Date addedAt;
  private final Album album;

  private SavedAlbum(final Builder builder) {
    super(builder);

    this.addedAt = builder.addedAt;
    this.album = builder.album;
  }

  /**
   * Get the date, when the album has been saved.
   *
   * @return The date and time the album was saved.
   */
  public Date getAddedAt() {
    return addedAt;
  }

  /**
   * Get information about the album from a saved album object.
   *
   * @return Information about the album.
   */
  public Album getAlbum() {
    return album;
  }

  @Override
  public String toString() {
    return "SavedAlbum(addedAt=" + addedAt + ", album=" + album + ")";
  }

  @Override
  public Builder builder() {
    return new Builder();
  }

  /**
   * Builder class for building {@link SavedAlbum} instances.
   */
  public static final class Builder extends AbstractModelObject.Builder {
    private Date addedAt;
    private Album album;

    /**
     * Set the "added at" date of the saved album to be built.
     *
     * @param addedAt The date and time the album was saved.
     * @return A {@link SavedAlbum.Builder}.
     */
    public Builder setAddedAt(Date addedAt) {
      this.addedAt = addedAt;
      return this;
    }

    /**
     * Set the full album object of the saved album to be built.
     *
     * @param album Information about the album.
     * @return A {@link SavedAlbum.Builder}.
     */
    public Builder setAlbum(Album album) {
      this.album = album;
      return this;
    }

    @Override
    public SavedAlbum build() {
      return new SavedAlbum(this);
    }
  }

  /**
   * JsonUtil class for building {@link SavedAlbum} instances.
   */
  public static final class JsonUtil extends AbstractModelObject.JsonUtil {
    public SavedAlbum createModelObject(JsonObject jsonObject) {
      if (jsonObject == null || jsonObject.isJsonNull()) {
        return null;
      }

      try {
        return new Builder()
          .setAddedAt(
            hasAndNotNull(jsonObject, "added_at")
              ? SpotifyApi.parseDefaultDate(jsonObject.get("added_at").getAsString())
              : null)
          .setAlbum(
            hasAndNotNull(jsonObject, "album")
              ? new Album.JsonUtil().createModelObject(
              jsonObject.getAsJsonObject("album"))
              : null)
          .build();
      } catch (ParseException e) {
        SpotifyApi.LOGGER.log(Level.SEVERE, e.getMessage());
        return null;
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy