/** * Define rel semantics for this representation. * * @param rel A defined relationship type */ public ResourceRepresentation<V> withRel(Rel rel) { if (rels.containsKey(rel.rel())) { throw new IllegalStateException(String.format("Rel %s is already declared.", rel.rel())); } final TreeMap<String, Rel> updatedRels = rels.put(rel.rel(), rel); return new ResourceRepresentation<>( content, links, updatedRels, namespaceManager, value, resources); }
/** * Adds a new namespace. * * @param namespace The CURIE prefix for the namespace being added. * @param href The target href of the namespace being added. This may be relative to the * resourceFactories baseref * @return A new instance of a PersistentRepresentation with the namespace included. */ public ResourceRepresentation<V> withNamespace(String namespace, String href) { if (!rels.containsKey("curies")) { rels = rels.put("curies", Rels.collection("curies")); } final NamespaceManager updatedNamespaceManager = namespaceManager.withNamespace(namespace, href); return new ResourceRepresentation<>( content, links, rels, updatedNamespaceManager, value, resources); }
public ResourceRepresentation<V> withRepresentation( String rel, ResourceRepresentation<?> resource) { if (resources.containsValue(resource)) { throw new IllegalStateException("Resource is already embedded."); } Support.checkRelType(rel); validateSingletonRel(rel); Multimap<String, ResourceRepresentation<?>> updatedResources = resources.put(rel, resource); ResourceRepresentation<V> updatedRepresentation = new ResourceRepresentation<>( content, links, rels, namespaceManager, value, updatedResources); // Propagate null property flag to parent. if (resource.hasNullProperties()) { updatedRepresentation.hasNullProperties = true; } if (!rels.containsKey(rel)) { updatedRepresentation = updatedRepresentation.withRel(Rels.natural(rel)); } return updatedRepresentation; }
/** * Add a link to this resource. * * @param link The target link */ public ResourceRepresentation<V> withLink(Link link) { String rel = Links.getRel(link); Support.checkRelType(rel); validateSingletonRel(rel); final TreeMap<String, Rel> updatedRels = !rels.containsKey(rel) ? rels.put(rel, Rels.natural(rel)) : rels; final List<Link> updatedLinks = links.append(link); return new ResourceRepresentation<>( content, updatedLinks, updatedRels, namespaceManager, value, resources); }
/** * Add a link to this resource. * * @param links The target link */ public ResourceRepresentation<V> withLinks(List<Link> links) { links.forEach( link -> { String rel = Links.getRel(link); Support.checkRelType(rel); validateSingletonRel(rel); }); final TreeMap<String, Rel> updatedRels = links .map(Links::getRel) .foldLeft( rels, (accum, rel) -> !accum.containsKey(rel) ? accum.put(rel, Rels.natural(rel)) : accum); final List<Link> updatedLinks = this.links.appendAll(links); return new ResourceRepresentation<>( content, updatedLinks, updatedRels, namespaceManager, value, resources); }