Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static Version tryParse(final String string)
- {
- if (nonNull(string))
- {
- final int[] versionItems = new int[segmentsCount];
- final String[] split = string.split("\\.");
- // Rescue 'x.y.z.w...' - like strings.
- if (split.length > versionItems.length)
- {
- return null;
- }
- int i = 0;
- for (final String s : split)
- {
- // String.trim() simply removes leading and trailing spaces.
- final String trimmed = s.trim();
- try
- {
- if (trimmed.isEmpty())
- {
- versionItems[i++] = 0;
- }
- else
- {
- versionItems[i++] = parseInt(trimmed);
- }
- }
- catch (NumberFormatException e)
- {
- // It is safe to return null from tryParse
- return null;
- }
- }
- return new Version(versionItems[0], versionItems[1], versionItems[2], versionItems[3]);
- }
- // It is safe to return null from tryParse
- return null;
- }
Add Comment
Please, Sign In to add comment