Quantities { public static int parseCpuAsMilliCpus(String cpu) { int suffixIndex = cpu.length(); int factor = 1000; for (int i = 0; i < cpu.length(); i++) { char ch = cpu.charAt(i); if (ch < '0' || '9' < ch) { suffixIndex = i; if ("m".equals(cpu.substring(i))) { factor = 1; break; } else if (cpu.substring(i).startsWith(".")) { return (int) (Double.parseDouble(cpu) * 1000L); } else { throw new IllegalArgumentException("Failed to parse CPU quantity \"" + cpu + "\""); } } } return factor * Integer.parseInt(cpu.substring(0, suffixIndex)); } private  Quantities(); static long parseMemory(String memory); static String formatMemory(long bytes); static String normalizeMemory(String memory); static int parseCpuAsMilliCpus(String cpu); static String formatMilliCpu(int milliCpu); static String normalizeCpu(String milliCpu);  }
@Test public void testParse() { assertThat(parseCpuAsMilliCpus("100000"), is(100000000)); assertThat(parseCpuAsMilliCpus("1"), is(1000)); assertThat(parseCpuAsMilliCpus("1m"), is(1)); assertThat(parseCpuAsMilliCpus("0.5"), is(500)); assertThat(parseCpuAsMilliCpus("0"), is(0)); assertThat(parseCpuAsMilliCpus("0m"), is(0)); assertThat(parseCpuAsMilliCpus("0.0"), is(0)); assertThat(parseCpuAsMilliCpus("0.000001"), is(0)); try { parseCpuAsMilliCpus("0.0m"); fail(); } catch (NumberFormatException e) { } try { parseCpuAsMilliCpus("0.1m"); fail(); } catch (NumberFormatException e) { } }