Skip to content

Commit

Permalink
#566 - Return list of links for a given rel
Browse files Browse the repository at this point in the history
Introduces `ResourceSupport.getLinks(String rel)` to allow returning ALL links related to a give rel.

Resolves #542,#318,#319,#157
  • Loading branch information
gregturn committed Mar 26, 2017
1 parent cca342d commit 0e8b0e1
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 173 deletions.
364 changes: 191 additions & 173 deletions src/main/java/org/springframework/hateoas/ResourceSupport.java
Original file line number Diff line number Diff line change
@@ -1,173 +1,191 @@
/*
* Copyright 2012-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.hateoas;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;

import org.springframework.util.Assert;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
* Base class for DTOs to collect links.
*
* @author Oliver Gierke
*/
public class ResourceSupport implements Identifiable<Link> {

private final List<Link> links;

public ResourceSupport() {
this.links = new ArrayList<Link>();
}

/**
* Returns the {@link Link} with a rel of {@link Link#REL_SELF}.
*/
@JsonIgnore
public Link getId() {
return getLink(Link.REL_SELF);
}

/**
* Adds the given link to the resource.
*
* @param link
*/
public void add(Link link) {
Assert.notNull(link, "Link must not be null!");
this.links.add(link);
}

/**
* Adds all given {@link Link}s to the resource.
*
* @param links
*/
public void add(Iterable<Link> links) {
Assert.notNull(links, "Given links must not be null!");
for (Link candidate : links) {
add(candidate);
}
}

/**
* Adds all given {@link Link}s to the resource.
*
* @param links must not be {@literal null}.
*/
public void add(Link... links) {
Assert.notNull(links, "Given links must not be null!");
add(Arrays.asList(links));
}

/**
* Returns whether the resource contains {@link Link}s at all.
*
* @return
*/
public boolean hasLinks() {
return !this.links.isEmpty();
}

/**
* Returns whether the resource contains a {@link Link} with the given rel.
*
* @param rel
* @return
*/
public boolean hasLink(String rel) {
return getLink(rel) != null;
}

/**
* Returns all {@link Link}s contained in this resource.
*
* @return
*/
@XmlElement(name = "link", namespace = Link.ATOM_NAMESPACE)
@JsonProperty("links")
public List<Link> getLinks() {
return links;
}

/**
* Removes all {@link Link}s added to the resource so far.
*/
public void removeLinks() {
this.links.clear();
}

/**
* Returns the link with the given rel.
*
* @param rel
* @return the link with the given rel or {@literal null} if none found.
*/
public Link getLink(String rel) {

for (Link link : links) {
if (link.getRel().equals(rel)) {
return link;
}
}

return null;
}

/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return String.format("links: %s", links.toString());
}

/*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {

if (this == obj) {
return true;
}

if (obj == null || !obj.getClass().equals(this.getClass())) {
return false;
}

ResourceSupport that = (ResourceSupport) obj;

return this.links.equals(that.links);
}

/*
* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
return this.links.hashCode();
}
}
/*
* Copyright 2012-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.hateoas;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;

import org.springframework.util.Assert;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
* Base class for DTOs to collect links.
*
* @author Oliver Gierke
*/
public class ResourceSupport implements Identifiable<Link> {

private final List<Link> links;

public ResourceSupport() {
this.links = new ArrayList<Link>();
}

/**
* Returns the {@link Link} with a rel of {@link Link#REL_SELF}.
*/
@JsonIgnore
public Link getId() {
return getLink(Link.REL_SELF);
}

/**
* Adds the given link to the resource.
*
* @param link
*/
public void add(Link link) {
Assert.notNull(link, "Link must not be null!");
this.links.add(link);
}

/**
* Adds all given {@link Link}s to the resource.
*
* @param links
*/
public void add(Iterable<Link> links) {
Assert.notNull(links, "Given links must not be null!");
for (Link candidate : links) {
add(candidate);
}
}

/**
* Adds all given {@link Link}s to the resource.
*
* @param links must not be {@literal null}.
*/
public void add(Link... links) {
Assert.notNull(links, "Given links must not be null!");
add(Arrays.asList(links));
}

/**
* Returns whether the resource contains {@link Link}s at all.
*
* @return
*/
public boolean hasLinks() {
return !this.links.isEmpty();
}

/**
* Returns whether the resource contains a {@link Link} with the given rel.
*
* @param rel
* @return
*/
public boolean hasLink(String rel) {
return getLink(rel) != null;
}

/**
* Returns all {@link Link}s contained in this resource.
*
* @return
*/
@XmlElement(name = "link", namespace = Link.ATOM_NAMESPACE)
@JsonProperty("links")
public List<Link> getLinks() {
return links;
}

/**
* Removes all {@link Link}s added to the resource so far.
*/
public void removeLinks() {
this.links.clear();
}

/**
* Returns the link with the given rel.
*
* @param rel
* @return the link with the given rel or {@literal null} if none found.
*/
public Link getLink(String rel) {

for (Link link : links) {
if (link.getRel().equals(rel)) {
return link;
}
}

return null;
}

/**
* Returns all {@link Link}s with the given relation type.
*
* @return the links in a {@link List}
*/
public List<Link> getLinks(String rel) {

List<Link> relatedLinks = new ArrayList<Link>();

for (Link link : links) {
if (link.getRel().equals(rel)) {
relatedLinks.add(link);
}
}

return relatedLinks;
}

/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return String.format("links: %s", links.toString());
}

/*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {

if (this == obj) {
return true;
}

if (obj == null || !obj.getClass().equals(this.getClass())) {
return false;
}

ResourceSupport that = (ResourceSupport) obj;

return this.links.equals(that.links);
}

/*
* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
return this.links.hashCode();
}
}
Loading

0 comments on commit 0e8b0e1

Please sign in to comment.