Skip to content

Commit fff5396

Browse files
committed
#1059 - Cleanup the core codebase.
Clean up code containing various patterns: * Remove redundant `final`, `public`, and `static` declarations in interfaces and enums. * Replace redundant `? extends Object` generic parameters with `?`. * Convert collections to arrays using empty array instead of pre-sized one (modern JVM recommendation). * Initialize collections using constructor call over `addAll`. * Favor `addAll` over iterating and adding one-by-one. * Take advantage of `Map.forEach` that was introduced with Java 8.
1 parent 9d83787 commit fff5396

24 files changed

+94
-103
lines changed

src/main/java/org/springframework/hateoas/AffordanceModel.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public boolean pointsToTargetOf(Link link) {
114114
*/
115115
public interface PayloadMetadata {
116116

117-
public static PayloadMetadata NONE = NoPayloadMetadata.INSTANCE;
117+
PayloadMetadata NONE = NoPayloadMetadata.INSTANCE;
118118

119119
/**
120120
* Returns all properties contained in a payload.
@@ -135,7 +135,7 @@ default Optional<PropertyMetadata> getPropertyMetadata(String name) {
135135
*/
136136
public interface InputPayloadMetadata extends PayloadMetadata {
137137

138-
static InputPayloadMetadata NONE = from(PayloadMetadata.NONE);
138+
InputPayloadMetadata NONE = from(PayloadMetadata.NONE);
139139

140140
static InputPayloadMetadata from(PayloadMetadata metadata) {
141141

@@ -298,7 +298,7 @@ public interface Named {
298298
*
299299
* @author Oliver Drotbohm
300300
*/
301-
private static enum NoPayloadMetadata implements PayloadMetadata {
301+
private enum NoPayloadMetadata implements PayloadMetadata {
302302

303303
INSTANCE;
304304

src/main/java/org/springframework/hateoas/Link.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,7 @@ public Link andAffordance(Affordance affordance) {
202202

203203
Assert.notNull(affordance, "Affordance must not be null!");
204204

205-
List<Affordance> newAffordances = new ArrayList<>();
206-
newAffordances.addAll(this.affordances);
205+
List<Affordance> newAffordances = new ArrayList<>(this.affordances);
207206
newAffordances.add(affordance);
208207

209208
return withAffordances(newAffordances);
@@ -281,7 +280,7 @@ public Link expand(Object... arguments) {
281280
* @param arguments must not be {@literal null}.
282281
* @return
283282
*/
284-
public Link expand(Map<String, ? extends Object> arguments) {
283+
public Link expand(Map<String, ?> arguments) {
285284
return new Link(template.expand(arguments).toString(), getRel());
286285
}
287286

src/main/java/org/springframework/hateoas/Links.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ private List<Link> allWithoutRels(Iterable<Link> links) {
457457
*
458458
* @author Oliver Drotbohm
459459
*/
460-
public static enum MergeMode {
460+
public enum MergeMode {
461461

462462
/**
463463
* Skips to add the same links on merge. Multiple links with the same link relation might appear.
@@ -472,6 +472,6 @@ public static enum MergeMode {
472472
/**
473473
* Replaces existing links with the same link relation.
474474
*/
475-
REPLACE_BY_REL;
475+
REPLACE_BY_REL
476476
}
477477
}

src/main/java/org/springframework/hateoas/StringLinkRelation.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
class StringLinkRelation implements LinkRelation, Serializable {
4242

4343
private static final long serialVersionUID = -3904935345545567957L;
44-
private static final Map<String, StringLinkRelation> CACHE = new ConcurrentHashMap<String, StringLinkRelation>(256);
44+
private static final Map<String, StringLinkRelation> CACHE = new ConcurrentHashMap<>(256);
4545

4646
@NonNull String relation;
4747

src/main/java/org/springframework/hateoas/UriTemplate.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public URI expand(Object... parameters) {
267267
* @param parameters must not be {@literal null}.
268268
* @return
269269
*/
270-
public URI expand(Map<String, ? extends Object> parameters) {
270+
public URI expand(Map<String, ?> parameters) {
271271

272272
if (TemplateVariables.NONE.equals(variables)) {
273273
return URI.create(baseUri);
@@ -373,8 +373,7 @@ private static void appendComposite(UriComponentsBuilder builder, String name, O
373373

374374
} else if (value instanceof Map) {
375375

376-
((Map<Object, Object>) value).entrySet() //
377-
.forEach(it -> builder.queryParam(it.getKey().toString(), it.getValue()));
376+
((Map<Object, Object>) value).forEach((key, value1) -> builder.queryParam(key.toString(), value1));
378377

379378
} else {
380379

src/main/java/org/springframework/hateoas/client/Traverson.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public static List<HttpMessageConverter<?>> getDefaultMessageConverters(MediaTyp
126126
return DEFAULTS.getHttpMessageConverters(Arrays.asList(mediaTypes));
127127
}
128128

129-
private static final RestOperations createDefaultTemplate(List<MediaType> mediaTypes) {
129+
private static RestOperations createDefaultTemplate(List<MediaType> mediaTypes) {
130130

131131
RestTemplate template = new RestTemplate();
132132
template.setMessageConverters(DEFAULTS.getHttpMessageConverters(mediaTypes));

src/main/java/org/springframework/hateoas/config/HypermediaConfigurationImportSelector.java

-5
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,6 @@ public String[] selectImports(AnnotationMetadata metadata) {
5353
List<MediaTypeConfigurationProvider> configurationProviders = SpringFactoriesLoader.loadFactories(
5454
MediaTypeConfigurationProvider.class, HypermediaConfigurationImportSelector.class.getClassLoader());
5555

56-
// No additional filtering needed.
57-
if (types.isEmpty()) {
58-
return configurationProviders.toArray(new String[0]);
59-
}
60-
6156
// Filter the ones supporting the given media types
6257
return configurationProviders.stream() //
6358
.filter(it -> it.supportsAny(types)) //

src/main/java/org/springframework/hateoas/mediatype/MessageResolver.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*/
2929
public interface MessageResolver {
3030

31-
public static final MessageResolver DEFAULTS_ONLY = DefaultOnlyMessageResolver.INSTANCE;
31+
MessageResolver DEFAULTS_ONLY = DefaultOnlyMessageResolver.INSTANCE;
3232

3333
/**
3434
* Resolve the given {@link MessageSourceResolvable}. Return {@literal null} if no message was found.
@@ -45,7 +45,7 @@ public interface MessageResolver {
4545
* @param messageSource can be {@literal null}.
4646
* @return will never be {@literal null}.
4747
*/
48-
public static MessageResolver of(@Nullable MessageSource messageSource) {
48+
static MessageResolver of(@Nullable MessageSource messageSource) {
4949

5050
return messageSource == null //
5151
? DefaultOnlyMessageResolver.INSTANCE //

src/main/java/org/springframework/hateoas/mediatype/collectionjson/Jackson2CollectionJsonModule.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ public T deserialize(JsonParser parser, DeserializationContext ctxt) throws IOEx
876876

877877
return collection.getItems().stream() //
878878
.map(CollectionJsonItem::withOwnSelfLink) //
879-
.<Object> map(it -> isResource //
879+
.map(it -> isResource //
880880
? new EntityModel<>(it.toRawData(rootType), it.getLinks()) //
881881
: it.toRawData(rootType)) //
882882
.collect(Collectors.collectingAndThen(Collectors.toList(), it -> finalizer.apply(it, links)));

src/main/java/org/springframework/hateoas/mediatype/hal/CurieProvider.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*/
3232
public interface CurieProvider {
3333

34-
public static CurieProvider NONE = new CurieProvider() {
34+
CurieProvider NONE = new CurieProvider() {
3535

3636
@Override
3737
public HalLinkRelation getNamespacedRelFrom(Link link) {
@@ -44,7 +44,7 @@ public HalLinkRelation getNamespacedRelFor(LinkRelation rel) {
4444
}
4545

4646
@Override
47-
public Collection<? extends Object> getCurieInformation(Links links) {
47+
public Collection<?> getCurieInformation(Links links) {
4848
throw new UnsupportedOperationException();
4949
}
5050
};
@@ -75,5 +75,5 @@ public Collection<? extends Object> getCurieInformation(Links links) {
7575
* @param links the {@link Links} that have been added to the response so far.
7676
* @return must not be {@literal null}.
7777
*/
78-
Collection<? extends Object> getCurieInformation(Links links);
78+
Collection<?> getCurieInformation(Links links);
7979
}

src/main/java/org/springframework/hateoas/mediatype/hal/DefaultCurieProvider.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public DefaultCurieProvider(Map<String, UriTemplate> curies, @Nullable String de
9898
* @see org.springframework.hateoas.hal.CurieProvider#getCurieInformation()
9999
*/
100100
@Override
101-
public Collection<? extends Object> getCurieInformation(Links links) {
101+
public Collection<?> getCurieInformation(Links links) {
102102

103103
return curies.entrySet().stream() //
104104
.map(it -> new Curie(it.getKey(), getCurieHref(it.getKey(), it.getValue()))) //

src/main/java/org/springframework/hateoas/mediatype/hal/HalTraversonDefaults.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public List<LinkDiscoverer> getLinkDiscoverers(Collection<MediaType> mediaTypes)
8080
*
8181
* @return
8282
*/
83-
private static final HttpMessageConverter<?> getHalConverter(List<MediaType> halFlavours) {
83+
private static HttpMessageConverter<?> getHalConverter(List<MediaType> halFlavours) {
8484

8585
ObjectMapper mapper = new ObjectMapper();
8686
mapper.registerModule(new Jackson2HalModule());

src/main/java/org/springframework/hateoas/mediatype/hal/Jackson2HalModule.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,7 @@ public void serialize(Links value, JsonGenerator jgen, SerializerProvider provid
182182

183183
if (!skipCuries && prefixingRequired && curiedLinkPresent) {
184184

185-
ArrayList<Object> curies = new ArrayList<>();
186-
curies.addAll(curieProvider.getCurieInformation(Links.of(links)));
185+
ArrayList<Object> curies = new ArrayList<>(curieProvider.getCurieInformation(Links.of(links)));
187186

188187
sortedLinks.put(HalLinkRelation.CURIES, curies);
189188
}
@@ -525,7 +524,7 @@ private void serializeContents(Object value, JsonGenerator jgen, SerializerProvi
525524
private JsonSerializer<Object> getOrLookupSerializerFor(Object value, SerializerProvider provider)
526525
throws JsonMappingException {
527526

528-
Class<? extends Object> type = value.getClass();
527+
Class<?> type = value.getClass();
529528
JsonSerializer<Object> serializer = serializers.get(type);
530529

531530
if (serializer == null) {

src/main/java/org/springframework/hateoas/mediatype/hal/forms/HalFormsDocument.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,11 @@ public HalFormsTemplate getTemplate(String key) {
171171
}
172172

173173
public HalFormsDocument<T> withPageMetadata(@Nullable PageMetadata metadata) {
174-
return new HalFormsDocument<T>(attributes, entity, entities, embedded, metadata, links, templates);
174+
return new HalFormsDocument<>(attributes, entity, entities, embedded, metadata, links, templates);
175175
}
176176

177177
private HalFormsDocument<T> withEntity(@Nullable T entity) {
178-
return new HalFormsDocument<T>(attributes, entity, entities, embedded, pageMetadata, links, templates);
178+
return new HalFormsDocument<>(attributes, entity, entities, embedded, pageMetadata, links, templates);
179179
}
180180

181181
/**

src/main/java/org/springframework/hateoas/mediatype/hal/forms/HalFormsTemplateBuilder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public String[] getCodes() {
206206

207207
codes.add(globalCode);
208208

209-
return codes.toArray(new String[codes.size()]);
209+
return codes.toArray(new String[0]);
210210
}
211211
}
212212
}

src/main/java/org/springframework/hateoas/mediatype/uber/Jackson2UberModule.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ private static CollectionModel<?> extractResources(UberDocument doc, JavaType ro
847847
List<LinkRelation> rel = item.getRel();
848848

849849
if (rel != null) {
850-
item.getLinks().forEach(resourceLinks::add);
850+
resourceLinks.addAll(item.getLinks());
851851
} else {
852852

853853
// Primitive type

src/main/java/org/springframework/hateoas/server/EntityLinks.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ default <T> Link linkToItemResource(T entity, Function<T, Object> identifierExtr
127127
* @param extractor the extractor to use to derive an identifier from the given entity.
128128
* @return
129129
*/
130-
default <T> TypedEntityLinks<T> forType(Function<T, ? extends Object> extractor) {
130+
default <T> TypedEntityLinks<T> forType(Function<T, ?> extractor) {
131131
return new TypedEntityLinks<>(extractor, this);
132132
}
133133

src/main/java/org/springframework/hateoas/server/LinkRelationProvider.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ public interface LinkRelationProvider extends Plugin<LookupContext> {
7373
*/
7474
@RequiredArgsConstructor(staticName = "of", access = AccessLevel.PRIVATE)
7575
@EqualsAndHashCode
76-
static class LookupContext {
76+
class LookupContext {
7777

7878
private enum ResourceType {
79-
ITEM, COLLECTION;
79+
ITEM, COLLECTION
8080
}
8181

8282
private final @NonNull @Getter Class<?> type;

src/main/java/org/springframework/hateoas/server/TypedEntityLinks.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
3636
public class TypedEntityLinks<T> {
3737

38-
private final @NonNull Function<T, ? extends Object> identifierExtractor;
38+
private final @NonNull Function<T, ?> identifierExtractor;
3939
private final @NonNull EntityLinks entityLinks;
4040

4141
/**
@@ -73,7 +73,7 @@ public static class ExtendedTypedEntityLinks<T> extends TypedEntityLinks<T> {
7373
private final Class<T> type;
7474
private final EntityLinks delegate;
7575

76-
ExtendedTypedEntityLinks(Function<T, ? extends Object> identifierExtractor, EntityLinks delegate, Class<T> type) {
76+
ExtendedTypedEntityLinks(Function<T, ?> identifierExtractor, EntityLinks delegate, Class<T> type) {
7777

7878
super(identifierExtractor, delegate);
7979

src/main/java/org/springframework/hateoas/server/core/UriTemplateFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*/
3131
public class UriTemplateFactory {
3232

33-
private static final Map<String, UriTemplate> CACHE = new ConcurrentReferenceHashMap<String, UriTemplate>();
33+
private static final Map<String, UriTemplate> CACHE = new ConcurrentReferenceHashMap<>();
3434

3535
/**
3636
* Returns the the {@link UriTemplate} for the given mapping.

src/main/java/org/springframework/hateoas/server/mvc/RepresentationModelProcessorHandlerMethodReturnValueHandler.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ Object rewrapResult(RepresentationModel<?> newBody, @Nullable Object originalVal
142142
return rootLinksAsHeaders ? HeaderLinksResponseEntity.wrap(newBody) : newBody;
143143
}
144144

145-
HttpEntity<RepresentationModel<?>> entity = null;
145+
HttpEntity<RepresentationModel<?>> entity;
146146

147147
if (originalValue instanceof ResponseEntity) {
148148
ResponseEntity<?> source = (ResponseEntity<?>) originalValue;

src/test/java/org/springframework/hateoas/mediatype/collectionjson/CollectionJsonWebFluxIntegrationTest.java

+28-28
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ void setUp() {
6161
@Test
6262
void singleEmployee() {
6363

64-
this.testClient.get().uri("http://localhost/employees/0")
65-
.accept(MediaTypes.COLLECTION_JSON)
66-
.exchange()
67-
.expectStatus().isOk()
68-
.expectHeader().contentType(MediaTypes.COLLECTION_JSON)
69-
.expectBody(String.class)
64+
this.testClient.get().uri("http://localhost/employees/0") //
65+
.accept(MediaTypes.COLLECTION_JSON) //
66+
.exchange() //
67+
.expectStatus().isOk() //
68+
.expectHeader().contentType(MediaTypes.COLLECTION_JSON) //
69+
.expectBody(String.class) //
7070
.value(jsonPath("$.collection.version", is("1.0")))
7171
.value(jsonPath("$.collection.href", is("http://localhost/employees/0")))
7272
.value(jsonPath("$.collection.links.*", hasSize(1)))
@@ -96,12 +96,12 @@ void singleEmployee() {
9696
@Test
9797
void collectionOfEmployees() {
9898

99-
this.testClient.get().uri("http://localhost/employees")
100-
.accept(MediaTypes.COLLECTION_JSON)
101-
.exchange()
102-
.expectStatus().isOk()
103-
.expectHeader().contentType(MediaTypes.COLLECTION_JSON)
104-
.expectBody(String.class)
99+
this.testClient.get().uri("http://localhost/employees") //
100+
.accept(MediaTypes.COLLECTION_JSON) //
101+
.exchange() //
102+
.expectStatus().isOk() //
103+
.expectHeader().contentType(MediaTypes.COLLECTION_JSON) //
104+
.expectBody(String.class) //
105105

106106
.value(jsonPath("$.collection.version", is("1.0")))
107107
.value(jsonPath("$.collection.href", is("http://localhost/employees")))
@@ -140,19 +140,19 @@ void createNewEmployee() throws Exception {
140140

141141
String specBasedJson = read(new ClassPathResource("spec-part7-adjusted.json", getClass()));
142142

143-
this.testClient.post().uri("http://localhost/employees")
144-
.contentType(MediaTypes.COLLECTION_JSON)
145-
.syncBody(specBasedJson)
146-
.exchange()
147-
.expectStatus().isCreated()
143+
this.testClient.post().uri("http://localhost/employees") //
144+
.contentType(MediaTypes.COLLECTION_JSON) //
145+
.syncBody(specBasedJson) //
146+
.exchange() //
147+
.expectStatus().isCreated() //
148148
.expectHeader().valueEquals(HttpHeaders.LOCATION, "http://localhost/employees/2");
149149

150-
this.testClient.get().uri("http://localhost/employees/2")
151-
.accept(MediaTypes.COLLECTION_JSON)
152-
.exchange()
153-
.expectStatus().isOk()
154-
.expectHeader().contentType(MediaTypes.COLLECTION_JSON)
155-
.expectBody(String.class)
150+
this.testClient.get().uri("http://localhost/employees/2") //
151+
.accept(MediaTypes.COLLECTION_JSON) //
152+
.exchange() //
153+
.expectStatus().isOk() //
154+
.expectHeader().contentType(MediaTypes.COLLECTION_JSON) //
155+
.expectBody(String.class) //
156156

157157
.value(jsonPath("$.collection.version", is("1.0")))
158158
.value(jsonPath("$.collection.href", is("http://localhost/employees/2")))
@@ -190,11 +190,11 @@ WebFluxEmployeeController employeeController() {
190190

191191
@Bean
192192
WebTestClient webTestClient(WebClientConfigurer webClientConfigurer, ApplicationContext ctx) {
193-
194-
return WebTestClient.bindToApplicationContext(ctx).build()
195-
.mutate()
196-
.exchangeStrategies(webClientConfigurer.hypermediaExchangeStrategies())
197-
.build();
193+
194+
return WebTestClient.bindToApplicationContext(ctx).build() //
195+
.mutate() //
196+
.exchangeStrategies(webClientConfigurer.hypermediaExchangeStrategies()) //
197+
.build();
198198
}
199199
}
200200
}

src/test/java/org/springframework/hateoas/mediatype/hal/forms/HalFormsWebFluxIntegrationTest.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,10 @@ void createNewEmployee() throws Exception {
122122
String specBasedJson = MappingUtils.read(new ClassPathResource("new-employee.json", getClass()));
123123

124124
this.testClient.post().uri("http://localhost/employees").contentType(MediaTypes.HAL_FORMS_JSON)
125-
.syncBody(specBasedJson).exchange().expectStatus().isCreated().expectHeader()
126-
.valueEquals(HttpHeaders.LOCATION, "http://localhost/employees/2");
125+
.syncBody(specBasedJson) //
126+
.exchange() //
127+
.expectStatus().isCreated() //
128+
.expectHeader().valueEquals(HttpHeaders.LOCATION, "http://localhost/employees/2");
127129
}
128130

129131
@Configuration

0 commit comments

Comments
 (0)