From 67f52d5d700d784fcb8dda47f936a11334ec85db Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Mon, 28 Aug 2017 13:15:36 +0200 Subject: [PATCH] DATACMNS-1141 - Polishing. Remove superfluous null checks in places where state cannot be null. Add nullable annotations to nullable variables. Remove whitespaces. --- .../data/mapping/Association.java | 8 +++-- .../init/Jackson2ResourceReader.java | 11 +++--- .../ResourceReaderRepositoryPopulator.java | 24 ++++++------- .../query/parser/OrderBySource.java | 14 ++++---- .../repository/query/parser/PartTree.java | 34 +++++++++---------- 5 files changed, 47 insertions(+), 44 deletions(-) diff --git a/src/main/java/org/springframework/data/mapping/Association.java b/src/main/java/org/springframework/data/mapping/Association.java index df9ae2342..68a9ff9ee 100644 --- a/src/main/java/org/springframework/data/mapping/Association.java +++ b/src/main/java/org/springframework/data/mapping/Association.java @@ -13,9 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.springframework.data.mapping; +import org.springframework.lang.Nullable; + /** * Value object to capture {@link Association}s. * @@ -26,7 +27,7 @@ package org.springframework.data.mapping; public class Association

> { private final P inverse; - private final P obverse; + private final @Nullable P obverse; /** * Creates a new {@link Association} between the two given {@link PersistentProperty}s. @@ -34,7 +35,7 @@ public class Association

> { * @param inverse * @param obverse */ - public Association(P inverse, P obverse) { + public Association(P inverse, @Nullable P obverse) { this.inverse = inverse; this.obverse = obverse; } @@ -43,6 +44,7 @@ public class Association

> { return inverse; } + @Nullable public P getObverse() { return obverse; } diff --git a/src/main/java/org/springframework/data/repository/init/Jackson2ResourceReader.java b/src/main/java/org/springframework/data/repository/init/Jackson2ResourceReader.java index fb59cb4b3..9618ab1b6 100644 --- a/src/main/java/org/springframework/data/repository/init/Jackson2ResourceReader.java +++ b/src/main/java/org/springframework/data/repository/init/Jackson2ResourceReader.java @@ -33,9 +33,10 @@ import com.fasterxml.jackson.databind.ObjectMapper; /** * A {@link ResourceReader} using Jackson to read JSON into objects. - * + * * @author Oliver Gierke * @author Christoph Strobl + * @author Mark Paluch * @since 1.6 */ public class Jackson2ResourceReader implements ResourceReader { @@ -59,7 +60,7 @@ public class Jackson2ResourceReader implements ResourceReader { /** * Creates a new {@link Jackson2ResourceReader} using the given {@link ObjectMapper}. - * + * * @param mapper */ public Jackson2ResourceReader(@Nullable ObjectMapper mapper) { @@ -69,7 +70,7 @@ public class Jackson2ResourceReader implements ResourceReader { /** * Configures the JSON document's key to lookup the type to instantiate the object. Defaults to * {@link Jackson2ResourceReader#DEFAULT_TYPE_KEY}. - * + * * @param typeKey */ public void setTypeKey(@Nullable String typeKey) { @@ -105,7 +106,7 @@ public class Jackson2ResourceReader implements ResourceReader { /** * Reads the given {@link JsonNode} into an instance of the type encoded in it using the configured type key. - * + * * @param node must not be {@literal null}. * @param classLoader can be {@literal null}. * @return @@ -118,7 +119,7 @@ public class Jackson2ResourceReader implements ResourceReader { throw new IllegalArgumentException(String.format("Could not find type for type key '%s'!", typeKey)); } - String typeName = typeNode == null ? null : typeNode.asText(); + String typeName = typeNode.asText(); Class type = ClassUtils.resolveClassName(typeName, classLoader); return mapper.readerFor(type).readValue(node); diff --git a/src/main/java/org/springframework/data/repository/init/ResourceReaderRepositoryPopulator.java b/src/main/java/org/springframework/data/repository/init/ResourceReaderRepositoryPopulator.java index 7c42ed950..ddd96abd2 100644 --- a/src/main/java/org/springframework/data/repository/init/ResourceReaderRepositoryPopulator.java +++ b/src/main/java/org/springframework/data/repository/init/ResourceReaderRepositoryPopulator.java @@ -36,7 +36,7 @@ import org.springframework.util.Assert; /** * A {@link RepositoryPopulator} using a {@link ResourceReader} to read objects from the configured {@link Resource}s. - * + * * @author Oliver Gierke * @author Christoph Strobl * @since 1.4 @@ -45,16 +45,16 @@ public class ResourceReaderRepositoryPopulator implements RepositoryPopulator, A private static final Logger LOGGER = LoggerFactory.getLogger(ResourceReaderRepositoryPopulator.class); - private final ResourcePatternResolver resolver; private final ResourceReader reader; - private final ClassLoader classLoader; + private final @Nullable ClassLoader classLoader; + private final ResourcePatternResolver resolver; private @Nullable ApplicationEventPublisher publisher; private Collection resources = Collections.emptySet(); /** * Creates a new {@link ResourceReaderRepositoryPopulator} using the given {@link ResourceReader}. - * + * * @param reader must not be {@literal null}. */ public ResourceReaderRepositoryPopulator(ResourceReader reader) { @@ -64,7 +64,7 @@ public class ResourceReaderRepositoryPopulator implements RepositoryPopulator, A /** * Creates a new {@link ResourceReaderRepositoryPopulator} using the given {@link ResourceReader} and * {@link ClassLoader}. - * + * * @param reader must not be {@literal null}. * @param classLoader can be {@literal null}. */ @@ -80,7 +80,7 @@ public class ResourceReaderRepositoryPopulator implements RepositoryPopulator, A /** * Configures the location of the {@link Resource}s to be used to initialize the repositories. - * + * * @param location must not be {@literal null} or empty. * @throws IOException */ @@ -91,22 +91,22 @@ public class ResourceReaderRepositoryPopulator implements RepositoryPopulator, A /** * Configures the {@link Resource}s to be used to initialize the repositories. - * + * * @param resources */ public void setResources(Resource... resources) { this.resources = Arrays.asList(resources); } - /* + /* * (non-Javadoc) * @see org.springframework.context.ApplicationEventPublisherAware#setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) */ - public void setApplicationEventPublisher(@Nullable ApplicationEventPublisher publisher) { + public void setApplicationEventPublisher(ApplicationEventPublisher publisher) { this.publisher = publisher; } - /* + /* * (non-Javadoc) * @see org.springframework.data.repository.init.RepositoryPopulator#initialize() */ @@ -142,7 +142,7 @@ public class ResourceReaderRepositoryPopulator implements RepositoryPopulator, A /** * Reads the given resource into an {@link Object} using the configured {@link ResourceReader}. - * + * * @param resource must not be {@literal null}. * @return */ @@ -156,7 +156,7 @@ public class ResourceReaderRepositoryPopulator implements RepositoryPopulator, A /** * Persists the given {@link Object} using a suitable repository. - * + * * @param object must not be {@literal null}. * @param invokerFactory must not be {@literal null}. */ diff --git a/src/main/java/org/springframework/data/repository/query/parser/OrderBySource.java b/src/main/java/org/springframework/data/repository/query/parser/OrderBySource.java index cbc129f04..5da296ec8 100644 --- a/src/main/java/org/springframework/data/repository/query/parser/OrderBySource.java +++ b/src/main/java/org/springframework/data/repository/query/parser/OrderBySource.java @@ -34,14 +34,14 @@ import org.springframework.util.StringUtils; * Simple helper class to create a {@link Sort} instance from a method name end. It expects the last part of the method * name to be given and supports lining up multiple properties ending with the sorting direction. So the following * method ends are valid: {@code LastnameUsernameDesc}, {@code LastnameAscUsernameDesc}. - * + * * @author Oliver Gierke * @author Christoph Strobl * @author Mark Paluch */ class OrderBySource { - public static OrderBySource EMPTY = new OrderBySource(""); + static OrderBySource EMPTY = new OrderBySource(""); private static final String BLOCK_SPLIT = "(?<=Asc|Desc)(?=\\p{Lu})"; private static final Pattern DIRECTION_SPLIT = Pattern.compile("(.+?)(Asc|Desc)?$"); @@ -53,7 +53,7 @@ class OrderBySource { /** * Creates a new {@link OrderBySource} for the given String clause not doing any checks whether the referenced * property actually exists. - * + * * @param clause must not be {@literal null}. */ OrderBySource(String clause) { @@ -63,7 +63,7 @@ class OrderBySource { /** * Creates a new {@link OrderBySource} for the given clause, checking the property referenced exists on the given * type. - * + * * @param clause must not be {@literal null}. * @param domainClass must not be {@literal null}. */ @@ -98,7 +98,7 @@ class OrderBySource { /** * Creates an {@link Order} instance from the given property source, direction and domain class. If the domain class * is given, we will use it for nested property traversal checks. - * + * * @param propertySource * @param direction must not be {@literal null}. * @param domainClass must not be {@literal null}. @@ -120,10 +120,10 @@ class OrderBySource { /** * Returns the clause as {@link Sort}. - * + * * @return the {@link Sort}. */ - public Sort toSort() { + Sort toSort() { return Sort.by(this.orders); } diff --git a/src/main/java/org/springframework/data/repository/query/parser/PartTree.java b/src/main/java/org/springframework/data/repository/query/parser/PartTree.java index 7eb927a41..281e21423 100644 --- a/src/main/java/org/springframework/data/repository/query/parser/PartTree.java +++ b/src/main/java/org/springframework/data/repository/query/parser/PartTree.java @@ -38,7 +38,7 @@ import org.springframework.util.StringUtils; * Takes a domain class as well to validate that each of the {@link Part}s are referring to a property of the domain * class. The {@link PartTree} can then be used to build queries based on its API instead of parsing the method name for * each query execution. - * + * * @author Oliver Gierke * @author Thomas Darimont * @author Christoph Strobl @@ -76,7 +76,7 @@ public class PartTree implements Streamable { /** * Creates a new {@link PartTree} by parsing the given {@link String}. - * + * * @param source the {@link String} to parse * @param domainClass the domain class to check individual parts against to ensure they refer to a property of the * class @@ -116,7 +116,7 @@ public class PartTree implements Streamable { /** * Returns whether we indicate distinct lookup of entities. - * + * * @return {@literal true} if distinct */ public boolean isDistinct() { @@ -125,7 +125,7 @@ public class PartTree implements Streamable { /** * Returns whether a count projection shall be applied. - * + * * @return */ public boolean isCountProjection() { @@ -144,7 +144,7 @@ public class PartTree implements Streamable { /** * return true if the created {@link PartTree} is meant to be used for delete operation. - * + * * @return * @since 1.8 */ @@ -154,7 +154,7 @@ public class PartTree implements Streamable { /** * Return {@literal true} if the create {@link PartTree} is meant to be used for a query with limited maximal results. - * + * * @return * @since 1.9 */ @@ -164,7 +164,7 @@ public class PartTree implements Streamable { /** * Return the number of maximal results to return or {@literal null} if not restricted. - * + * * @return {@literal null} if not restricted. * @since 1.9 */ @@ -175,7 +175,7 @@ public class PartTree implements Streamable { /** * Returns an {@link Iterable} of all parts contained in the {@link PartTree}. - * + * * @return the iterable {@link Part}s */ public Streamable getParts() { @@ -184,7 +184,7 @@ public class PartTree implements Streamable { /** * Returns all {@link Part}s of the {@link PartTree} of the given {@link Type}. - * + * * @param type * @return */ @@ -194,7 +194,7 @@ public class PartTree implements Streamable { /** * Returns whether the {@link PartTree} contains predicate {@link Part}s. - * + * * @return */ public boolean hasPredicate() { @@ -215,7 +215,7 @@ public class PartTree implements Streamable { /** * Splits the given text at the given keywords. Expects camel-case style to only match concrete keywords and not * derivatives of it. - * + * * @param text the text to split * @param keyword the keyword to split around * @return an array of split items @@ -236,7 +236,7 @@ public class PartTree implements Streamable { /** * Creates a new {@link OrPart}. - * + * * @param source the source to split up into {@literal And} parts in turn. * @param domainClass the domain class to check the resulting {@link Part}s against. * @param alwaysIgnoreCase if always ignoring case @@ -264,7 +264,7 @@ public class PartTree implements Streamable { /** * Represents the subject part of the query. E.g. {@code findDistinctUserByNameOrderByAge} would have the subject * {@code DistinctUser}. - * + * * @author Phil Webb * @author Oliver Gierke * @author Christoph Strobl @@ -317,7 +317,7 @@ public class PartTree implements Streamable { /** * Returns {@literal true} if {@link Subject} matches {@link #DELETE_BY_TEMPLATE}. - * + * * @return * @since 1.8 */ @@ -354,7 +354,7 @@ public class PartTree implements Streamable { /** * Represents the predicate part of the query. - * + * * @author Oliver Gierke * @author Phil Webb */ @@ -376,7 +376,7 @@ public class PartTree implements Streamable { } this.nodes = Arrays.stream(split(parts[0], "Or")) // - .filter(part -> StringUtils.hasText(part)) // + .filter(StringUtils::hasText) // .map(part -> new OrPart(part, domainClass, alwaysIgnoreCase)) // .collect(Collectors.toList()); @@ -396,7 +396,7 @@ public class PartTree implements Streamable { return predicate; } - /* + /* * (non-Javadoc) * @see java.lang.Iterable#iterator() */