ElasticParserUtil { static double convertToMeters(String distanceWithUnit) throws IllegalArgumentException { if (distanceWithUnit == null || distanceWithUnit.isEmpty()) { throw new IllegalArgumentException("Null of zero length distance string argument"); } final Matcher matcher = ELASTIC_DISTANCE_PATTERN.matcher(distanceWithUnit); if (matcher.matches()) { final double distance = Double.valueOf(matcher.group(1)); final String unit = matcher.group(3); Double conversion = FilterToElasticHelper.UNITS_MAP.get(unit); if (conversion == null) { if (unit != null && ! unit.isEmpty()) { throw new IllegalArgumentException("Illegal unit: " + unit); } else { conversion = 1.0; } } return distance * conversion; } else { throw new IllegalArgumentException("Distance string argument has incorrect format"); } } ElasticParserUtil(); @SuppressWarnings("unchecked") Geometry createGeometry(Object obj); @SuppressWarnings({"rawtypes", "unchecked"}) Geometry createGeometry(final Map<String, Object> properties); List<Object> readField(Map<String, Object> source, String name); @SuppressWarnings("rawtypes") static boolean isGeoPointFeature(Map<String, Object> map); static String urlDecode(String value);  }
@Test public void testConvertToMeters() { double distance = ElasticParserUtil.convertToMeters("1.2mm"); assertEquals(0.0012, distance, 0.0000000001); distance = ElasticParserUtil.convertToMeters("1.2"); assertEquals(1.2, distance, 0.0000000001); distance = ElasticParserUtil.convertToMeters("12"); assertEquals(12.0, distance, 0.0000000001); distance = ElasticParserUtil.convertToMeters("0.12cm"); assertEquals(0.0012, distance, 0.0000000001); try { ElasticParserUtil.convertToMeters("999xyz"); fail("Shouldn't get here"); } catch(IllegalArgumentException ignored) { } try { ElasticParserUtil.convertToMeters("mm1.2"); fail("Shouldn't get here"); } catch(IllegalArgumentException ignored) { } try { ElasticParserUtil.convertToMeters(".2"); fail("Shouldn't get here"); } catch(IllegalArgumentException ignored) { } try { ElasticParserUtil.convertToMeters(".2m"); fail("Shouldn't get here"); } catch(IllegalArgumentException ignored) { } }