TemplatedLink implements ResourceLink { @Override public ResourceLink mergeWith(ResourceLink other) { if (other.isDirectLink()) { throw new IllegalArgumentException(format( "Merging TemplatedLink with DirectLink is not supported. " + "\nTemplatedLink = %s \nDirectLink = %s", this, other)); } if (!relationType.equals(other.getLinkRelationType())) { throw new IllegalArgumentException(format( "Resource links with different relation types can not be merged. " + "One resource must not have multiple relation types. " + "\nTemplatedLink=%s, \nDirectLink=%s", this, other)); } final TemplatedLink otherTemplatedLink = (TemplatedLink)other; if (hrefTemplate.startsWith(otherTemplatedLink.getHrefTemplate())) { return new TemplatedLink( relationType, hrefTemplate, hrefVars, hints.mergeWith(other.getHints()) ); } if (otherTemplatedLink.getHrefTemplate().startsWith(hrefTemplate)) { return new TemplatedLink( relationType, otherTemplatedLink.getHrefTemplate(), otherTemplatedLink.getHrefVars(), hints.mergeWith(otherTemplatedLink.getHints()) ); } throw new IllegalArgumentException(format( "Templated resource-links with different uri templates can not be merged. " + "\nTemplatedLink=%s, \nDirectLink=%s", this, other)); } private  TemplatedLink(final URI relationType,
                          final String hrefTemplate,
                          final Collection<HrefVar> hrefVars,
                          final Hints hints); static TemplatedLink templatedLink(final URI relationType,
                                              final String hrefTemplate,
                                              final List<HrefVar> hrefVars,
                                              final Hints hints); @Override URI getLinkRelationType(); String getHrefTemplate(); List<HrefVar> getHrefVars(); HrefVar getHrefVar(final URI varType); @Override Hints getHints(); @Override boolean isDirectLink(); @Override TemplatedLink asTemplatedLink(); @Override DirectLink asDirectLink(); URI expandToUri(final Map<URI, Object> values); URI expandToUri(final URI hrefVarBarUri, final Object value); URI expandToUri(final URI hrefVarBarUri0, final Object value0,
                           final URI hrefVarBarUri1, final Object value1); URI expandToUri(final URI hrefVarBarUri0, final Object value0,
                           final URI hrefVarBarUri1, final Object value1,
                           final URI hrefVarBarUri2, final Object value2); URI expandToUri(final URI hrefVarBarUri0, final Object value0,
                           final URI hrefVarBarUri1, final Object value1,
                           final URI hrefVarBarUri2, final Object value2,
                           final URI hrefVarBarUri3, final Object value3); URI expandToUri(final URI hrefVarBarUri0, final Object value0,
                           final URI hrefVarBarUri1, final Object value1,
                           final URI hrefVarBarUri2, final Object value2,
                           final URI hrefVarBarUri3, final Object value3,
                           final URI hrefVarBarUri4, final Object value4); URI expandToUri(final URI hrefVarBarUri0, final Object value0,
                           final URI hrefVarBarUri1, final Object value1,
                           final URI hrefVarBarUri2, final Object value2,
                           final URI hrefVarBarUri3, final Object value3,
                           final URI hrefVarBarUri4, final Object value4,
                           final URI hrefVarBarUri5, final Object value5); URI expandToUri(final URI hrefVarBarUri0, final Object value0,
                           final URI hrefVarBarUri1, final Object value1,
                           final URI hrefVarBarUri2, final Object value2,
                           final URI hrefVarBarUri3, final Object value3,
                           final URI hrefVarBarUri4, final Object value4,
                           final URI hrefVarBarUri5, final Object value5,
                           final URI hrefVarBarUri6, final Object value6); URI expandToUri(final URI hrefVarBarUri0, final Object value0,
                           final URI hrefVarBarUri1, final Object value1,
                           final URI hrefVarBarUri2, final Object value2,
                           final URI hrefVarBarUri3, final Object value3,
                           final URI hrefVarBarUri4, final Object value4,
                           final URI hrefVarBarUri5, final Object value5,
                           final URI hrefVarBarUri6, final Object value6,
                           final URI hrefVarBarUri7, final Object value7); URI expandToUri(final URI hrefVarBarUri0, final Object value0,
                           final URI hrefVarBarUri1, final Object value1,
                           final URI hrefVarBarUri2, final Object value2,
                           final URI hrefVarBarUri3, final Object value3,
                           final URI hrefVarBarUri4, final Object value4,
                           final URI hrefVarBarUri5, final Object value5,
                           final URI hrefVarBarUri6, final Object value6,
                           final URI hrefVarBarUri7, final Object value7,
                           final URI hrefVarBarUri8, final Object value8); URI expandToUri(final URI hrefVarBarUri0, final Object value0,
                           final URI hrefVarBarUri1, final Object value1,
                           final URI hrefVarBarUri2, final Object value2,
                           final URI hrefVarBarUri3, final Object value3,
                           final URI hrefVarBarUri4, final Object value4,
                           final URI hrefVarBarUri5, final Object value5,
                           final URI hrefVarBarUri6, final Object value6,
                           final URI hrefVarBarUri7, final Object value7,
                           final URI hrefVarBarUri8, final Object value8,
                           final URI hrefVarBarUri9, final Object value9); @Override ResourceLink mergeWith(ResourceLink other); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString();  }
@Test(expectedExceptions = IllegalArgumentException.class) public void mergeWithDirectLinkShouldFailWithIllegalStateException() { final TemplatedLink templatedLink = ABOUTPAGE_LINK; final DirectLink directLink = STOREFRONT_LINK; templatedLink.mergeWith(directLink); }
@Test public void mergeWithSelfShouldReturnEqualTemplatedLink() { final TemplatedLink aboutLink = ABOUTPAGE_LINK; final ResourceLink mergedLink = aboutLink.mergeWith(aboutLink); assertEquals(aboutLink, mergedLink); }