#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.
This commit is contained in:
Greg Turnquist
2019-08-30 15:32:13 -05:00
parent 9d83787a54
commit fff5396a1c
24 changed files with 94 additions and 103 deletions

View File

@@ -114,7 +114,7 @@ public abstract class AffordanceModel {
*/
public interface PayloadMetadata {
public static PayloadMetadata NONE = NoPayloadMetadata.INSTANCE;
PayloadMetadata NONE = NoPayloadMetadata.INSTANCE;
/**
* Returns all properties contained in a payload.
@@ -135,7 +135,7 @@ public abstract class AffordanceModel {
*/
public interface InputPayloadMetadata extends PayloadMetadata {
static InputPayloadMetadata NONE = from(PayloadMetadata.NONE);
InputPayloadMetadata NONE = from(PayloadMetadata.NONE);
static InputPayloadMetadata from(PayloadMetadata metadata) {
@@ -298,7 +298,7 @@ public abstract class AffordanceModel {
*
* @author Oliver Drotbohm
*/
private static enum NoPayloadMetadata implements PayloadMetadata {
private enum NoPayloadMetadata implements PayloadMetadata {
INSTANCE;

View File

@@ -202,8 +202,7 @@ public class Link implements Serializable {
Assert.notNull(affordance, "Affordance must not be null!");
List<Affordance> newAffordances = new ArrayList<>();
newAffordances.addAll(this.affordances);
List<Affordance> newAffordances = new ArrayList<>(this.affordances);
newAffordances.add(affordance);
return withAffordances(newAffordances);
@@ -281,7 +280,7 @@ public class Link implements Serializable {
* @param arguments must not be {@literal null}.
* @return
*/
public Link expand(Map<String, ? extends Object> arguments) {
public Link expand(Map<String, ?> arguments) {
return new Link(template.expand(arguments).toString(), getRel());
}

View File

@@ -457,7 +457,7 @@ public class Links implements Iterable<Link> {
*
* @author Oliver Drotbohm
*/
public static enum MergeMode {
public enum MergeMode {
/**
* Skips to add the same links on merge. Multiple links with the same link relation might appear.
@@ -472,6 +472,6 @@ public class Links implements Iterable<Link> {
/**
* Replaces existing links with the same link relation.
*/
REPLACE_BY_REL;
REPLACE_BY_REL
}
}

View File

@@ -41,7 +41,7 @@ import com.fasterxml.jackson.annotation.JsonValue;
class StringLinkRelation implements LinkRelation, Serializable {
private static final long serialVersionUID = -3904935345545567957L;
private static final Map<String, StringLinkRelation> CACHE = new ConcurrentHashMap<String, StringLinkRelation>(256);
private static final Map<String, StringLinkRelation> CACHE = new ConcurrentHashMap<>(256);
@NonNull String relation;

View File

@@ -267,7 +267,7 @@ public class UriTemplate implements Iterable<TemplateVariable>, Serializable {
* @param parameters must not be {@literal null}.
* @return
*/
public URI expand(Map<String, ? extends Object> parameters) {
public URI expand(Map<String, ?> parameters) {
if (TemplateVariables.NONE.equals(variables)) {
return URI.create(baseUri);
@@ -373,8 +373,7 @@ public class UriTemplate implements Iterable<TemplateVariable>, Serializable {
} else if (value instanceof Map) {
((Map<Object, Object>) value).entrySet() //
.forEach(it -> builder.queryParam(it.getKey().toString(), it.getValue()));
((Map<Object, Object>) value).forEach((key, value1) -> builder.queryParam(key.toString(), value1));
} else {

View File

@@ -126,7 +126,7 @@ public class Traverson {
return DEFAULTS.getHttpMessageConverters(Arrays.asList(mediaTypes));
}
private static final RestOperations createDefaultTemplate(List<MediaType> mediaTypes) {
private static RestOperations createDefaultTemplate(List<MediaType> mediaTypes) {
RestTemplate template = new RestTemplate();
template.setMessageConverters(DEFAULTS.getHttpMessageConverters(mediaTypes));

View File

@@ -53,11 +53,6 @@ class HypermediaConfigurationImportSelector implements ImportSelector {
List<MediaTypeConfigurationProvider> configurationProviders = SpringFactoriesLoader.loadFactories(
MediaTypeConfigurationProvider.class, HypermediaConfigurationImportSelector.class.getClassLoader());
// No additional filtering needed.
if (types.isEmpty()) {
return configurationProviders.toArray(new String[0]);
}
// Filter the ones supporting the given media types
return configurationProviders.stream() //
.filter(it -> it.supportsAny(types)) //

View File

@@ -28,7 +28,7 @@ import org.springframework.lang.Nullable;
*/
public interface MessageResolver {
public static final MessageResolver DEFAULTS_ONLY = DefaultOnlyMessageResolver.INSTANCE;
MessageResolver DEFAULTS_ONLY = DefaultOnlyMessageResolver.INSTANCE;
/**
* Resolve the given {@link MessageSourceResolvable}. Return {@literal null} if no message was found.
@@ -45,7 +45,7 @@ public interface MessageResolver {
* @param messageSource can be {@literal null}.
* @return will never be {@literal null}.
*/
public static MessageResolver of(@Nullable MessageSource messageSource) {
static MessageResolver of(@Nullable MessageSource messageSource) {
return messageSource == null //
? DefaultOnlyMessageResolver.INSTANCE //

View File

@@ -876,7 +876,7 @@ class Jackson2CollectionJsonModule extends SimpleModule {
return collection.getItems().stream() //
.map(CollectionJsonItem::withOwnSelfLink) //
.<Object> map(it -> isResource //
.map(it -> isResource //
? new EntityModel<>(it.toRawData(rootType), it.getLinks()) //
: it.toRawData(rootType)) //
.collect(Collectors.collectingAndThen(Collectors.toList(), it -> finalizer.apply(it, links)));

View File

@@ -31,7 +31,7 @@ import org.springframework.hateoas.Links;
*/
public interface CurieProvider {
public static CurieProvider NONE = new CurieProvider() {
CurieProvider NONE = new CurieProvider() {
@Override
public HalLinkRelation getNamespacedRelFrom(Link link) {
@@ -44,7 +44,7 @@ public interface CurieProvider {
}
@Override
public Collection<? extends Object> getCurieInformation(Links links) {
public Collection<?> getCurieInformation(Links links) {
throw new UnsupportedOperationException();
}
};
@@ -75,5 +75,5 @@ public interface CurieProvider {
* @param links the {@link Links} that have been added to the response so far.
* @return must not be {@literal null}.
*/
Collection<? extends Object> getCurieInformation(Links links);
Collection<?> getCurieInformation(Links links);
}

View File

@@ -98,7 +98,7 @@ public class DefaultCurieProvider implements CurieProvider {
* @see org.springframework.hateoas.hal.CurieProvider#getCurieInformation()
*/
@Override
public Collection<? extends Object> getCurieInformation(Links links) {
public Collection<?> getCurieInformation(Links links) {
return curies.entrySet().stream() //
.map(it -> new Curie(it.getKey(), getCurieHref(it.getKey(), it.getValue()))) //

View File

@@ -80,7 +80,7 @@ class HalTraversonDefaults implements TraversonDefaults {
*
* @return
*/
private static final HttpMessageConverter<?> getHalConverter(List<MediaType> halFlavours) {
private static HttpMessageConverter<?> getHalConverter(List<MediaType> halFlavours) {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new Jackson2HalModule());

View File

@@ -182,8 +182,7 @@ public class Jackson2HalModule extends SimpleModule {
if (!skipCuries && prefixingRequired && curiedLinkPresent) {
ArrayList<Object> curies = new ArrayList<>();
curies.addAll(curieProvider.getCurieInformation(Links.of(links)));
ArrayList<Object> curies = new ArrayList<>(curieProvider.getCurieInformation(Links.of(links)));
sortedLinks.put(HalLinkRelation.CURIES, curies);
}
@@ -525,7 +524,7 @@ public class Jackson2HalModule extends SimpleModule {
private JsonSerializer<Object> getOrLookupSerializerFor(Object value, SerializerProvider provider)
throws JsonMappingException {
Class<? extends Object> type = value.getClass();
Class<?> type = value.getClass();
JsonSerializer<Object> serializer = serializers.get(type);
if (serializer == null) {

View File

@@ -171,11 +171,11 @@ public class HalFormsDocument<T> {
}
public HalFormsDocument<T> withPageMetadata(@Nullable PageMetadata metadata) {
return new HalFormsDocument<T>(attributes, entity, entities, embedded, metadata, links, templates);
return new HalFormsDocument<>(attributes, entity, entities, embedded, metadata, links, templates);
}
private HalFormsDocument<T> withEntity(@Nullable T entity) {
return new HalFormsDocument<T>(attributes, entity, entities, embedded, pageMetadata, links, templates);
return new HalFormsDocument<>(attributes, entity, entities, embedded, pageMetadata, links, templates);
}
/**

View File

@@ -206,7 +206,7 @@ class HalFormsTemplateBuilder {
codes.add(globalCode);
return codes.toArray(new String[codes.size()]);
return codes.toArray(new String[0]);
}
}
}

View File

@@ -847,7 +847,7 @@ public class Jackson2UberModule extends SimpleModule {
List<LinkRelation> rel = item.getRel();
if (rel != null) {
item.getLinks().forEach(resourceLinks::add);
resourceLinks.addAll(item.getLinks());
} else {
// Primitive type

View File

@@ -127,7 +127,7 @@ public interface EntityLinks extends Plugin<Class<?>> {
* @param extractor the extractor to use to derive an identifier from the given entity.
* @return
*/
default <T> TypedEntityLinks<T> forType(Function<T, ? extends Object> extractor) {
default <T> TypedEntityLinks<T> forType(Function<T, ?> extractor) {
return new TypedEntityLinks<>(extractor, this);
}

View File

@@ -73,10 +73,10 @@ public interface LinkRelationProvider extends Plugin<LookupContext> {
*/
@RequiredArgsConstructor(staticName = "of", access = AccessLevel.PRIVATE)
@EqualsAndHashCode
static class LookupContext {
class LookupContext {
private enum ResourceType {
ITEM, COLLECTION;
ITEM, COLLECTION
}
private final @NonNull @Getter Class<?> type;

View File

@@ -35,7 +35,7 @@ import org.springframework.util.Assert;
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
public class TypedEntityLinks<T> {
private final @NonNull Function<T, ? extends Object> identifierExtractor;
private final @NonNull Function<T, ?> identifierExtractor;
private final @NonNull EntityLinks entityLinks;
/**
@@ -73,7 +73,7 @@ public class TypedEntityLinks<T> {
private final Class<T> type;
private final EntityLinks delegate;
ExtendedTypedEntityLinks(Function<T, ? extends Object> identifierExtractor, EntityLinks delegate, Class<T> type) {
ExtendedTypedEntityLinks(Function<T, ?> identifierExtractor, EntityLinks delegate, Class<T> type) {
super(identifierExtractor, delegate);

View File

@@ -30,7 +30,7 @@ import org.springframework.web.util.UriTemplate;
*/
public class UriTemplateFactory {
private static final Map<String, UriTemplate> CACHE = new ConcurrentReferenceHashMap<String, UriTemplate>();
private static final Map<String, UriTemplate> CACHE = new ConcurrentReferenceHashMap<>();
/**
* Returns the the {@link UriTemplate} for the given mapping.

View File

@@ -142,7 +142,7 @@ public class RepresentationModelProcessorHandlerMethodReturnValueHandler impleme
return rootLinksAsHeaders ? HeaderLinksResponseEntity.wrap(newBody) : newBody;
}
HttpEntity<RepresentationModel<?>> entity = null;
HttpEntity<RepresentationModel<?>> entity;
if (originalValue instanceof ResponseEntity) {
ResponseEntity<?> source = (ResponseEntity<?>) originalValue;