Skip to content

Commit

Permalink
Utilizing diamond interface for concise instantiation, leveraging ins…
Browse files Browse the repository at this point in the history
…tanceof pattern matching for enhanced type checking, and incorporating various improvements. Enhances code readability and embraces modern Java features.
  • Loading branch information
achhibi committed Dec 13, 2023
1 parent 0e62f5d commit 64a73d5
Show file tree
Hide file tree
Showing 26 changed files with 58 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public UriToEntityConverter(PersistentEntities entities, RepositoryInvokerFactor
Assert.notNull(invokerFactory, "RepositoryInvokerFactory must not be null");
Assert.notNull(conversionService, "ConversionService must not be null!");

this.convertiblePairs = new HashSet<ConvertiblePair>();
this.convertiblePairs = new HashSet<>();
this.identifierTypes = new HashSet<>();

for (TypeInformation<?> domainType : entities.getManagedTypes()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.data.rest.core;

import java.io.Serial;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
Expand Down Expand Up @@ -42,6 +43,7 @@
*/
public class ValidationErrors extends AbstractPropertyBindingResult {

@Serial
private static final long serialVersionUID = 8141826537389141361L;

private final Object source;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class EntityLookupConfiguration implements EntityLookupRegistrar {

@Override
public <T, ID, R extends Repository<T, ?>> IdMappingRegistrar<T, R> forRepository(Class<R> type) {
return new MappingBuilder<T, ID, R>(type);
return new MappingBuilder<>(type);
}

@Override
Expand Down Expand Up @@ -115,15 +115,15 @@ private MappingBuilder(Class<R> repositoryType, Converter<T, ID> mapping) {
public EntityLookupRegistrar withLookup(Lookup<R, ID> lookup) {

EntityLookupConfiguration.this.lookupInformation
.add((LookupInformation<Object, Object, Repository<? extends Object, ?>>) new LookupInformation<T, ID, R>(
.add((LookupInformation<Object, Object, Repository<? extends Object, ?>>) new LookupInformation<>(
repositoryType, idMapping, lookup));

return EntityLookupConfiguration.this;
}

@Override
public <ID2> LookupRegistrar<T, ID2, R> withIdMapping(Converter<T, ID2> idMapping) {
return new MappingBuilder<T, ID2, R>(repositoryType, idMapping);
return new MappingBuilder<>(repositoryType, idMapping);
}
}

Expand Down Expand Up @@ -195,7 +195,7 @@ public Optional<T> lookupEntity(Object id) {

Object result = lookupInfo.getLookup().lookup(repository, id);

return Optional.class.isInstance(result) ? (Optional<T>) result : Optional.ofNullable((T) result);
return result instanceof Optional optional ? optional : Optional.ofNullable((T) result);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public enum JsonSchemaFormat {
EMAIL, DATE_TIME, HOSTNAME, IPV4, IPV6, URI;

@JsonValue
@Override
public String toString() {
return name().toLowerCase(Locale.US).replaceAll("_", "-");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
*/
public class MetadataConfiguration {

private final Map<Class<?>, JsonSchemaFormat> schemaFormats = new HashMap<Class<?>, JsonSchemaFormat>();
private final Map<Class<?>, Pattern> patterns = new HashMap<Class<?>, Pattern>();
private final Map<Class<?>, JsonSchemaFormat> schemaFormats = new HashMap<>();
private final Map<Class<?>, Pattern> patterns = new HashMap<>();
private boolean omitUnresolvableDescriptionKeys = true;
private boolean alpsEnabled = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ public Map<String, Class<?>> getProjectionsFor(Class<?> sourceType) {
Assert.notNull(sourceType, "Source type must not be null");

Class<?> userType = ProxyUtils.getUserClass(sourceType);
Map<String, ProjectionDefinition> byName = new HashMap<String, ProjectionDefinition>();
Map<String, Class<?>> result = new HashMap<String, Class<?>>();
Map<String, ProjectionDefinition> byName = new HashMap<>();
Map<String, Class<?>> result = new HashMap<>();

for (ProjectionDefinition entry : projectionDefinitions) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class ResourceMapping {
private String rel;
private String path;
private boolean exported = true;
private final Map<String, ResourceMapping> resourceMappings = new HashMap<String, ResourceMapping>();
private final Map<String, ResourceMapping> resourceMappings = new HashMap<>();

public ResourceMapping() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,10 @@
@SuppressWarnings("deprecation")
public class ResourceMappingConfiguration {

private final Map<Class<?>, ResourceMapping> resourceMappings = new HashMap<Class<?>, ResourceMapping>();
private final Map<Class<?>, ResourceMapping> resourceMappings = new HashMap<>();

public ResourceMapping setResourceMappingFor(Class<?> type) {
ResourceMapping rm = resourceMappings.get(type);
if (null == rm) {
rm = new ResourceMapping(type);
resourceMappings.put(type, rm);
}
return rm;
return resourceMappings.computeIfAbsent(type, value -> new ResourceMapping(type));
}

public ResourceMapping getResourceMappingFor(Class<?> type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ public final void onApplicationEvent(RepositoryEvent event) {
onAfterCreate((T) event.getSource());
} else if (event instanceof AfterSaveEvent) {
onAfterSave((T) event.getSource());
} else if (event instanceof BeforeLinkSaveEvent) {
onBeforeLinkSave((T) event.getSource(), ((BeforeLinkSaveEvent) event).getLinked());
} else if (event instanceof AfterLinkSaveEvent) {
onAfterLinkSave((T) event.getSource(), ((AfterLinkSaveEvent) event).getLinked());
} else if (event instanceof BeforeLinkDeleteEvent) {
onBeforeLinkDelete((T) event.getSource(), ((BeforeLinkDeleteEvent) event).getLinked());
} else if (event instanceof AfterLinkDeleteEvent) {
onAfterLinkDelete((T) event.getSource(), ((AfterLinkDeleteEvent) event).getLinked());
} else if (event instanceof BeforeLinkSaveEvent beforeLinkSaveEvent) {
onBeforeLinkSave((T) event.getSource(), beforeLinkSaveEvent.getLinked());
} else if (event instanceof AfterLinkSaveEvent afterLinkSaveEvent) {
onAfterLinkSave((T) event.getSource(), afterLinkSaveEvent.getLinked());
} else if (event instanceof BeforeLinkDeleteEvent beforeLinkDeleteEvent) {
onBeforeLinkDelete((T) event.getSource(), beforeLinkDeleteEvent.getLinked());
} else if (event instanceof AfterLinkDeleteEvent afterLinkDeleteEvent) {
onAfterLinkDelete((T) event.getSource(), afterLinkDeleteEvent.getLinked());
} else if (event instanceof BeforeDeleteEvent) {
onBeforeDelete((T) event.getSource());
} else if (event instanceof AfterDeleteEvent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class AnnotatedEventHandlerInvoker implements ApplicationListener<Reposit
private static final Logger LOG = LoggerFactory.getLogger(AnnotatedEventHandlerInvoker.class);
private static final String PARAMETER_MISSING = "Invalid event handler method %s; At least a single argument is required to determine the domain type for which you are interested in events";

private final MultiValueMap<Class<? extends RepositoryEvent>, EventHandlerMethod> handlerMethods = new LinkedMultiValueMap<Class<? extends RepositoryEvent>, EventHandlerMethod>();
private final MultiValueMap<Class<? extends RepositoryEvent>, EventHandlerMethod> handlerMethods = new LinkedMultiValueMap<>();

@Override
public void onApplicationEvent(RepositoryEvent event) {
Expand All @@ -70,11 +70,11 @@ public void onApplicationEvent(RepositoryEvent event) {
continue;
}

List<Object> parameters = new ArrayList<Object>();
List<Object> parameters = new ArrayList<>();
parameters.add(src);

if (event instanceof LinkedEntityEvent) {
parameters.add(((LinkedEntityEvent) event).getLinked());
if (event instanceof LinkedEntityEvent linkedEntityEvent) {
parameters.add(linkedEntityEvent.getLinked());
}

if (LOG.isDebugEnabled()) {
Expand Down Expand Up @@ -149,7 +149,7 @@ private <T extends Annotation> void inspect(Object handler, Method method, Class
List<EventHandlerMethod> events = handlerMethods.get(eventType);

if (events == null) {
events = new ArrayList<EventHandlerMethod>();
events = new ArrayList<>();
}

if (events.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public ValidatingRepositoryEventListener(ObjectFactory<PersistentEntities> persi
Assert.notNull(persistentEntitiesFactory, "PersistentEntities must not be null");

this.persistentEntitiesFactory = persistentEntitiesFactory;
this.validators = new LinkedMultiValueMap<String, Validator>();
this.validators = new LinkedMultiValueMap<>();
}

/**
Expand All @@ -69,7 +69,7 @@ public ValidatingRepositoryEventListener(ObjectFactory<PersistentEntities> persi
public ValidatingRepositoryEventListener setValidators(Map<String, Collection<Validator>> validators) {

for (Map.Entry<String, Collection<Validator>> entry : validators.entrySet()) {
this.validators.put(entry.getKey(), new ArrayList<Validator>(entry.getValue()));
this.validators.put(entry.getKey(), new ArrayList<>(entry.getValue()));
}

return this;
Expand Down Expand Up @@ -153,6 +153,6 @@ private Errors validate(String event, Object entity) {
private Collection<Validator> getValidatorsForEvent(String event) {

Collection<Validator> validators = this.validators.get(event);
return validators == null ? Collections.<Validator> emptySet() : validators;
return validators == null ? Collections.emptySet() : validators;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ static ConfigurableHttpMethods of(HttpMethods methods) {

Assert.notNull(methods, "HttpMethods must not be null");

if (ConfigurableHttpMethods.class.isInstance(methods)) {
return ConfigurableHttpMethods.class.cast(methods);
if (methods instanceof ConfigurableHttpMethods configurableHttpMethods) {
return configurableHttpMethods;
}

return new ConfigurableHttpMethods(methods.stream().collect(Collectors.toSet()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public HttpMethods getMethodsFor(PersistentProperty<?> property) {
HttpMethods methodsFor = delegate.getMethodsFor(property);
ResourceMapping mapping = resourceMetadata.getMappingFor(property);

if (!PropertyAwareResourceMapping.class.isInstance(mapping)) {
if (! (mapping instanceof PropertyAwareResourceMapping) ) {
return methodsFor;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public HttpMethods getMethodsFor(ResourceType resourceType) {

Assert.notNull(resourceType, "EntityRepresentationModel type must not be null");

Set<HttpMethod> methods = new HashSet<HttpMethod>();
Set<HttpMethod> methods = new HashSet<>();
methods.add(OPTIONS);

switch (resourceType) {
Expand Down Expand Up @@ -110,7 +110,7 @@ public HttpMethods getMethodsFor(PersistentProperty<?> property) {
return HttpMethods.none();
}

Set<HttpMethod> methods = new HashSet<HttpMethod>();
Set<HttpMethod> methods = new HashSet<>();

methods.add(GET);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ HttpMethods filter(ConfigurableHttpMethods methods, PropertyAwareResourceMapping
return property.filter(mapping, methods);
}

private static <T> ComposableFilter<ResourceMetadata, ConfigurableHttpMethods> withTypeFilter(Class<?> type,
private static ComposableFilter<ResourceMetadata, ConfigurableHttpMethods> withTypeFilter(Class<?> type,
ComposableFilter<ResourceMetadata, ConfigurableHttpMethods> function) {

return (metadata, httpMethods) -> //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public static HttpMethods of(Collection<HttpMethod> methods) {
*
* @return
*/
@Override
default Set<HttpMethod> toSet() {
return stream().collect(StreamUtils.toUnmodifiableSet());
}
Expand All @@ -74,6 +75,7 @@ default Set<HttpMethod> toSet() {
* @param method must not be {@literal null}.
* @return
*/
@Override
default HttpMethods and(HttpMethod... method) {
return of(Stream.concat(stream(), Arrays.stream(method)).collect(Collectors.toSet()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class ParametersMetadata implements Iterable<ParameterMetadata> {
*/
public List<String> getParameterNames() {

List<String> names = new ArrayList<String>(parameterMetadata.size());
List<String> names = new ArrayList<>(parameterMetadata.size());

for (ParameterMetadata metadata : parameterMetadata) {
names.add(metadata.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public interface RepositoryDetectionStrategy {
* @since 2.5
* @soundtrack Katinka - Ausverkauf
*/
public static enum RepositoryDetectionStrategies implements RepositoryDetectionStrategy {
enum RepositoryDetectionStrategies implements RepositoryDetectionStrategy {

/**
* Considers all repositories.
Expand Down Expand Up @@ -84,7 +84,7 @@ public boolean isExported(RepositoryMetadata metadata) {
@Override
public boolean isExported(RepositoryMetadata metadata) {
return Modifier.isPublic(metadata.getRepositoryInterface().getModifiers());
};
}
},

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public RepositoryMethodResourceMapping(Method method, ResourceMapping resourceMa

private static final List<ParameterMetadata> discoverParameterMetadata(Method method, String baseRel) {

List<ParameterMetadata> result = new ArrayList<ParameterMetadata>();
List<ParameterMetadata> result = new ArrayList<>();

for (MethodParameter parameter : new MethodParameters(method, PARAM_VALUE).getParameters()) {
if (!IMPLICIT_PARAMETER_TYPES.contains(parameter.getParameterType())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class RepositoryResourceMappings extends PersistentEntitiesResourceMappin

private final Repositories repositories;
private final RepositoryRestConfiguration configuration;
private final Map<Class<?>, SearchResourceMappings> searchCache = new HashMap<Class<?>, SearchResourceMappings>();
private final Map<Class<?>, SearchResourceMappings> searchCache = new HashMap<>();

/**
* Creates a new {@link RepositoryResourceMappings} from the given {@link RepositoryRestConfiguration},
Expand Down Expand Up @@ -103,7 +103,7 @@ public SearchResourceMappings getSearchResourceMappings(Class<?> domainType) {
}

RepositoryInformation repositoryInformation = repositories.getRequiredRepositoryInformation(domainType);
List<MethodResourceMapping> mappings = new ArrayList<MethodResourceMapping>();
List<MethodResourceMapping> mappings = new ArrayList<>();
ResourceMetadata resourceMapping = getMetadataFor(domainType);

if (resourceMapping.isExported()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public SearchResourceMappings(List<MethodResourceMapping> mappings) {

Assert.notNull(mappings, "MethodResourceMappings must not be null");

this.mappings = new HashMap<Path, MethodResourceMapping>(mappings.size());
this.mappings = new HashMap<>(mappings.size());

for (MethodResourceMapping mapping : mappings) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class TypeBasedCollectionResourceMapping implements CollectionResourceMapping {

private final Lazy<Path> path;
private final Lazy<LinkRelation> rel;
private final Lazy<ResourceDescription> description, itemResourceDescription;
private final Lazy<ResourceDescription> description;
private final Lazy<ResourceDescription> itemResourceDescription;

/**
* Creates a new {@link TypeBasedCollectionResourceMapping} using the given type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ protected ResourceMappingUtils() {}
public static String findRel(Class<?> type) {

RestResource anno = findAnnotation(type, RestResource.class);
if (anno != null) {
if (hasText(anno.rel())) {
if (anno != null && hasText(anno.rel())) {
return anno.rel();
}
}

return uncapitalize(type.getSimpleName().replaceAll("Repository", ""));
Expand All @@ -57,10 +55,8 @@ public static String findRel(Method method) {

RestResource anno = findAnnotation(method, RestResource.class);

if (anno != null) {
if (hasText(anno.rel())) {
if (anno != null && hasText(anno.rel()) ) {
return anno.rel();
}
}

return method.getName();
Expand All @@ -85,10 +81,8 @@ public static String findPath(Class<?> type) {

RestResource anno = findAnnotation(type, RestResource.class);

if (anno != null) {
if (hasTextExceptSlash(anno.path())) {
if (anno != null && hasTextExceptSlash(anno.path())) {
return removeLeadingSlash(anno.path());
}
}

return uncapitalize(type.getSimpleName().replaceAll("Repository", ""));
Expand All @@ -98,10 +92,8 @@ public static String findPath(Method method) {

RestResource anno = findAnnotation(method, RestResource.class);

if (anno != null) {
if (hasTextExceptSlash(anno.path())) {
if (anno != null && hasTextExceptSlash(anno.path())) {
return removeLeadingSlash(anno.path());
}
}

return method.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
*
* @author Florent Biville
*/
public class ResourceStringUtils {
public final class ResourceStringUtils {

private ResourceStringUtils(){
}

/**
* Checks whether the given input contains actual text (slash excluded). This is a specializing variant of
Expand Down
Loading

0 comments on commit 64a73d5

Please sign in to comment.