AddressTemplate implements Iterable<String> { @JsIgnore public AddressTemplate replaceWildcards(String wildcard, String... wildcards) { List<String> allWildcards = new ArrayList<>(); allWildcards.add(wildcard); if (wildcards != null) { allWildcards.addAll(Arrays.asList(wildcards)); } LinkedList<Token> replacedTokens = new LinkedList<>(); Iterator<String> wi = allWildcards.iterator(); for (Token token : tokens) { if (wi.hasNext() && token.hasKey() && "*".equals(token.getValue())) { replacedTokens.add(new Token(token.getKey(), wi.next())); } else { replacedTokens.add(new Token(token.key, token.value)); } } return AddressTemplate.of(join(this.optional, replacedTokens)); } private  AddressTemplate(String template); @JsIgnore static AddressTemplate of(StatementContext.Expression placeholder); @JsIgnore static AddressTemplate of(StatementContext.Expression placeholder, String template); @JsIgnore static AddressTemplate of(StatementContext.Expression placeholder1,
            StatementContext.Expression placeholder2); @JsIgnore static AddressTemplate of(StatementContext.Expression placeholder1, StatementContext.Expression placeholder2,
            String template); static AddressTemplate of(String template); @JsIgnore static AddressTemplate of(ResourceAddress address); @JsIgnore static AddressTemplate of(ResourceAddress address, Unresolver unresolver); @Override @JsIgnore boolean equals(Object o); @Override @JsIgnore int hashCode(); @Override String toString(); @JsProperty boolean isEmpty(); @JsProperty(name = "size") int size(); @Override @JsIgnore Iterator<String> iterator(); @JsIgnore AddressTemplate append(String template); @JsIgnore AddressTemplate append(AddressTemplate template); @JsIgnore AddressTemplate subTemplate(int fromIndex, int toIndex); @JsProperty AddressTemplate getParent(); @JsIgnore AddressTemplate replaceWildcards(String wildcard, String... wildcards); @JsProperty(name = "firstName") String firstName(); @JsProperty(name = "firstValue") String firstValue(); @JsProperty(name = "lastName") String lastName(); @JsProperty(name = "lastValue") String lastValue(); @JsIgnore boolean isOptional(); ResourceAddress resolve(StatementContext context, @EsParam("...string") String... wildcards); @JsMethod(name = "append") AddressTemplate jsAppend(@EsParam("string|AddressTemplate") Object address); static final AddressTemplate ROOT; static final String EQUALS; @JsIgnore static final String OPTIONAL; }
@Test public void replaceWildcards() { AddressTemplate at = AddressTemplate.of("a=b"); at = at.replaceWildcards(null); assertEquals("a=b", at.getTemplate()); at = AddressTemplate.of("a=b"); at = at.replaceWildcards(null, (String[]) null); assertEquals("a=b", at.getTemplate()); at = AddressTemplate.of("a=b"); at = at.replaceWildcards("foo"); assertEquals("a=b", at.getTemplate()); at = AddressTemplate.of("{a}/b={c}"); at = at.replaceWildcards("foo"); assertEquals("{a}/b={c}", at.getTemplate()); at = AddressTemplate.of("a=*/c=*"); at = at.replaceWildcards("b"); assertEquals("a=b/c=*", at.getTemplate()); at = AddressTemplate.of("a=*/c=*"); at = at.replaceWildcards("b", (String[]) null); assertEquals("a=b/c=*", at.getTemplate()); at = AddressTemplate.of("a=*/c=*"); at = at.replaceWildcards("b", "d"); assertEquals("a=b/c=d", at.getTemplate()); at = AddressTemplate.of("a=*/c=*"); at = at.replaceWildcards("b", "d", "foo"); assertEquals("a=b/c=d", at.getTemplate()); at = AddressTemplate.of("a=*/c={d}"); at = at.replaceWildcards("b", "d", "foo"); assertEquals("a=b/c={d}", at.getTemplate()); }