DirectLink implements ResourceLink { @Override public ResourceLink mergeWith(ResourceLink other) { if (!other.isDirectLink()) { throw new IllegalArgumentException(format( "Merging DirectLink with TemplatedLink is not supported. \nDirectLink = %s \nTemplatedLink = %s", toString(), other)); } if (!relationType.equals(other.getLinkRelationType())) { throw new IllegalArgumentException("Resource links with different relation types can not be merged. One resource must not have multiple relation types."); } final DirectLink otherDirectLink = (DirectLink)other; if (!href.equals(otherDirectLink.getHref())) { throw new IllegalArgumentException("Resource links with different hrefs can not be merged."); } return new DirectLink( relationType, href, hints.mergeWith(other.getHints()) ); } private  DirectLink(final URI relationType,
                       final URI href,
                       final Hints hints); static DirectLink directLink(final URI relationType,
                                        final URI href,
                                        final Hints hints); @Override URI getLinkRelationType(); URI getHref(); @Override Hints getHints(); @Override boolean isDirectLink(); @Override TemplatedLink asTemplatedLink(); @Override DirectLink asDirectLink(); @Override ResourceLink mergeWith(ResourceLink other); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString();  }
@Test(expectedExceptions = IllegalArgumentException.class) public void mergeWithShouldFailWithExceptionIfOtherDoesNotHaveSameRelationType() { final DirectLink storeFrontLink = STOREFRONT_LINK; final DirectLink aboutPageLink = SHOPPAGES_LINK; storeFrontLink.mergeWith(aboutPageLink); }
@Test(expectedExceptions = IllegalArgumentException.class) public void mergeWithTemplatedLinkShouldFailWithIllegalStateException() { final DirectLink storeFrontLink = STOREFRONT_LINK; final TemplatedLink templatedLink = ABOUTPAGE_LINK; storeFrontLink.mergeWith(templatedLink); }
@Test public void mergeWithSelfShouldReturnEqualDirectLink() { final DirectLink storeFrontLink = STOREFRONT_LINK; final ResourceLink mergedLink = storeFrontLink.mergeWith(storeFrontLink); assertEquals(storeFrontLink, mergedLink); }