77import java .util .ArrayList ;
88import java .util .List ;
99import java .util .Map ;
10+ import java .util .regex .Matcher ;
11+ import java .util .regex .Pattern ;
1012
1113/**
12- *
14+ * Represents a condition for {@link Transformation#ifCondition()}
1315 */
1416public class Condition {
1517 public static final Map OPERATORS = ObjectUtils .asMap (
@@ -22,35 +24,61 @@ public class Condition {
2224 "&&" , "and" ,
2325 "||" , "or" );
2426
27+ public static final Map PARAMETERS = ObjectUtils .asMap (
28+ "width" , "w" ,
29+ "height" , "h" ,
30+ "aspect_ratio" , "ar" ,
31+ "aspectRatio" , "ar" ,
32+ "page_count" , "pc" ,
33+ "pageCount" , "pc" ,
34+ "face_count" , "fc" ,
35+ "faceCount" , "fc"
36+ );
37+
2538 protected List <String > predicateList = null ;
2639 private Transformation parent = null ;
2740
28- public Condition ( ) {
41+ public Condition () {
2942 predicateList = new ArrayList <String >();
3043
3144 }
32- public Condition ( String conditionStr ) {
45+
46+ /**
47+ * Create a Condition Object. The conditionStr string will be translated to a serialized condition.
48+ *
49+ * @param conditionStr condition in string format
50+ */
51+ public Condition (String conditionStr ) {
3352 this ();
3453 if (conditionStr != null ) {
35- predicateList .add ( literal (conditionStr ));
54+ predicateList .add (literal (conditionStr ));
3655 }
3756 }
3857
3958 private String literal (String conditionStr ) {
40- String [] list = conditionStr .split ("[ _]+" );
41- String [] translated = new String [list .length ];
42- for (int i = 0 , j = 0 ; i < list .length ; i ++) {
43- String s = list [i ];
44- if (OPERATORS .containsKey (s )) {
45- translated [j ++] = (String ) OPERATORS .get (s );
59+
60+ String replacement ;
61+ conditionStr = conditionStr .replaceAll ("[ _]+" , "_" );
62+ Pattern replaceRE = Pattern .compile ("(" + StringUtils .join (PARAMETERS .keySet (), "|" ) + "|[=<>&|!]+)" );
63+ Matcher matcher = replaceRE .matcher (conditionStr );
64+ StringBuffer result = new StringBuffer (conditionStr .length ());
65+ while (matcher .find ()) {
66+ if (OPERATORS .containsKey (matcher .group ())) {
67+ replacement = (String ) OPERATORS .get (matcher .group ());
68+ } else if (PARAMETERS .containsKey (matcher .group ())) {
69+ replacement = (String ) PARAMETERS .get (matcher .group ());
4670 } else {
47- translated [ j ++] = s ;
71+ replacement = matcher . group () ;
4872 }
73+ matcher .appendReplacement (result , replacement );
4974 }
50- return StringUtils .join (translated , "_" );
75+ matcher .appendTail (result );
76+ return result .toString ();
5177 }
78+
5279 public Transformation getParent () { return parent ;}
53- public Condition setParent ( Transformation parent ) {
80+
81+ public Condition setParent (Transformation parent ) {
5482 this .parent = parent ;
5583 return this ;
5684 }
@@ -62,11 +90,11 @@ public String toString() {
6290 return serialize ();
6391 }
6492
65- protected Condition predicate ( String name , String operator , String value ) {
93+ protected Condition predicate (String name , String operator , String value ) {
6694 if (OPERATORS .containsKey (operator )) {
6795 operator = (String ) OPERATORS .get (operator );
6896 }
69- predicateList .add (String .format ("%s_%s_%s" ,name , operator , value ));
97+ predicateList .add (String .format ("%s_%s_%s" , name , operator , value ));
7098 return this ;
7199 }
72100
@@ -80,33 +108,51 @@ public Condition or() {
80108 return this ;
81109 }
82110
111+ /**
112+ * Terminates the definition of the condition and continue with Transformation definition.
113+ * @return the Transformation object this Condition is attached to.
114+ */
83115 public Transformation then () {
84- getParent ().ifCondition ( serialize ());
116+ getParent ().ifCondition (serialize ());
85117 return getParent ();
86118 }
87119
88120 public Condition width (String operator , Object value ) {
89- predicateList .add ("w_" + operator + "_" + value );
121+ predicateList .add ("w_" + operator + "_" + value );
90122 return this ;
91123 }
92124
93125 public Condition height (String operator , Object value ) {
94- predicateList .add ("h_" + operator + "_" + value );
126+ predicateList .add ("h_" + operator + "_" + value );
95127 return this ;
96128 }
97129
98130 public Condition aspectRatio (String operator , Object value ) {
99- predicateList .add ("ar_" + operator + "_" + value );
131+ predicateList .add ("ar_" + operator + "_" + value );
100132 return this ;
101133 }
102134
135+ /**
136+ * @deprecated Use {@link #faceCount(String, Object)} instead
137+ */
103138 public Condition faces (String operator , Object value ) {
104- predicateList .add ("faces_" + operator + "_" + value );
139+ return faceCount (operator , value );
140+ }
141+
142+ public Condition faceCount (String operator , Object value ) {
143+ predicateList .add ("fc_" + operator + "_" + value );
105144 return this ;
106145 }
107146
147+ /**
148+ * @deprecated Use {@link #pageCount(String, Object)} instead
149+ */
108150 public Condition pages (String operator , Object value ) {
109- predicateList .add ("pg_" + operator + "_" + value );
151+ return pageCount (operator , value );
152+ }
153+
154+ public Condition pageCount (String operator , Object value ) {
155+ predicateList .add ("pc_" + operator + "_" + value );
110156 return this ;
111157 }
112158}
0 commit comments