Package com.castlabs.utils

# Class StringUtils

java.lang.Object
com.castlabs.utils.StringUtils

public class StringUtils extends Object
String utilities and formats

Since:
  2.0.0

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static String`
  `contentTypeToString(int contentType)`
  Helper method to convert content type to human-readable string for logging.
  `static String`
  `durationStrToMinString(String durationStr)`
  Convert a string formatted as "PnYnMnDTnHnMnS" (as defined by XML Schema Part 2: Datatypes, 3.2.6 duration) into a string formatted as "M" (ie.
  `static String`
  `durationStrToTimeString(String contentRunLength)`
  Convert a string formatted as "PnYnMnDTnHnMnS" (as defined by XML Schema Part 2: Datatypes, 3.2.6 duration) into a string formatted as"H+:MM:SS"
  `static String`
  `format(Locale locale, String format, Object... args)`
  Tries safely to format the string using the provided locale and format.
  `static String`
  `join(Collection<String> tokens, String delimiter)`
  Join a list of token using teh given delimiter
  `static String`
  `stringForBitrate(int bitrate)`
  Formats a given bitrate as bps, Kbps, or Mbps.
  `static String`
  `stringForBitrate(long bitrate)`
  Formats a given bitrate as bps, Kbps, or Mbps.
  `static String`
  `stringForComputerSize(long sizeInBytes)`
  Convert the size in bytes into a string typed as "x.xx bytes", "x.xx KB" or "x.xx MB" or "x.xx GB"
  `static String`
  `stringForTime(long totalSeconds)`
  Convert the time in seconds into a string type as "hh:mm:ss" or "mm:ss" depending on the duration being >= 1h or not
  `static String`
  `stringForTime(long totalSeconds, boolean forceHoursDisplay)`
  Convert the time in seconds into a string type as "hh:mm:ss" or "mm:ss" depending on the duration being >= 1h or not or the hour format is forced.
  `static String`
  `stringForTime(long time, TimeUnit unit)`
  Convert the time in seconds into a string type as "hh:mm:ss" or "mm:ss" depending on the duration being >= 1h or not
  `static String`
  `stringForTimeMs(long timeMs, boolean forceHoursDisplay, boolean showMillis)`
  Convert the time in milliseconds into a string type as "hh:mm:ss" or "mm:ss" depending on the duration being >= 1h or not or the hour format is forced.
  `static String`
  `valueOrDefault(String value, String fallback)`
  Returns the provided value or falls back to the supplied default when the value is null.

  ### Methods inherited from class java.lang.Object

  `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### stringForTime

    public static String stringForTime(long time, TimeUnit unit)
    Convert the time in seconds into a string type as "hh:mm:ss" or "mm:ss" depending on the duration being >= 1h or not
    Parameters:
      `time` - durations in seconds
      `unit` - The time unit
    Returns:
      formatted time string in the form "mm:ss: or "hh:mm:ss"
    See Also:
      - `stringForTime(long, boolean)`

  - ### stringForTime

    public static String stringForTime(long totalSeconds)
    Convert the time in seconds into a string type as "hh:mm:ss" or "mm:ss" depending on the duration being >= 1h or not
    Parameters:
      `totalSeconds` - durations in seconds
    Returns:
      formatted time string in the form "mm:ss: or "hh:mm:ss"
    See Also:
      - `stringForTime(long, boolean)`

  - ### stringForTime

    public static String stringForTime(long totalSeconds, boolean forceHoursDisplay)
    Convert the time in seconds into a string type as "hh:mm:ss" or "mm:ss" depending on the duration being >= 1h or not or the hour format is forced. If seconds is negative, the formatted string will be prefixed with "-".
    Parameters:
      `totalSeconds` - duration in seconds
      `forceHoursDisplay` - force "hh:mm:ss" display
    Returns:
      formatted time string in the form "mm:ss: or "hh:mm:ss"

  - ### stringForTimeMs

    public static String stringForTimeMs(long timeMs, boolean forceHoursDisplay, boolean showMillis)
    Convert the time in milliseconds into a string type as "hh:mm:ss" or "mm:ss" depending on the duration being >= 1h or not or the hour format is forced. Milliseconds can also be added to the output. If seconds is negative, the formatted string will be prefixed with "-".
    Parameters:
      `timeMs` - duration in milliseconds
      `forceHoursDisplay` - force "hh:mm:ss" display
      `showMillis` - render milliseconds as "hh:mm:ss.MMM" display
    Returns:
      formatted time string in the form "mm:ss or "hh:mm:ss"

  - ### stringForComputerSize

    public static String stringForComputerSize(long sizeInBytes)
    Convert the size in bytes into a string typed as "x.xx bytes", "x.xx KB" or "x.xx MB" or "x.xx GB"
    Parameters:
      `sizeInBytes` - the size in bytes
    Returns:
      foramated size

  - ### stringForBitrate

    public static String stringForBitrate(int bitrate)
    Formats a given bitrate as bps, Kbps, or Mbps.
    Parameters:
      `bitrate` - The bitrate in bits per second
    Returns:
      The formatted string representation

  - ### stringForBitrate

    public static String stringForBitrate(long bitrate)
    Formats a given bitrate as bps, Kbps, or Mbps.
    Parameters:
      `bitrate` - The bitrate in bits per second
    Returns:
      The formatted string representation

  - ### join

    public static String join(Collection<String> tokens, String delimiter)
    Join a list of token using teh given delimiter
    Parameters:
      `tokens` - the list of tokens
      `delimiter` - the delimiter
    Returns:
      joined string

  - ### valueOrDefault

    @NonNull public static String valueOrDefault(@Nullable String value, @NonNull String fallback)
    Returns the provided value or falls back to the supplied default when the value is null.
    Parameters:
      `value` - the value, may be `null`
      `fallback` - the fallback to use when `value` is `null`
    Returns:
      the value or the default never `null`

  - ### durationStrToTimeString

    public static String durationStrToTimeString(String contentRunLength) throws Exception
    Convert a string formatted as "PnYnMnDTnHnMnS" (as defined by XML Schema Part 2: Datatypes, 3.2.6 duration) into a string formatted as"H+:MM:SS"
    Parameters:
      `contentRunLength` - the duration string
    Returns:
      time string formatted as"H+:MM:SS"
    Throws:
      `Exception` - in case of an error during conversion

  - ### durationStrToMinString

    public static String durationStrToMinString(String durationStr) throws Exception
    Convert a string formatted as "PnYnMnDTnHnMnS" (as defined by XML Schema Part 2: Datatypes, 3.2.6 duration) into a string formatted as "M" (ie. only minutes)
    Parameters:
      `durationStr` - duration string
    Returns:
      formatted string
    Throws:
      `Exception` - in case of an error during conversion

  - ### format

    @Nullable public static String format(Locale locale, String format, Object... args)
    Tries safely to format the string using the provided locale and format. When exception occurs then the default locale is used instead and in case of error the `null` is returned finally.
    Parameters:
      `locale` - The locale to be used in the first place
      `format` - The format
      `args` - The arguments to format
    Returns:
      Formatted `String` or `null` if error occurs

  - ### contentTypeToString

    @NonNull public static String contentTypeToString(int contentType)
    Helper method to convert content type to human-readable string for logging.
    Parameters:
      `contentType` - Content type value
    Returns:
      content type description