1
1
/*
2
- * Copyright 2008-2012 the original author or authors.
2
+ * Copyright 2008-2013 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
37
37
*/
38
38
public class PartTree implements Iterable <OrPart > {
39
39
40
- private static final Pattern PREFIX_TEMPLATE = Pattern .compile ("^(find|read|get)(\\ p{Lu}.*?)??By" );
40
+ private static final Pattern PREFIX_TEMPLATE = Pattern .compile ("^(find|read|get|count )(\\ p{Lu}.*?)??By" );
41
41
private static final String KEYWORD_TEMPLATE = "(%s)(?=\\ p{Lu})" ;
42
42
43
43
/**
@@ -67,7 +67,7 @@ public PartTree(String source, Class<?> domainClass) {
67
67
this .subject = new Subject (null );
68
68
this .predicate = new Predicate (source , domainClass );
69
69
} else {
70
- this .subject = new Subject (matcher .group (2 ));
70
+ this .subject = new Subject (matcher .group (0 ));
71
71
this .predicate = new Predicate (source .substring (matcher .group ().length ()), domainClass );
72
72
}
73
73
}
@@ -94,13 +94,21 @@ public Sort getSort() {
94
94
/**
95
95
* Returns whether we indicate distinct lookup of entities.
96
96
*
97
- * @return <tt> true<tt> if distinct
97
+ * @return {@literal true} if distinct
98
98
*/
99
99
public boolean isDistinct () {
100
-
101
100
return subject .isDistinct ();
102
101
}
103
102
103
+ /**
104
+ * Returns whether a count projection shall be applied.
105
+ *
106
+ * @return
107
+ */
108
+ public Boolean isCountProjection () {
109
+ return subject .isCountProjection ();
110
+ }
111
+
104
112
/**
105
113
* Returns an {@link Iterable} of all parts contained in the {@link PartTree}.
106
114
*
@@ -141,7 +149,7 @@ public String toString() {
141
149
142
150
OrderBySource orderBySource = predicate .getOrderBySource ();
143
151
return String .format ("%s%s" , StringUtils .collectionToDelimitedString (predicate .nodes , " or " ),
144
- ( orderBySource == null ? "" : " " + orderBySource ) );
152
+ orderBySource == null ? "" : " " + orderBySource );
145
153
}
146
154
147
155
/**
@@ -198,15 +206,24 @@ public String toString() {
198
206
* {@code DistinctUser}.
199
207
*
200
208
* @author Phil Webb
209
+ * @author Oliver Gierke
201
210
*/
202
211
private static class Subject {
203
212
204
213
private static final String DISTINCT = "Distinct" ;
214
+ private static final Pattern COUNT_BY_TEMPLATE = Pattern .compile ("^count(\\ p{Lu}.*?)??By" );
205
215
206
- private boolean distinct ;
216
+ private final boolean distinct ;
217
+ private final boolean count ;
207
218
208
219
public Subject (String subject ) {
209
- this .distinct = (subject == null ? false : subject .contains (DISTINCT ));
220
+
221
+ this .distinct = subject == null ? false : subject .contains (DISTINCT );
222
+ this .count = subject == null ? false : COUNT_BY_TEMPLATE .matcher (subject ).find ();
223
+ }
224
+
225
+ public boolean isCountProjection () {
226
+ return count ;
210
227
}
211
228
212
229
public boolean isDistinct () {
@@ -269,4 +286,5 @@ public OrderBySource getOrderBySource() {
269
286
return orderBySource ;
270
287
}
271
288
}
289
+
272
290
}
0 commit comments