1
1
/*
2
- * Copyright 2012-2013 the original author or authors.
2
+ * Copyright 2012-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
23
23
import org .springframework .beans .BeanUtils ;
24
24
import org .springframework .hateoas .ResourceAssembler ;
25
25
import org .springframework .hateoas .ResourceSupport ;
26
- import org .springframework .util .Assert ;
26
+ import org .springframework .hateoas .Resources ;
27
+ import org .springframework .hateoas .core .Objects ;
27
28
28
29
/**
29
30
* Base class to implement {@link ResourceAssembler}s. Will automate {@link ResourceSupport} instance creation and make
30
31
* sure a self-link is always added.
31
32
*
32
33
* @author Oliver Gierke
34
+ * @author Greg Turnquist
33
35
*/
34
36
public abstract class ResourceAssemblerSupport <T , D extends ResourceSupport > implements ResourceAssembler <T , D > {
35
37
@@ -44,30 +46,20 @@ public abstract class ResourceAssemblerSupport<T, D extends ResourceSupport> imp
44
46
*/
45
47
public ResourceAssemblerSupport (Class <?> controllerClass , Class <D > resourceType ) {
46
48
47
- Assert . notNull (controllerClass , "ControllerClass must not be null!" );
48
- Assert . notNull (resourceType , "ResourceType must not be null!" );
49
+ Objects . requireNonNull (controllerClass , "ControllerClass must not be null!" );
50
+ Objects . requireNonNull (resourceType , "ResourceType must not be null!" );
49
51
50
52
this .controllerClass = controllerClass ;
51
53
this .resourceType = resourceType ;
52
54
}
53
55
54
- /**
55
- * Converts all given entities into resources.
56
- *
57
- * @see #toResource(Object)
58
- * @param entities must not be {@literal null}.
59
- * @return
60
- */
61
- public List <D > toResources (Iterable <? extends T > entities ) {
62
-
63
- Assert .notNull (entities , "Entities must not be null!" );
64
- List <D > result = new ArrayList <D >();
65
-
66
- for (T entity : entities ) {
67
- result .add (toResource (entity ));
68
- }
56
+ @ Override
57
+ public Resources <D > toResources (List <T > entities ) {
58
+ return this .map (entities ).toResources ();
59
+ }
69
60
70
- return result ;
61
+ public Builder <T , D > map (Iterable <? extends T > entities ) {
62
+ return new Builder <>(entities , this );
71
63
}
72
64
73
65
/**
@@ -83,11 +75,11 @@ protected D createResourceWithId(Object id, T entity) {
83
75
84
76
protected D createResourceWithId (Object id , T entity , Object ... parameters ) {
85
77
86
- Assert . notNull (entity , "Entity must not be null!" );
87
- Assert . notNull (id , "Id must not be null!" );
78
+ Objects . requireNonNull (entity , "Entity must not be null!" );
79
+ Objects . requireNonNull (id , "Id must not be null!" );
88
80
89
81
D instance = instantiateResource (entity );
90
- instance .add (linkTo (controllerClass , parameters ).slash (id ).withSelfRel ());
82
+ instance .add (linkTo (this . controllerClass , parameters ).slash (id ).withSelfRel ());
91
83
return instance ;
92
84
}
93
85
@@ -100,6 +92,46 @@ protected D createResourceWithId(Object id, T entity, Object... parameters) {
100
92
* @return
101
93
*/
102
94
protected D instantiateResource (T entity ) {
103
- return BeanUtils .instantiateClass (resourceType );
95
+ return BeanUtils .instantiateClass (this .resourceType );
96
+ }
97
+
98
+ static class Builder <T , D extends ResourceSupport > {
99
+
100
+ private final Iterable <? extends T > entities ;
101
+ private final ResourceAssemblerSupport <T , D > resourceAssembler ;
102
+
103
+ Builder (Iterable <? extends T > entities , ResourceAssemblerSupport <T , D > resourceAssembler ) {
104
+
105
+ this .entities = Objects .requireNonNull (entities , "entities must not null!" );
106
+ this .resourceAssembler = resourceAssembler ;
107
+ }
108
+
109
+ /**
110
+ * Transform a list of {@code T}s into a list of {@link ResourceSupport}s.
111
+ *
112
+ * @see {@link #toListOfResources()} if you need this transformed list rendered as hypermedia
113
+ *
114
+ * @return
115
+ */
116
+ public List <D > toListOfResources () {
117
+
118
+ List <D > result = new ArrayList <>();
119
+
120
+ for (T entity : this .entities ) {
121
+ result .add (this .resourceAssembler .toResource (entity ));
122
+ }
123
+
124
+ return result ;
125
+ }
126
+
127
+ /**
128
+ * Converts all given entities into resources and wraps the result in a {@link Resources} instance.
129
+ *
130
+ * @see {@link #toListOfResources()}} and {@link ResourceAssembler#toResource(Object)}
131
+ * @return
132
+ */
133
+ public Resources <D > toResources () {
134
+ return new Resources <>(toListOfResources ());
135
+ }
104
136
}
105
137
}
0 commit comments