Skip to content

Commit 2569cf8

Browse files
committed
spring-projects#57 - Polishing.
Replaced custom builder with Lombok generated one. Turned Lombok into a value object as a side effect. Removed obsolete toString() method. Extended copyright clauses wehere missing. Reduced visibility of repository extension.
1 parent 9cffcef commit 2569cf8

File tree

7 files changed

+32
-98
lines changed

7 files changed

+32
-98
lines changed

solr/example/src/main/java/example/springdata/solr/Product.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717

1818
import java.util.List;
1919

20-
import lombok.Data;
20+
import lombok.Builder;
21+
import lombok.Value;
2122

2223
import org.springframework.data.annotation.Id;
2324
import org.springframework.data.geo.Point;
@@ -26,12 +27,14 @@
2627
import org.springframework.data.solr.repository.Score;
2728

2829
/**
29-
* Document representing a Product and its attributes matching the fields defined in the <a
30+
* Document representing a {@link Product} and its attributes matching the fields defined in the <a
3031
* href="http://localhost:8983/solr/collection1/schema">example solr schema</a>.
3132
*
3233
* @author Christoph Strobl
34+
* @author Oliver Gierke
3335
*/
34-
@Data
36+
@Value
37+
@Builder
3538
@SolrDocument(solrCoreName = "collection1")
3639
public class Product {
3740

@@ -43,10 +46,4 @@ public class Product {
4346
private @Indexed boolean inStock;
4447
private @Indexed Integer popularity;
4548
private @Score Float score;
46-
47-
@Override
48-
public String toString() {
49-
return "Product [id=" + id + ", name=" + name + ", category=" + category + ", location=" + location + ", inStock="
50-
+ inStock + ", score=" + score + "]";
51-
}
5249
}

solr/example/src/main/java/example/springdata/solr/ProductRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2015 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.

solr/example/src/main/java/example/springdata/solr/ProductRepositoryCustom.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2015 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.
@@ -23,7 +23,7 @@
2323
*
2424
* @author Christoph Strobl
2525
*/
26-
public interface ProductRepositoryCustom {
26+
interface ProductRepositoryCustom {
2727

2828
/**
2929
* Use a {@link Cursor} to scroll through documents in index. <br />

solr/example/src/main/java/example/springdata/solr/ProductRepositoryImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2015 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.
@@ -25,8 +25,9 @@
2525
* Implementation of {@link ProductRepositoryCustom}.
2626
*
2727
* @author Christoph Strobl
28+
* @author Oliver Gierke
2829
*/
29-
public class ProductRepositoryImpl implements ProductRepositoryCustom {
30+
class ProductRepositoryImpl implements ProductRepositoryCustom {
3031

3132
@Autowired SolrTemplate solrTemplate;
3233

solr/example/src/test/java/example/springdata/solr/AdvancedSolrRepositoryTests.java

Lines changed: 14 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
import static org.springframework.data.solr.core.query.Criteria.*;
2121
import static org.springframework.data.solr.core.query.ExistsFunction.*;
2222

23-
import java.util.ArrayList;
2423
import java.util.Arrays;
25-
import java.util.List;
2624

2725
import org.junit.ClassRule;
2826
import org.junit.Test;
@@ -44,6 +42,7 @@
4442

4543
/**
4644
* @author Christoph Strobl
45+
* @author Oliver Gierke
4746
*/
4847
@RunWith(SpringJUnit4ClassRunner.class)
4948
@ContextConfiguration
@@ -57,22 +56,18 @@ static class Config extends SolrTestConfiguration {
5756
@Override
5857
protected void doInitTestData(CrudRepository<Product, String> repository) {
5958

60-
Product playstation = new ProductBuilder().withId("id-1").named("Playstation")
61-
.withDescription("The Sony playstation was the top selling gaming system in 1994.").withPopularity(5).build();
62-
63-
Product playstation2 = new ProductBuilder().withId("id-2").named("Playstation Two")
64-
.withDescription("Playstation two is the successor of playstation in 2000.").build();
65-
66-
Product superNES = new ProductBuilder().withId("id-3").named("Super Nintendo").withPopularity(3).build();
67-
68-
Product nintendo64 = new ProductBuilder().withId("id-4").named("N64").withDescription("Nintendo 64")
69-
.withPopularity(2).build();
59+
Product playstation = Product.builder().id("id-1").name("Playstation")
60+
.description("The Sony playstation was the top selling gaming system in 1994.").popularity(5).build();
61+
Product playstation2 = Product.builder().id("id-2").name("Playstation Two")
62+
.description("Playstation two is the successor of playstation in 2000.").build();
63+
Product superNES = Product.builder().id("id-3").name("Super Nintendo").popularity(3).build();
64+
Product nintendo64 = Product.builder().id("id-4").name("N64").description("Nintendo 64").popularity(2).build();
7065

7166
repository.save(Arrays.asList(playstation, playstation2, superNES, nintendo64));
7267
}
7368
}
7469

75-
@Autowired ProductRepository repo;
70+
@Autowired ProductRepository repository;
7671
@Autowired SolrOperations operations;
7772

7873
/**
@@ -82,7 +77,7 @@ protected void doInitTestData(CrudRepository<Product, String> repository) {
8277
@Test
8378
public void annotationBasedHighlighting() {
8479

85-
HighlightPage<Product> products = repo.findByDescriptionStartingWith("play", new PageRequest(0, 10));
80+
HighlightPage<Product> products = repository.findByDescriptionStartingWith("play", new PageRequest(0, 10));
8681

8782
products.getHighlighted().forEach(
8883
entry -> entry.getHighlights().forEach(
@@ -96,9 +91,7 @@ public void annotationBasedHighlighting() {
9691
*/
9792
@Test
9893
public void annotationBasedBoosting() {
99-
100-
repo.findTop10ByNameOrDescription("Nintendo", "Nintendo") //
101-
.forEach(System.out::println);
94+
repository.findTop10ByNameOrDescription("Nintendo", "Nintendo").forEach(System.out::println);
10295
}
10396

10497
/**
@@ -109,9 +102,9 @@ public void annotationBasedBoosting() {
109102
@Test
110103
public void influcenceScoreWithFunctions() {
111104

112-
operations.queryForPage(new SimpleQuery(where(exists("popularity"))).addProjectionOnFields("*", "score"),
113-
Product.class) //
114-
.forEach(System.out::println);
105+
Query query = new SimpleQuery(where(exists("popularity"))).addProjectionOnFields("*", "score");
106+
107+
operations.queryForPage(query, Product.class).forEach(System.out::println);
115108
}
116109

117110
/**
@@ -121,8 +114,7 @@ public void influcenceScoreWithFunctions() {
121114
@Test
122115
public void useRealtimeGetToReadUncommitedDocuments() throws InterruptedException {
123116

124-
Product xbox = new ProductBuilder().withId("id-5").named("XBox").withDescription("Microsift XBox")
125-
.withPopularity(2).build();
117+
Product xbox = Product.builder().id("id-5").name("XBox").description("Microsift XBox").popularity(2).build();
126118
Query query = new SimpleQuery(where("id").is(xbox.getId()));
127119

128120
// add document but delay commit for 3 seconds
@@ -138,52 +130,4 @@ public void useRealtimeGetToReadUncommitedDocuments() throws InterruptedExceptio
138130
Thread.sleep(3010);
139131
assertThat(operations.queryForObject(query, Product.class), notNullValue());
140132
}
141-
142-
static class ProductBuilder {
143-
144-
private Product product;
145-
146-
public ProductBuilder() {
147-
this.product = new Product();
148-
}
149-
150-
public ProductBuilder withId(String id) {
151-
this.product.setId(id);
152-
return this;
153-
}
154-
155-
public ProductBuilder named(String name) {
156-
this.product.setName(name);
157-
return this;
158-
}
159-
160-
public ProductBuilder withDescription(String description) {
161-
this.product.setDescription(description);
162-
return this;
163-
}
164-
165-
public ProductBuilder withPopularity(Integer popularity) {
166-
this.product.setPopularity(popularity);
167-
return this;
168-
}
169-
170-
public ProductBuilder inCategory(String category) {
171-
172-
List<String> categories = new ArrayList<>();
173-
categories.add(category);
174-
175-
if (this.product.getCategory() == null) {
176-
categories.addAll(this.product.getCategory());
177-
}
178-
179-
this.product.setCategory(categories);
180-
return this;
181-
182-
}
183-
184-
public Product build() {
185-
return this.product;
186-
}
187-
188-
}
189133
}

solr/example/src/test/java/example/springdata/solr/BasicSolrRepositoryTests.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2015 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.
@@ -33,24 +33,21 @@ public class BasicSolrRepositoryTests {
3333

3434
public static @ClassRule RequiresSolrServer requiresRunningServer = RequiresSolrServer.onLocalhost();
3535

36-
@Autowired ProductRepository repo;
36+
@Autowired ProductRepository repository;
3737

3838
/**
3939
* Finds all entries using a single request.
4040
*/
4141
@Test
4242
public void findAll() {
43-
repo.findAll()//
44-
.forEach(System.out::println);
43+
repository.findAll().forEach(System.out::println);
4544
}
4645

4746
/**
4847
* Pages through all entries using cursor marks. Have a look at the Solr console output to see iteration steps.
4948
*/
5049
@Test
5150
public void findAllUsingDeepPagination() {
52-
repo.findAllUsingCursor()//
53-
.forEachRemaining(System.out::println);
51+
repository.findAllUsingCursor().forEachRemaining(System.out::println);
5452
}
55-
5653
}

solr/example/src/test/java/example/springdata/solr/SolrTestConfiguration.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2015 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.
@@ -59,12 +59,7 @@ public void initWithTestData() {
5959
protected void doInitTestData(CrudRepository<Product, String> repository) {
6060

6161
for (int i = 0; i < 100; i++) {
62-
63-
Product p = new Product();
64-
p.setId("p-" + i);
65-
p.setName("foobar");
66-
67-
repository.save(p);
62+
repository.save(Product.builder().id("p-" + i).name("foobar").build());
6863
}
6964
}
7065
}

0 commit comments

Comments
 (0)