PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/atlassian/jconnect/jira/customfields/LocationParser.java

https://bitbucket.org/atlassian/jiraconnect-jiraplugin/
Java | 100 lines | 84 code | 11 blank | 5 comment | 14 complexity | a3ea330108c3cf8d9e17b72e8a1c5eb9 MD5 | raw file
  1. package com.atlassian.jconnect.jira.customfields;
  2. import com.atlassian.jira.util.I18nHelper;
  3. import com.atlassian.jira.util.MessageSet;
  4. import com.atlassian.jira.util.MessageSetImpl;
  5. import com.google.common.collect.ImmutableMap;
  6. import java.util.Map;
  7. import static java.text.MessageFormat.format;
  8. /**
  9. * Parses location string values.
  10. *
  11. */
  12. public final class LocationParser {
  13. private static final Map<String,String> DEFAULT_MESSAGES = ImmutableMap.<String,String>builder()
  14. .put("customfields.locationsearcher.error.nothreefields", "Raw value '{0}' should contain 3 fields separated by comma")
  15. .put("customfields.location.lat", "Latitude")
  16. .put("customfields.location.lng", "Longitude")
  17. .put("customfields.locationsearcher.error.parselnglat", "Unable to parse value {0} of field {1}")
  18. .put("customfields.locationsearcher.error.latlngnotinrange", "{0} value {1} is not in expected range {2}")
  19. .put("customfields.locationsearcher.error.parsedistance", "Unable to parse distance value {0}")
  20. .put("customfields.locationsearcher.error.distancenegative", "Distance value {0} must not be negative")
  21. .build();
  22. private LocationParser() {
  23. throw new AssertionError("Don't instantiate me");
  24. }
  25. public static LocationQuery validateAndParseLocationQuery(String rawValue, I18nHelper i18n, MessageSet errors) {
  26. final String[] split = rawValue.split(",");
  27. if (split.length != 3) {
  28. errors.addErrorMessage(i18nizeOrNot(i18n, "customfields.locationsearcher.error.nothreefields", rawValue));
  29. return null;
  30. }
  31. final String latName = i18nizeOrNot(i18n, "customfields.location.lat");
  32. final double lat = checkLatLng(i18n, errors, split[0].trim(), latName);
  33. if (!GeoCalculator.LAT_RANGE.containsDouble(lat)) {
  34. i18nizeOrNot(i18n, "customfields.locationsearcher.error.latlngnotinrange", latName, lat, GeoCalculator.LAT_RANGE);
  35. }
  36. final String lngName = i18nizeOrNot(i18n, "customfields.location.lng");
  37. final double lng = checkLatLng(i18n, errors, split[1].trim(), lngName);
  38. if (!GeoCalculator.LNG_RANGE.containsDouble(lng)) {
  39. i18nizeOrNot(i18n, "customfields.locationsearcher.error.latlngnotinrange", lngName, lng, GeoCalculator.LNG_RANGE);
  40. }
  41. final long distance = checkDistance(i18n, errors, split[2].trim());
  42. if (errors.hasAnyErrors()) {
  43. return null;
  44. }
  45. else return new LocationQuery(lat, lng, distance);
  46. }
  47. public static LocationQuery parseLocationQuery(String rawValue) {
  48. MessageSet errors = new MessageSetImpl();
  49. LocationQuery result = validateAndParseLocationQuery(rawValue, null, errors);
  50. if (errors.hasAnyErrors()) {
  51. throw new IllegalArgumentException("Unable to parse " + rawValue + ":\n" + errors.getErrorMessages());
  52. }
  53. return result;
  54. }
  55. private static double checkLatLng(I18nHelper i18n, MessageSet errors, String value, String name) {
  56. try {
  57. return Double.parseDouble(value);
  58. } catch(NumberFormatException nfe) {
  59. errors.addErrorMessage(i18nizeOrNot(i18n, "customfields.locationsearcher.error.parselnglat", value, name));
  60. }
  61. return -1;
  62. }
  63. private static long checkDistance(I18nHelper i18n, MessageSet errors, String value) {
  64. try {
  65. // TODO this has to be smarter if we allow users to define units
  66. final long distance = Long.parseLong(value);
  67. if (distance < 0) {
  68. errors.addErrorMessage(i18nizeOrNot(i18n, "customfields.locationsearcher.error.distancenegative", value));
  69. return -1;
  70. }
  71. return distance;
  72. } catch(NumberFormatException nfe) {
  73. errors.addErrorMessage(i18nizeOrNot(i18n, "customfields.locationsearcher.error.parsedistance", value));
  74. }
  75. return -1;
  76. }
  77. private static String i18nizeOrNot(I18nHelper i18n, String key, Object... params) {
  78. if (i18n != null) {
  79. return i18n.getText(key, params);
  80. } else {
  81. String rawMsg = DEFAULT_MESSAGES.get(key);
  82. if (rawMsg == null) {
  83. return "*** NO MESSAGE FOR <" + key + ">";
  84. } else {
  85. return format(rawMsg, params);
  86. }
  87. }
  88. }
  89. }