11package org .baeldung .persistence .dao ;
22
33import java .util .ArrayList ;
4- import java .util .Comparator ;
4+ import java .util .Collections ;
5+ import java .util .Deque ;
6+ import java .util .LinkedList ;
57import java .util .List ;
68import java .util .function .Function ;
79import java .util .stream .Collectors ;
1113import org .springframework .data .jpa .domain .Specification ;
1214import org .springframework .data .jpa .domain .Specifications ;
1315
14- public class GenericSpecificationsBuilder {
16+ public class GenericSpecificationsBuilder < U > {
1517
1618 private final List <SpecSearchCriteria > params ;
1719
1820 public GenericSpecificationsBuilder () {
1921 this .params = new ArrayList <>();
2022 }
2123
22- public final GenericSpecificationsBuilder with (final String key , final String operation , final Object value , final String prefix , final String suffix ) {
24+ public final GenericSpecificationsBuilder < U > with (final String key , final String operation , final Object value , final String prefix , final String suffix ) {
2325 return with (null , key , operation , value , prefix , suffix );
2426 }
2527
26- public final GenericSpecificationsBuilder with (final String precedenceIndicator , final String key , final String operation , final Object value , final String prefix , final String suffix ) {
28+ public final GenericSpecificationsBuilder < U > with (final String precedenceIndicator , final String key , final String operation , final Object value , final String prefix , final String suffix ) {
2729 SearchOperation op = SearchOperation .getSimpleOperation (operation .charAt (0 ));
2830 if (op != null ) {
2931 if (op == SearchOperation .EQUALITY ) // the operation may be complex operation
@@ -44,33 +46,54 @@ public final GenericSpecificationsBuilder with(final String precedenceIndicator,
4446 return this ;
4547 }
4648
47- public < U > Specification <U > build (Function <SpecSearchCriteria , Specification <U >> converter ) {
49+ public Specification <U > build (Function <SpecSearchCriteria , Specification <U >> converter ) {
4850
4951 if (params .size () == 0 ) {
5052 return null ;
5153 }
5254
53- params .sort (Comparator .comparing (SpecSearchCriteria ::isOrPredicate ));
54-
55- final List <Specification <U >> specs = params
56- .stream ()
57- .map (converter )
58- .collect (Collectors .toCollection (ArrayList ::new ));
55+ final List <Specification <U >> specs = params .stream ()
56+ .map (converter )
57+ .collect (Collectors .toCollection (ArrayList ::new ));
5958
6059 Specification <U > result = specs .get (0 );
6160
6261 for (int idx = 1 ; idx < specs .size (); idx ++) {
63- result = params
64- .get (idx )
65- .isOrPredicate ()
66- ? Specifications
67- .where (result )
68- .or (specs .get (idx ))
69- : Specifications
70- .where (result )
71- .and (specs .get (idx ));
62+ result = params .get (idx )
63+ .isOrPredicate ()
64+ ? Specifications .where (result )
65+ .or (specs .get (idx ))
66+ : Specifications .where (result )
67+ .and (specs .get (idx ));
7268 }
7369 return result ;
7470 }
7571
72+ public Specification <U > build (Deque <?> postFixedExprStack , Function <SpecSearchCriteria , Specification <U >> converter ) {
73+
74+ Deque <Specification <U >> specStack = new LinkedList <>();
75+
76+ Collections .reverse ((List <?>) postFixedExprStack );
77+
78+ while (!postFixedExprStack .isEmpty ()) {
79+ Object mayBeOperand = postFixedExprStack .pop ();
80+
81+ if (!(mayBeOperand instanceof String )) {
82+ specStack .push (converter .apply ((SpecSearchCriteria ) mayBeOperand ));
83+ } else {
84+ Specification <U > operand1 = specStack .pop ();
85+ Specification <U > operand2 = specStack .pop ();
86+ if (mayBeOperand .equals (SearchOperation .AND_OPERATOR ))
87+ specStack .push (Specifications .where (operand1 )
88+ .and (operand2 ));
89+ else if (mayBeOperand .equals (SearchOperation .OR_OPERATOR ))
90+ specStack .push (Specifications .where (operand1 )
91+ .or (operand2 ));
92+ }
93+
94+ }
95+ return specStack .pop ();
96+
97+ }
98+
7699}
0 commit comments