-
Notifications
You must be signed in to change notification settings - Fork 560
/
Resource.java
90 lines (78 loc) · 1.64 KB
/
Resource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package org.springframework.data.rest.core;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.annotate.JsonUnwrapped;
import org.springframework.util.Assert;
/**
* Wraps a simple object or a bean plus a set of links.
*
* @author Jon Brisbin
*/
public class Resource<T> {
@JsonUnwrapped
protected T resource;
@JsonProperty("links")
protected Set<Link> links = new HashSet<Link>();
public Resource() {
}
public Resource(T resource) {
this.resource = resource;
}
/**
* Get the resource. May be {@literal null}.
*
* @return The resource or {@literal null}.
*/
public T getResource() {
return resource;
}
/**
* Set the resource.
*
* @param resource
*
* @return {@literal this}
*/
public Resource<T> setResource(T resource) {
this.resource = resource;
return this;
}
/**
* Get the set of {@link Link}s for this resource.
*
* @return
*/
public Set<Link> getLinks() {
return links;
}
/**
* Set the entire set of {@link Link}s.
*
* @param links
*
* @return {@literal this}
*/
@SuppressWarnings({"unchecked"})
public Resource<T> setLinks(Set<Link> links) {
if(null == links) {
this.links = Collections.emptySet();
} else {
this.links = links;
}
return this;
}
/**
* Add a {@link Link} to this resource's set.
*
* @param link
*
* @return {@literal this}
*/
public Resource<T> addLink(Link link) {
Assert.notNull(link, "Link cannot be null.");
links.add(link);
return this;
}
}