Uploaded image for project: 'Terrier Core'
  1. Terrier Core
  2. TR-339

Unit Utils to avoid expressing Terrier properties with too many zeroes like inverted2direct.processtokens=100000000

    Details

    • Type: New Feature
    • Status: Resolved
    • Priority: Trivial
    • Resolution: Fixed
    • Affects Version/s: 4.0
    • Fix Version/s: 4.1
    • Component/s: .utility
    • Labels:
      None

      Description

      package org.terrier.utility;

      import org.apache.commons.lang.StringUtils;

      public class UnitUtils
      {
      private final static long Ki_FACTOR = 1024;
      private final static long Mi_FACTOR = 1024 * Ki_FACTOR;
      private final static long Gi_FACTOR = 1024 * Mi_FACTOR;

      private final static long K_FACTOR = 1000;
      private final static long M_FACTOR = 1000 * K_FACTOR;
      private final static long G_FACTOR = 1000 * M_FACTOR;

      public static long parseLong(String str)
      {
      if (str == null)
      throw new NullPointerException();
      int notNumberIndex = StringUtils.indexOfAnyBut(str, "0123456789");
      long ret = Long.parseLong(str.substring(0, notNumberIndex));
      if (notNumberIndex == -1)
      return ret;
      switch (str.substring(notNumberIndex).trim()) {
      case "G":
      return ret * G_FACTOR;
      case "M":
      return ret * M_FACTOR;
      case "K":
      return ret * K_FACTOR;
      case "Gi":
      return ret * Gi_FACTOR;
      case "Mi":
      return ret * Mi_FACTOR;
      case "Ki":
      return ret * Ki_FACTOR;
      }
      throw new NumberFormatException(str + " can't be correctly parsed.");
      }

      public static int parseInt(String str)
      {
      if (str == null)
      throw new NullPointerException();
      int notNumberIndex = StringUtils.indexOfAnyBut(str, "0123456789");
      int ret = Integer.parseInt(str.substring(0, notNumberIndex));
      if (notNumberIndex == -1)
      return ret;
      switch (str.substring(notNumberIndex).trim()) {
      case "G":
      return (int) (ret * G_FACTOR);
      case "M":
      return (int) (ret * M_FACTOR);
      case "K":
      return (int) (ret * K_FACTOR);
      case "Gi":
      return (int) (ret * Gi_FACTOR);
      case "Mi":
      return (int) (ret * Mi_FACTOR);
      case "Ki":
      return (int) (ret * Ki_FACTOR);
      }
      throw new NumberFormatException(str + " can't be correctly parsed.");
      }

      public static float parseFloat(String str)
      {
      if (str == null)
      throw new NullPointerException();
      int notNumberIndex = StringUtils.indexOfAnyBut(str, "0123456789");
      float ret = Float.parseFloat(str.substring(0, notNumberIndex));
      if (notNumberIndex == -1)
      return ret;
      switch (str.substring(notNumberIndex).trim()) {
      case "G":
      return ret * G_FACTOR;
      case "M":
      return ret * M_FACTOR;
      case "K":
      return ret * K_FACTOR;
      case "Gi":
      return ret * Gi_FACTOR;
      case "Mi":
      return ret * Mi_FACTOR;
      case "Ki":
      return ret * Ki_FACTOR;
      }
      throw new NumberFormatException(str + " can't be correctly parsed.");
      }

      public static double parseDouble(String str)
      {
      if (str == null)
      throw new NullPointerException();
      int notNumberIndex = StringUtils.indexOfAnyBut(str, "0123456789");
      double ret = Double.parseDouble(str.substring(0, notNumberIndex));
      if (notNumberIndex == -1)
      return ret;
      switch (str.substring(notNumberIndex).trim()) {
      case "G":
      return ret * G_FACTOR;
      case "M":
      return ret * M_FACTOR;
      case "K":
      return ret * K_FACTOR;
      case "Gi":
      return ret * Gi_FACTOR;
      case "Mi":
      return ret * Mi_FACTOR;
      case "Ki":
      return ret * Ki_FACTOR;
      }
      throw new NumberFormatException(str + " can't be correctly parsed.");
      }

      public static void main(String args[])
      {
      UnitUtils.parseLong("1 K");
      UnitUtils.parseLong("1M");
      }

      }

        Attachments

          Activity

            People

            • Assignee:
              craigm Craig Macdonald
              Reporter:
              nicola.tonellotto Nicola Tonellotto
            • Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: