Utils.java

  1. /*
  2.  * Copyright (C) 2021 B3Partners B.V.
  3.  *
  4.  * SPDX-License-Identifier: MIT
  5.  */

  6. package nl.b3p.brmo.bgt.loader;

  7. import java.text.MessageFormat;
  8. import java.time.Duration;
  9. import java.time.Instant;
  10. import java.util.ResourceBundle;

  11. public class Utils {

  12.   public static String formatTimeSince(Instant start) {
  13.     return formatDuration(Duration.between(start, Instant.now()));
  14.   }

  15.   public static String formatDuration(Duration d) {
  16.     String days = d.toDaysPart() > 0 ? d.toDaysPart() + "d " : "";
  17.     if (d.toHoursPart() == 0 && d.toMinutesPart() == 0) {
  18.       return days + d.toSecondsPart() + "s";
  19.     } else if (d.toHoursPart() == 0) {
  20.       return String.format("%s%dm %2ds", days, d.toMinutesPart(), d.toSecondsPart());
  21.     }
  22.     return String.format(
  23.         "%s%dh %2dm %2ds", days, d.toHoursPart(), d.toMinutesPart(), d.toSecondsPart());
  24.   }

  25.   public static final String BUNDLE_NAME = "BGTLoader";

  26.   private static final ResourceBundle bundle = ResourceBundle.getBundle(BUNDLE_NAME);

  27.   public static ResourceBundle getBundle() {
  28.     return bundle;
  29.   }

  30.   public static String getBundleString(String key) {
  31.     return bundle.getString(key);
  32.   }

  33.   public static String getMessageFormattedString(String key, Object... args) {
  34.     ResourceBundle bundle = getBundle();
  35.     return new MessageFormat(bundle.getString(key), bundle.getLocale()).format(args);
  36.   }

  37.   public static String getLoaderVersion() {
  38.     return getBundleString("app.version");
  39.   }

  40.   public static String getBrmoVersion() {
  41.     return getBundleString("brmo.version");
  42.   }

  43.   public static String getUserAgent() {
  44.     return String.format(
  45.         "%s, %s (%s)/%s, %s/%s",
  46.         getBundleString("app.user-agent"),
  47.         System.getProperty("os.name"),
  48.         System.getProperty("os.arch"),
  49.         System.getProperty("os.version"),
  50.         System.getProperty("java.vm.name"),
  51.         System.getProperty("java.vm.version"));
  52.   }
  53. }