Skip to content

Commit 3a6011e

Browse files
committed
DATACMNS-810 - Extend Query by Example API to typed and untyped specs.
1 parent f6007ec commit 3a6011e

File tree

10 files changed

+1171
-212
lines changed

10 files changed

+1171
-212
lines changed

src/main/java/org/springframework/data/domain/Example.java

+43-20
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@
2929
public class Example<T> {
3030

3131
private final T probe;
32-
private final ExampleSpec<? extends T> exampleSpec;
32+
private final ExampleSpec exampleSpec;
3333

3434
/**
3535
* Create a new {@link Example} including all non-null properties by default.
3636
*
3737
* @param probe The probe to use. Must not be {@literal null}.
3838
*/
3939
@SuppressWarnings("unchecked")
40-
public Example(T probe) {
40+
private Example(T probe) {
4141

4242
Assert.notNull(probe, "Probe must not be null!");
4343

4444
this.probe = probe;
45-
this.exampleSpec = ExampleSpec.of((Class<T>) probe.getClass());
45+
this.exampleSpec = ExampleSpec.untyped();
4646
}
4747

4848
/**
@@ -51,14 +51,46 @@ public Example(T probe) {
5151
* @param probe The probe to use. Must not be {@literal null}.
5252
* @param exampleSpec The example specification to use. Must not be {@literal null}.
5353
*/
54-
public Example(T probe, ExampleSpec<? extends T> exampleSpec) {
54+
private Example(T probe, ExampleSpec exampleSpec) {
5555

5656
Assert.notNull(probe, "Probe must not be null!");
5757

5858
this.probe = probe;
5959
this.exampleSpec = exampleSpec;
6060
}
6161

62+
/**
63+
* Create a new {@link Example} including all non-null properties by default.
64+
*
65+
* @param probe must not be {@literal null}.
66+
* @return
67+
*/
68+
public static <T> Example<T> of(T probe) {
69+
return new Example<T>(probe);
70+
}
71+
72+
/**
73+
* Create a new {@link Example} with a configured {@link ExampleSpec}.
74+
*
75+
* @param probe must not be {@literal null}.
76+
* @param exampleSpec must not be {@literal null}.
77+
* @return
78+
*/
79+
public static <T> Example<T> of(T probe, ExampleSpec exampleSpec) {
80+
return new Example<T>(probe, exampleSpec);
81+
}
82+
83+
/**
84+
* Create a new {@link Example} with a configured {@link TypedExampleSpec}.
85+
*
86+
* @param probe must not be {@literal null}.
87+
* @param exampleSpec must not be {@literal null}.
88+
* @return
89+
*/
90+
public static <T, S extends T> Example<S> of(S probe, TypedExampleSpec<T> exampleSpec) {
91+
return new Example<S>(probe, exampleSpec);
92+
}
93+
6294
/**
6395
* Get the example used.
6496
*
@@ -73,7 +105,7 @@ public T getProbe() {
73105
*
74106
* @return never {@literal null}.
75107
*/
76-
public ExampleSpec<? extends T> getExampleSpec() {
108+
public ExampleSpec getExampleSpec() {
77109
return exampleSpec;
78110
}
79111

@@ -90,24 +122,15 @@ public Class<T> getProbeType() {
90122
}
91123

92124
/**
93-
* Create a new {@link Example} including all non-null properties by default.
94-
*
95-
* @param probe must not be {@literal null}.
96-
* @return
97-
*/
98-
public static <T> Example<T> of(T probe) {
99-
return new Example<T>(probe);
100-
}
101-
102-
/**
103-
* Create a new {@link Example} with a configured {@link ExampleSpec}.
125+
* Get the actual result type for the query. The result type can be different when using {@link TypedExampleSpec}.
104126
*
105-
* @param probe must not be {@literal null}.
106-
* @param exampleSpec must not be {@literal null}.
107127
* @return
128+
* @see ClassUtils#getUserClass(Class)
108129
*/
109-
public static <T> Example<T> of(T probe, ExampleSpec<? extends T> exampleSpec) {
110-
return new Example<T>(probe, exampleSpec);
130+
@SuppressWarnings("unchecked")
131+
public <S extends T> Class<S> getResultType() {
132+
return (Class<S>) (exampleSpec instanceof TypedExampleSpec<?> ? ((TypedExampleSpec<T>) exampleSpec).getType()
133+
: getProbeType());
111134
}
112135

113136
}

0 commit comments

Comments
 (0)