Skip to content

Commit 5f3bacf

Browse files
committed
DATAES-328 - Reduce null usage, use PageRequest.of factory method.
1 parent 4c7dd49 commit 5f3bacf

9 files changed

+39
-34
lines changed

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ public <T> void delete(DeleteQuery deleteQuery, Class<T> clazz) {
689689
: 10000l;
690690

691691
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(deleteQuery.getQuery()).withIndices(indexName)
692-
.withTypes(typeName).withPageable(new PageRequest(0, pageSize)).build();
692+
.withTypes(typeName).withPageable(PageRequest.of(0, pageSize)).build();
693693

694694
String scrollId = scan(searchQuery, scrollTimeInMillis, true);
695695

src/main/java/org/springframework/data/elasticsearch/core/query/NativeSearchQueryBuilder.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
* @author Rizwan Idrees
3434
* @author Mohsin Husen
3535
* @author Artur Konczak
36+
* @author Mark Paluch
3637
*/
3738
public class NativeSearchQueryBuilder {
3839

@@ -43,7 +44,7 @@ public class NativeSearchQueryBuilder {
4344
private List<FacetRequest> facetRequests = new ArrayList<>();
4445
private List<AbstractAggregationBuilder> aggregationBuilders = new ArrayList<>();
4546
private HighlightBuilder.Field[] highlightFields;
46-
private Pageable pageable;
47+
private Pageable pageable = Pageable.NONE;
4748
private String[] indices;
4849
private String[] types;
4950
private String[] fields;
@@ -141,9 +142,7 @@ public NativeSearchQueryBuilder withSearchType(SearchType searchType) {
141142

142143
public NativeSearchQuery build() {
143144
NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(queryBuilder, filterBuilder, sortBuilders, highlightFields);
144-
if (pageable != null) {
145-
nativeSearchQuery.setPageable(pageable);
146-
}
145+
nativeSearchQuery.setPageable(pageable);
147146

148147
if (indices != null) {
149148
nativeSearchQuery.addIndices(indices);

src/main/java/org/springframework/data/elasticsearch/core/query/Query.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2016 the original author or authors.
2+
* Copyright 2013-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,11 +28,12 @@
2828
*
2929
* @author Rizwan Idrees
3030
* @author Mohsin Husen
31+
* @author Mark Paluch
3132
*/
3233
public interface Query {
3334

34-
public static final int DEFAULT_PAGE_SIZE = 10;
35-
public static final Pageable DEFAULT_PAGE = new PageRequest(0, DEFAULT_PAGE_SIZE);
35+
int DEFAULT_PAGE_SIZE = 10;
36+
Pageable DEFAULT_PAGE = PageRequest.of(0, DEFAULT_PAGE_SIZE);
3637

3738
/**
3839
* restrict result to entries on given page. Corresponds to the 'start' and 'rows' parameter in elasticsearch

src/main/java/org/springframework/data/elasticsearch/repository/cdi/ElasticsearchRepositoryExtension.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ void afterBeanDiscovery(@Observes AfterBeanDiscovery afterBeanDiscovery, BeanMan
7171
private <T> CdiRepositoryBean<T> createRepositoryBean(Class<T> repositoryType, Set<Annotation> qualifiers,
7272
BeanManager beanManager) {
7373

74-
Bean<ElasticsearchOperations> elasticsearchOperationsBean = this.elasticsearchOperationsMap.get(qualifiers);
75-
76-
if (elasticsearchOperationsBean == null) {
74+
if (!this.elasticsearchOperationsMap.containsKey(qualifiers)) {
7775
throw new UnsatisfiedResolutionException(String.format("Unable to resolve a bean for '%s' with qualifiers %s.",
7876
ElasticsearchOperations.class.getName(), qualifiers));
7977
}
8078

79+
Bean<ElasticsearchOperations> elasticsearchOperationsBean = this.elasticsearchOperationsMap.get(qualifiers);
80+
8181
return new ElasticsearchRepositoryBean<>(elasticsearchOperationsBean, qualifiers, repositoryType, beanManager,
8282
Optional.ofNullable(getCustomImplementationDetector()));
8383
}

src/main/java/org/springframework/data/elasticsearch/repository/query/ElasticsearchPartQuery.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616
package org.springframework.data.elasticsearch.repository.query;
1717

1818
import org.springframework.data.domain.PageRequest;
19+
import org.springframework.data.domain.Pageable;
1920
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
2021
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
2122
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
@@ -33,6 +34,7 @@
3334
* @author Rizwan Idrees
3435
* @author Mohsin Husen
3536
* @author Kevin Leturc
37+
* @author Mark Paluch
3638
*/
3739
public class ElasticsearchPartQuery extends AbstractElasticsearchRepositoryQuery {
3840

@@ -58,16 +60,16 @@ public Object execute(Object[] parameters) {
5860
return elasticsearchOperations.queryForPage(query, queryMethod.getEntityInformation().getJavaType());
5961
} else if (queryMethod.isStreamQuery()) {
6062
Class<?> entityType = queryMethod.getEntityInformation().getJavaType();
61-
if (query.getPageable() == null) {
62-
query.setPageable(new PageRequest(0, 20));
63+
if (query.getPageable() == Pageable.NONE) {
64+
query.setPageable(PageRequest.of(0, 20));
6365
}
6466

6567
return StreamUtils.createStreamFromIterator((CloseableIterator<Object>) elasticsearchOperations.stream(query, entityType));
6668

6769
} else if (queryMethod.isCollectionQuery()) {
6870
if (accessor.getPageable() == null) {
6971
int itemCount = (int) elasticsearchOperations.count(query, queryMethod.getEntityInformation().getJavaType());
70-
query.setPageable(new PageRequest(0, Math.max(1, itemCount)));
72+
query.setPageable(PageRequest.of(0, Math.max(1, itemCount)));
7173
} else {
7274
query.setPageable(accessor.getPageable());
7375
}
@@ -83,9 +85,9 @@ private Object countOrGetDocumentsForDelete(CriteriaQuery query, ParametersParam
8385
Object result = null;
8486

8587
if (queryMethod.isCollectionQuery()) {
86-
if (accessor.getPageable() == null) {
88+
if (accessor.getPageable() == Pageable.NONE) {
8789
int itemCount = (int) elasticsearchOperations.count(query, queryMethod.getEntityInformation().getJavaType());
88-
query.setPageable(new PageRequest(0, Math.max(1, itemCount)));
90+
query.setPageable(PageRequest.of(0, Math.max(1, itemCount)));
8991
} else {
9092
query.setPageable(accessor.getPageable());
9193
}

src/main/java/org/springframework/data/elasticsearch/repository/query/ElasticsearchQueryMethod.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2015 the original author or authors.
2+
* Copyright 2013-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,14 +22,14 @@
2222
import org.springframework.data.projection.ProjectionFactory;
2323
import org.springframework.data.repository.core.RepositoryMetadata;
2424
import org.springframework.data.repository.query.QueryMethod;
25-
import org.springframework.util.StringUtils;
2625

2726
/**
2827
* ElasticsearchQueryMethod
2928
*
3029
* @author Rizwan Idrees
3130
* @author Mohsin Husen
3231
* @author Oliver Gierke
32+
* @author Mark Paluch
3333
*/
3434
public class ElasticsearchQueryMethod extends QueryMethod {
3535

@@ -45,7 +45,6 @@ public boolean hasAnnotatedQuery() {
4545
}
4646

4747
public String getAnnotatedQuery() {
48-
String query = (String) AnnotationUtils.getValue(queryAnnotation, "value");
49-
return StringUtils.hasText(query) ? query : null;
48+
return (String) AnnotationUtils.getValue(queryAnnotation, "value");
5049
}
5150
}

src/main/java/org/springframework/data/elasticsearch/repository/query/ElasticsearchStringQuery.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.util.regex.Pattern;
2020

2121
import org.springframework.core.convert.support.GenericConversionService;
22+
import org.springframework.data.domain.Pageable;
2223
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
2324
import org.springframework.data.elasticsearch.core.convert.DateTimeConverters;
2425
import org.springframework.data.elasticsearch.core.query.StringQuery;
@@ -30,6 +31,7 @@
3031
*
3132
* @author Rizwan Idrees
3233
* @author Mohsin Husen
34+
* @author Mark Paluch
3335
*/
3436
public class ElasticsearchStringQuery extends AbstractElasticsearchRepositoryQuery {
3537

@@ -50,8 +52,8 @@ public class ElasticsearchStringQuery extends AbstractElasticsearchRepositoryQue
5052
}
5153
}
5254

53-
public ElasticsearchStringQuery(ElasticsearchQueryMethod queryMethod,
54-
ElasticsearchOperations elasticsearchOperations, String query) {
55+
public ElasticsearchStringQuery(ElasticsearchQueryMethod queryMethod, ElasticsearchOperations elasticsearchOperations,
56+
String query) {
5557
super(queryMethod, elasticsearchOperations);
5658
Assert.notNull(query, "Query cannot be empty");
5759
this.query = query;
@@ -65,7 +67,7 @@ public Object execute(Object[] parameters) {
6567
stringQuery.setPageable(accessor.getPageable());
6668
return elasticsearchOperations.queryForPage(stringQuery, queryMethod.getEntityInformation().getJavaType());
6769
} else if (queryMethod.isCollectionQuery()) {
68-
if (accessor.getPageable() != null) {
70+
if (accessor.getPageable() != Pageable.NONE) {
6971
stringQuery.setPageable(accessor.getPageable());
7072
}
7173
return elasticsearchOperations.queryForList(stringQuery, queryMethod.getEntityInformation().getJavaType());

src/main/java/org/springframework/data/elasticsearch/repository/support/AbstractElasticsearchRepository.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public Iterable<T> findAll() {
106106
if (itemCount == 0) {
107107
return new PageImpl<>(Collections.<T>emptyList());
108108
}
109-
return this.findAll(new PageRequest(0, Math.max(1, itemCount)));
109+
return this.findAll(PageRequest.of(0, Math.max(1, itemCount)));
110110
}
111111

112112
@Override
@@ -122,7 +122,7 @@ public Iterable<T> findAll(Sort sort) {
122122
return new PageImpl<>(Collections.<T>emptyList());
123123
}
124124
SearchQuery query = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
125-
.withPageable(new PageRequest(0, itemCount, sort)).build();
125+
.withPageable(PageRequest.of(0, itemCount, sort)).build();
126126
return elasticsearchOperations.queryForPage(query, getEntityClass());
127127
}
128128

@@ -188,7 +188,7 @@ public Iterable<T> search(QueryBuilder query) {
188188
if (count == 0) {
189189
return new PageImpl<>(Collections.<T>emptyList());
190190
}
191-
searchQuery.setPageable(new PageRequest(0, count));
191+
searchQuery.setPageable(PageRequest.of(0, count));
192192
return elasticsearchOperations.queryForPage(searchQuery, getEntityClass());
193193
}
194194

src/main/java/org/springframework/data/elasticsearch/repository/support/MappingElasticsearchEntityInformation.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818
import java.io.Serializable;
1919
import java.util.Optional;
2020

21-
import org.slf4j.Logger;
22-
import org.slf4j.LoggerFactory;
2321
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
2422
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
2523
import org.springframework.data.repository.core.support.PersistentEntityInformation;
24+
import org.springframework.util.Assert;
2625

2726
/**
2827
* Elasticsearch specific implementation of
@@ -39,17 +38,20 @@
3938
public class MappingElasticsearchEntityInformation<T, ID extends Serializable>
4039
extends PersistentEntityInformation<T, ID> implements ElasticsearchEntityInformation<T, ID> {
4140

42-
private static final Logger logger = LoggerFactory.getLogger(MappingElasticsearchEntityInformation.class);
4341
private final ElasticsearchPersistentEntity<T> entityMetadata;
4442
private final String indexName;
4543
private final String type;
4644

4745
public MappingElasticsearchEntityInformation(ElasticsearchPersistentEntity<T> entity) {
48-
this(entity, null, null);
46+
this(entity, entity.getIndexName(), entity.getIndexType());
4947
}
5048

5149
public MappingElasticsearchEntityInformation(ElasticsearchPersistentEntity<T> entity, String indexName, String type) {
5250
super(entity);
51+
52+
Assert.notNull(indexName, "IndexName must not be null!");
53+
Assert.notNull(type, "IndexType must not be null!");
54+
5355
this.entityMetadata = entity;
5456
this.indexName = indexName;
5557
this.type = type;
@@ -68,12 +70,12 @@ public String getIdAttribute() {
6870

6971
@Override
7072
public String getIndexName() {
71-
return indexName != null ? indexName : entityMetadata.getIndexName();
73+
return indexName;
7274
}
7375

7476
@Override
7577
public String getType() {
76-
return type != null ? type : entityMetadata.getIndexType();
78+
return type;
7779
}
7880

7981
@Override

0 commit comments

Comments
 (0)