diff --git a/mapbox/libjava-services/src/main/java/com/mapbox/services/api/staticimage/v1/models/StaticMarkerAnnotation.java b/mapbox/libjava-services/src/main/java/com/mapbox/services/api/staticimage/v1/models/StaticMarkerAnnotation.java index 34d22f60b..af11db097 100644 --- a/mapbox/libjava-services/src/main/java/com/mapbox/services/api/staticimage/v1/models/StaticMarkerAnnotation.java +++ b/mapbox/libjava-services/src/main/java/com/mapbox/services/api/staticimage/v1/models/StaticMarkerAnnotation.java @@ -44,7 +44,7 @@ public StaticMarkerAnnotation(Builder builder) { TextUtils.formatCoordinate(builder.getLat(), builder.getPrecision()) ); } else { - marker = String.format(Constants.DEFAULT_LOCALE, "%s-%s+%s(%s,%s)", + marker = String.format(Constants.DEFAULT_LOCALE, "%s%s%s(%s,%s)", builder.getName(), builder.getLabel(), builder.getColor(), @@ -58,7 +58,7 @@ public StaticMarkerAnnotation(Builder builder) { marker = String.format(Constants.DEFAULT_LOCALE, "url-%s(%s,%s)", builder.getUrl(), builder.getLon(), builder.getLat()); } else { - marker = String.format(Constants.DEFAULT_LOCALE, "%s-%s+%s(%f,%f)", builder.getName(), + marker = String.format(Constants.DEFAULT_LOCALE, "%s%s%s(%f,%f)", builder.getName(), builder.getLabel(), builder.getColor(), builder.getLon(), builder.getLat()); } } @@ -81,6 +81,11 @@ public String getMarker() { */ public static class Builder { + private static final String EMPTY = ""; + private static final String HYPHEN_CHAR = "-"; + private static final String PLUS_CHAR = "+"; + private static final String NUMERIC_FROM_ZERO_TO_NINETYNINE_REGEX = "^(0|[1-9][0-9]{0,1})$"; + private static final String ONE_ALPHABET_LETTER_REGEX = "^([a-zA-Z])$"; private String name; private String label = ""; private String color = ""; @@ -144,12 +149,17 @@ public Builder setName(String name) { * @since 2.1.0 */ public String getLabel() { - return label; + if (label.isEmpty()) { + return EMPTY; + } + + return HYPHEN_CHAR.concat(label).toLowerCase(); } /** - * StaticMarkerAnnotation symbol. Options are an alphanumeric label {@code a} through {@code z}, {@code 0} through - * {@code 99}, or a valid Maki icon. If a letter is requested, it will be rendered uppercase only. + * StaticMarkerAnnotation symbol. Options are an alphanumeric label {@code a}-{@code A} through + * {@code z}-{@code Z}, {@code 0} through {@code 99}, or a valid Maki icon. If a letter is requested, it will be + * rendered uppercase only. * * @param label String containing the marker symbol. * @return This StaticMarkerAnnotation builder. @@ -167,7 +177,11 @@ public Builder setLabel(String label) { * @since 2.1.0 */ public String getColor() { - return color; + if (color.isEmpty()) { + return EMPTY; + } + + return PLUS_CHAR.concat(color); } /** @@ -296,6 +310,33 @@ public StaticMarkerAnnotation build() throws ServicesException { } } + if (!label.isEmpty()) { + boolean isANumber = true; + try { + Integer.parseInt(label); + } catch (NumberFormatException notANumber) { + isANumber = false; + } + if (isANumber) { + Pattern pattern = Pattern.compile(NUMERIC_FROM_ZERO_TO_NINETYNINE_REGEX); + Matcher matcher = pattern.matcher(label); + if (!matcher.matches()) { + throw new ServicesException("You need to pass an alphanumeric label [0-99] code."); + } + } else { + // TODO Find a better way to know when a label is not a valid Maki icon + // Right now, this is not verified + // At the moment there's no Maki icon name with 2 characters or less + if (label.length() < 3) { + Pattern pattern = Pattern.compile(ONE_ALPHABET_LETTER_REGEX); + Matcher matcher = pattern.matcher(label); + if (!matcher.matches()) { + throw new ServicesException("You need to pass an alphanumeric label [a-zA-Z] code."); + } + } + } + } + return new StaticMarkerAnnotation(this); } } diff --git a/mapbox/libjava-services/src/test/java/com/mapbox/services/api/staticimage/v1/StaticMarkerAnnotationTest.java b/mapbox/libjava-services/src/test/java/com/mapbox/services/api/staticimage/v1/StaticMarkerAnnotationTest.java index 6318c6c03..b35600d71 100644 --- a/mapbox/libjava-services/src/test/java/com/mapbox/services/api/staticimage/v1/StaticMarkerAnnotationTest.java +++ b/mapbox/libjava-services/src/test/java/com/mapbox/services/api/staticimage/v1/StaticMarkerAnnotationTest.java @@ -26,7 +26,7 @@ public void testSanity() throws ServicesException { .setName(Constants.PIN_SMALL) .build(); - assertTrue(staticMarkerAnnotation.getMarker().contains("pin-s-+(1.000000,2.000000)")); + assertTrue(staticMarkerAnnotation.getMarker().contains("pin-s(1.000000,2.000000)")); } @Test @@ -65,8 +65,76 @@ public void requireMarkerLon() throws ServicesException { new StaticMarkerAnnotation.Builder().setName(Constants.PIN_SMALL).setLat(2.0).build(); } + @Test(expected = ServicesException.class) + public void requiresOneAlphabetCharLabelIfPresent() throws ServicesException { + new StaticMarkerAnnotation.Builder() + .setName(Constants.PIN_SMALL) + .setLat(2.0) + .setLon(2.0) + .setLabel("aa") + .build(); + } + + @Test(expected = ServicesException.class) + public void requiresZeroToNinetynineNumericCharLabelIfPresent() throws ServicesException { + new StaticMarkerAnnotation.Builder() + .setName(Constants.PIN_SMALL) + .setLat(2.0) + .setLon(2.0) + .setLabel("100") + .build(); + } + + @Test + public void checksMarkerAlphabetCharLabel() throws ServicesException { + StaticMarkerAnnotation staticMarkerAnnotation = new StaticMarkerAnnotation.Builder() + .setLat(2.0) + .setLon(1.0) + .setName(Constants.PIN_SMALL) + .setLabel("a") + .build(); + + assertTrue(staticMarkerAnnotation.getMarker().contains("pin-s-a(1.000000,2.000000)")); + } + + @Test + public void checksMarkerCaseInsensitiveAlphabetCharLabel() throws ServicesException { + StaticMarkerAnnotation staticMarkerAnnotation = new StaticMarkerAnnotation.Builder() + .setLat(2.0) + .setLon(1.0) + .setName(Constants.PIN_SMALL) + .setLabel("A") + .build(); + + assertTrue(staticMarkerAnnotation.getMarker().contains("pin-s-a(1.000000,2.000000)")); + } + + @Test + public void checksMarkerNumericCharLabel() throws ServicesException { + StaticMarkerAnnotation staticMarkerAnnotation = new StaticMarkerAnnotation.Builder() + .setLat(2.0) + .setLon(1.0) + .setName(Constants.PIN_SMALL) + .setLabel("33") + .build(); + + assertTrue(staticMarkerAnnotation.getMarker().contains("pin-s-33(1.000000,2.000000)")); + } + @Test - public void requireValidMarkerColor() throws ServicesException { + public void checksMarkerMapboxMakiIconCharLabel() throws ServicesException { + StaticMarkerAnnotation staticMarkerAnnotation = new StaticMarkerAnnotation.Builder() + .setLat(2.0) + .setLon(1.0) + .setName(Constants.PIN_SMALL) + .setLabel("bakery") + .build(); + + assertTrue(staticMarkerAnnotation.getMarker().contains("pin-s-bakery(1.000000,2.000000)")); + } + + @Test + public void requiresValidColorCodeIfPresent() throws ServicesException { thrown.expect(ServicesException.class); thrown.expectMessage(startsWith("You need to pass 3- or 6-digit hexadecimal color code.")); @@ -78,6 +146,30 @@ public void requireValidMarkerColor() throws ServicesException { .build(); } + @Test + public void checksMarkerWithThreeDigitColorCode() throws ServicesException { + StaticMarkerAnnotation staticMarkerAnnotation = new StaticMarkerAnnotation.Builder() + .setLat(2.0) + .setLon(1.0) + .setName(Constants.PIN_SMALL) + .setColor("333") + .build(); + + assertTrue(staticMarkerAnnotation.getMarker().contains("pin-s+333(1.000000,2.000000)")); + } + + @Test + public void checksMarkerWithSixDigitHexColorCode() throws ServicesException { + StaticMarkerAnnotation staticMarkerAnnotation = new StaticMarkerAnnotation.Builder() + .setLat(2.0) + .setLon(1.0) + .setName(Constants.PIN_SMALL) + .setColor("666666") + .build(); + + assertTrue(staticMarkerAnnotation.getMarker().contains("pin-s+666666(1.000000,2.000000)")); + } + @Test public void markerPositionWorking() throws ServicesException { Position position = Position.fromCoordinates(1.0, 2.0); @@ -102,7 +194,7 @@ public void singleMarkerInUrl() { .setWidth(100).setHeight(200) .build(); - assertTrue(image.getUrl().toString().contains("pin-s-+(1.000000,2.000000)")); + assertTrue(image.getUrl().toString().contains("pin-s(1.000000,2.000000)")); } @Test @@ -125,7 +217,7 @@ public void doubleMarkersInUrl() { .setWidth(100).setHeight(200) .build(); - assertTrue(image.getUrl().toString().contains("pin-s-+(1.000000,2.000000),pin-m-+(5.000000,6.000000)")); + assertTrue(image.getUrl().toString().contains("pin-s(1.000000,2.000000),pin-m(5.000000,6.000000)")); } @Test