[TR-339] Unit Utils to avoid expressing Terrier properties with too many zeroes like inverted2direct.processtokens=100000000 Created: 07/Jul/15  Updated: 11/Nov/15  Resolved: 06/Nov/15

Status: Resolved
Project: Terrier Core
Component/s: .utility
Affects Version/s: 4.0
Fix Version/s: 4.1

Type: New Feature Priority: Trivial
Reporter: Nicola Tonellotto Assignee: Craig Macdonald
Resolution: Fixed  
Labels: None

Issue Links:
Related

 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");
}

}


 Comments   
Comment by Craig Macdonald [ 07/Jul/15 ]

Genius!

Comment by Craig Macdonald [ 06/Nov/15 ]

Tagging for 4.1

Comment by Craig Macdonald [ 06/Nov/15 ]

Thanks Nicola, this will be in v4.1

Generated at Fri Dec 15 23:19:13 GMT 2017 using JIRA 7.1.1#71004-sha1:d6b2c0d9b7051e9fb5e4eb8ce177ca56d91d7bd8.