@Test public void testValueOf() { { FileSize fs = FileSize.valueOf("8"); assertEquals(8, fs.getSize()); } { FileSize fs = FileSize.valueOf("8 kbs"); assertEquals(8 * KB_CO, fs.getSize()); } { FileSize fs = FileSize.valueOf("8 kb"); assertEquals(8 * KB_CO, fs.getSize()); } { FileSize fs = FileSize.valueOf("12 mb"); assertEquals(12 * MB_CO, fs.getSize()); } { FileSize fs = FileSize.valueOf("5 GBs"); assertEquals(5 * GB_CO, fs.getSize()); } }
static public FileSize valueOf(String fileSizeStr) { Matcher matcher = FILE_SIZE_PATTERN.matcher(fileSizeStr); long coefficient; if (matcher.matches()) { String lenStr = matcher.group(DOUBLE_GROUP); String unitStr = matcher.group(UNIT_GROUP); long lenValue = Long.valueOf(lenStr); if (unitStr.equalsIgnoreCase("")) { coefficient = 1; } else if (unitStr.equalsIgnoreCase("kb")) { coefficient = KB_COEFFICIENT; } else if (unitStr.equalsIgnoreCase("mb")) { coefficient = MB_COEFFICIENT; } else if (unitStr.equalsIgnoreCase("gb")) { coefficient = GB_COEFFICIENT; } else { throw new IllegalStateException("Unexpected " + unitStr); } return new FileSize(lenValue * coefficient); } else { throw new IllegalArgumentException("String value [" + fileSizeStr + "] is not in the expected format."); } }
FileSize { static public FileSize valueOf(String fileSizeStr) { Matcher matcher = FILE_SIZE_PATTERN.matcher(fileSizeStr); long coefficient; if (matcher.matches()) { String lenStr = matcher.group(DOUBLE_GROUP); String unitStr = matcher.group(UNIT_GROUP); long lenValue = Long.valueOf(lenStr); if (unitStr.equalsIgnoreCase("")) { coefficient = 1; } else if (unitStr.equalsIgnoreCase("kb")) { coefficient = KB_COEFFICIENT; } else if (unitStr.equalsIgnoreCase("mb")) { coefficient = MB_COEFFICIENT; } else if (unitStr.equalsIgnoreCase("gb")) { coefficient = GB_COEFFICIENT; } else { throw new IllegalStateException("Unexpected " + unitStr); } return new FileSize(lenValue * coefficient); } else { throw new IllegalArgumentException("String value [" + fileSizeStr + "] is not in the expected format."); } } }
FileSize { static public FileSize valueOf(String fileSizeStr) { Matcher matcher = FILE_SIZE_PATTERN.matcher(fileSizeStr); long coefficient; if (matcher.matches()) { String lenStr = matcher.group(DOUBLE_GROUP); String unitStr = matcher.group(UNIT_GROUP); long lenValue = Long.valueOf(lenStr); if (unitStr.equalsIgnoreCase("")) { coefficient = 1; } else if (unitStr.equalsIgnoreCase("kb")) { coefficient = KB_COEFFICIENT; } else if (unitStr.equalsIgnoreCase("mb")) { coefficient = MB_COEFFICIENT; } else if (unitStr.equalsIgnoreCase("gb")) { coefficient = GB_COEFFICIENT; } else { throw new IllegalStateException("Unexpected " + unitStr); } return new FileSize(lenValue * coefficient); } else { throw new IllegalArgumentException("String value [" + fileSizeStr + "] is not in the expected format."); } } FileSize(long size); }
FileSize { static public FileSize valueOf(String fileSizeStr) { Matcher matcher = FILE_SIZE_PATTERN.matcher(fileSizeStr); long coefficient; if (matcher.matches()) { String lenStr = matcher.group(DOUBLE_GROUP); String unitStr = matcher.group(UNIT_GROUP); long lenValue = Long.valueOf(lenStr); if (unitStr.equalsIgnoreCase("")) { coefficient = 1; } else if (unitStr.equalsIgnoreCase("kb")) { coefficient = KB_COEFFICIENT; } else if (unitStr.equalsIgnoreCase("mb")) { coefficient = MB_COEFFICIENT; } else if (unitStr.equalsIgnoreCase("gb")) { coefficient = GB_COEFFICIENT; } else { throw new IllegalStateException("Unexpected " + unitStr); } return new FileSize(lenValue * coefficient); } else { throw new IllegalArgumentException("String value [" + fileSizeStr + "] is not in the expected format."); } } FileSize(long size); long getSize(); static FileSize valueOf(String fileSizeStr); @Override String toString(); }
FileSize { static public FileSize valueOf(String fileSizeStr) { Matcher matcher = FILE_SIZE_PATTERN.matcher(fileSizeStr); long coefficient; if (matcher.matches()) { String lenStr = matcher.group(DOUBLE_GROUP); String unitStr = matcher.group(UNIT_GROUP); long lenValue = Long.valueOf(lenStr); if (unitStr.equalsIgnoreCase("")) { coefficient = 1; } else if (unitStr.equalsIgnoreCase("kb")) { coefficient = KB_COEFFICIENT; } else if (unitStr.equalsIgnoreCase("mb")) { coefficient = MB_COEFFICIENT; } else if (unitStr.equalsIgnoreCase("gb")) { coefficient = GB_COEFFICIENT; } else { throw new IllegalStateException("Unexpected " + unitStr); } return new FileSize(lenValue * coefficient); } else { throw new IllegalArgumentException("String value [" + fileSizeStr + "] is not in the expected format."); } } FileSize(long size); long getSize(); static FileSize valueOf(String fileSizeStr); @Override String toString(); static final long KB_COEFFICIENT; static final long MB_COEFFICIENT; static final long GB_COEFFICIENT; }