Record Class BuildInfo

java.lang.Object
java.lang.Record
com.dua3.utility.lang.BuildInfo
Record Components:
version - the version
buildTime - the build timestamp
key - the key associated with the build information
commit - the commit information
system - the system information

public record BuildInfo(Version version, ZonedDateTime buildTime, String key, String commit, String system) extends Record
A record class representing build information, including build time and version components.
  • Field Details

    • KEY_BUILD_VERSION

      public static final String KEY_BUILD_VERSION
      Key to use for the build version in properties files.
      See Also:
    • KEY_BUILD_TIME

      public static final String KEY_BUILD_TIME
      Key to use for the build time in properties files.
      See Also:
    • KEY_PUBLIC_KEY

      public static final String KEY_PUBLIC_KEY
      Key to use for the key in properties files.
      See Also:
    • KEY_COMMIT

      public static final String KEY_COMMIT
      Key to use for the commit in properties files.
      See Also:
    • KEY_SYSTEM

      public static final String KEY_SYSTEM
      Key to use for the system in properties files.
      See Also:
  • Constructor Details

    • BuildInfo

      public BuildInfo(Version version, ZonedDateTime buildTime, String key, String commit, String system)
      Canonical record constructor.
      Parameters:
      version - the version
      buildTime - the build timestamp
      key - the key associated with the build information
      commit - the commit information
      system - the system information
  • Method Details

    • create

      public static BuildInfo create(String versionString, String key, String commit)
      Creates a new BuildInfo instance using the given version string, key, and commit. The current timestamp and system information are automatically generated.

      Note: This method is intended to be called from the build script.

      Parameters:
      versionString - the version string to be parsed into a Version object. The format should be "major.minor.patch" or "major.minor.patch-suffix".
      key - the key associated with the build information.
      commit - the commit information associated with the build.
      Returns:
      a new BuildInfo instance containing the parsed version, current timestamp, the provided key, the provided commit, and the current system information.
      Throws:
      IllegalArgumentException - if the version string is invalid.
    • create

      public static BuildInfo create(String versionString, ZonedDateTime buildTime, String key, String commit, String system)
      Creates a new instance of BuildInfo using the provided version string, build timestamp, key, commit, and system.
      Parameters:
      versionString - the version string to parse into a Version object, in the format "major.minor.patch" or "major.minor.patch-suffix"
      buildTime - the build time and date
      key - the key associated with the build information
      commit - the commit information
      system - the system information
      Returns:
      a new BuildInfo instance containing the build time, version, key, commit, and system
      Throws:
      IllegalArgumentException - if the version string is invalid
    • create

      public static BuildInfo create(String versionString, ZonedDateTime buildTime, String key)
      Creates a new instance of BuildInfo using the provided version string, build timestamp, and key. Uses empty strings for commit and system.
      Parameters:
      versionString - the version string to parse into a Version object, in the format "major.minor.patch" or "major.minor.patch-suffix"
      buildTime - the build timestamp
      key - the key associated with the build information
      Returns:
      a new BuildInfo instance containing the parsed build time, version, and key
      Throws:
      IllegalArgumentException - if the version string is invalid
    • create

      public static BuildInfo create(Properties properties)
      Create an instance from properties.

      The following keys are required:

      • "build.version" - The version number in format major.minor.patch or major.minor.patch-suffix
      • "build.time" - The build timestamp
      • "build.key" - The public key associated with this build
      • "build.commit" - The version control commit identifier
      • "build.system" - The system information where the build was created
      Parameters:
      properties - the properties instance; use keys as described above
      Returns:
      BuildInfo instance
      Throws:
      IllegalStateException - if the BuildInfo instance cannot be created from the supplied properties
    • create

      public static BuildInfo create(Class<?> cls, String resource) throws IOException
      Load properties from a resource and return BuildInfo.
      Parameters:
      cls - the class used to load the properties; typically the class whose package contains the resource
      resource - the name of the resource file to load the build information from
      Returns:
      BuildInfo instance
      Throws:
      MissingResourceException - if the resource can not be found
      IOException - if the BuildInfo could not be loaded due to an I/O error
      IllegalStateException - if the BuildInfo could not be loaded due an invalid resource
    • toString

      public String toString()
      Generates a string representation of the BuildInfo object. The representation includes version and build time in the format "version (buildTime)". Optional fields, such as commit and system, are included if they are not blank.
      Specified by:
      toString in class Record
      Returns:
      a string representing the build information, including the version, build time, and optionally the commit and system details if provided.
    • digest

      public byte[] digest()
      Computes the SHA-256 hash of the concatenated string representation of all fields of this instance.
      Returns:
      a byte array containing the SHA-256 digest of the string representation of this BuildInfo object
      Throws:
      IllegalStateException - if the SHA-256 algorithm is not available
    • writeToPropertiesFile

      public void writeToPropertiesFile(Path destination) throws IOException
      Writes the build information as properties to the specified file destination. Each field of the build information, such as version, build time, key, commit, and system, is written as a key-value pair into the generated properties file.
      Parameters:
      destination - the path where the properties file should be written
      Throws:
      IOException - if an I/O error occurs while writing to the specified destination
    • 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 Objects::equals(Object,Object).
      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.
    • version

      public Version version()
      Returns the value of the version record component.
      Returns:
      the value of the version record component
    • buildTime

      public ZonedDateTime buildTime()
      Returns the value of the buildTime record component.
      Returns:
      the value of the buildTime record component
    • key

      public String key()
      Returns the value of the key record component.
      Returns:
      the value of the key record component
    • commit

      public String commit()
      Returns the value of the commit record component.
      Returns:
      the value of the commit record component
    • system

      public String system()
      Returns the value of the system record component.
      Returns:
      the value of the system record component