Record Class Durata

java.lang.Object
java.lang.Record
it.unimi.di.prog2.temisvolti.playfy.Durata

public record Durata(int secondi) extends Record
Record (immutabile) che rappresenta una durata (non negativa).

Le istanze possono essere costruite a partire dalla durata espressa in secondi, oppure tramite un metodo di fabbricazione che accetta stringhe del formato HH:MM:SS.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final int
    The field for the secondi record component.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Durata(int secondi)
    Costruisce una durata.
  • Method Summary

    Modifier and Type
    Method
    Description
    final boolean
    Indicates whether some other object is "equal to" this one.
    final int
    Returns a hash code value for this object.
    int
    Returns the value of the secondi record component.
    somma(Durata altra)
    Restituisce una nuova durata pari alla somma di questa durata con l'argomento.
    sottrai(Durata altra)
    Restituisce una nuova durata pari alla differenza tra questa durata con l'argomento.
    private static int
    toHMS(String componente, boolean bounded)
    Metodo di utilità che effettua la conversione da una componente della stringa HH:MM:SS al valore numerico corrispondete.
    Returns a string representation of this record class.
    (package private) static Durata
    valueOf(String durata)
    Fabbrica una durata data una stringa del formato HH:MM:SS, MM:SS o SS.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • secondi

      private final int secondi
      The field for the secondi record component.
  • Constructor Details

    • Durata

      public Durata(int secondi)
      Costruisce una durata.
      Parameters:
      secondi - la durata espressa in secondin.
      Throws:
      IllegalArgumentException - se la durata è negativa.
  • Method Details

    • toHMS

      private static int toHMS(String componente, boolean bounded)
      Metodo di utilità che effettua la conversione da una componente della stringa HH:MM:SS al valore numerico corrispondete.

      Questo metodo può essere usato per convertire una delle parti ottenute dividendo la stringa HH:MM:SS ove occorre il carattere :, il parametro bounded consente di specificare se il massimo valore accettabile sia 60 (per le componenti MM e SS) o non se c'è limite (per la componente HH).

      Parameters:
      componente - la componente dell'orario.
      bounded - se il il valore numerico della componente dev'essere minore di 60.
      Returns:
      il valore numerico della componente.
      Throws:
      NullPointerException - se la componente è null.
      IllegalArgumentException - se la componente è vuota, se non può essere converita in un intero, se il suo valore non è compreso tra 0 (compreso) e il bound specificato (escluso).
    • valueOf

      static Durata valueOf(String durata)
      Fabbrica una durata data una stringa del formato HH:MM:SS, MM:SS o SS.
      Parameters:
      durata - la durata nel formato specificato.
      Returns:
      una Durata.
      Throws:
      NullPointerException - se la durata è null.
      IllegalArgumentException - se la stringa è nulla, o non è nel formato specificato.
    • somma

      public Durata somma(Durata altra)
      Restituisce una nuova durata pari alla somma di questa durata con l'argomento.
      Parameters:
      altra - la durata da sommare a questa.
      Returns:
      la somma delle durate.
      Throws:
      NullPointerException - se la durata è null.
    • sottrai

      public Durata sottrai(Durata altra)
      Restituisce una nuova durata pari alla differenza tra questa durata con l'argomento.
      Parameters:
      altra - la durata da sottrarre da questa.
      Returns:
      la differenza delle durate.
      Throws:
      NullPointerException - se la durata è null.
      IllegalArgumentException - se l'altra durata è maggiore di questa.
    • toString

      public String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with '=='.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • secondi

      public int secondi()
      Returns the value of the secondi record component.
      Returns:
      the value of the secondi record component