Skip to content

Commit 64d44e0

Browse files
author
Amir Tocker
committed
Rename conditional parameters faces and pages to faceCount and pageCount
1 parent dfc62f4 commit 64d44e0

File tree

3 files changed

+94
-38
lines changed

3 files changed

+94
-38
lines changed

cloudinary-core/src/main/java/com/cloudinary/Transformation.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class Transformation {
2020
protected static boolean defaultIsResponsive = false;
2121
protected static Object defaultDPR = null;
2222

23-
private static final Map DEFAULT_RESPONSIVE_WIDTH_TRANSFORMATION = ObjectUtils.asMap("width", "auto", "crop", "limit");
23+
private static final Map<String, String> DEFAULT_RESPONSIVE_WIDTH_TRANSFORMATION = ObjectUtils.asMap("width", "auto", "crop", "limit");
2424
protected static Map responsiveWidthTransformation = null;
2525
private static final Pattern RANGE_VALUE_RE = Pattern.compile("^((?:\\d+\\.)?\\d+)([%pP])?$");
2626
private static final Pattern RANGE_RE = Pattern.compile("^(\\d+\\.)?\\d+[%pP]?\\.\\.(\\d+\\.)?\\d+[%pP]?$");
@@ -348,19 +348,29 @@ public Transformation responsiveWidth(boolean value) {
348348
return param("responsive_width", value);
349349
}
350350

351+
/**
352+
* Start defining a condition, which will be completed with a call {@link Condition#then()}
353+
* @return condition
354+
*/
351355
public Condition ifCondition() {
352356
return new Condition().setParent(this);
353357
}
358+
359+
/**
360+
* Define a conditional transformation defined by the condition string
361+
* @param condition a condition string
362+
* @return the transformation for chaining
363+
*/
354364
public Transformation ifCondition(String condition) {
355365
return param("if", condition);
356366
}
367+
357368
public Transformation ifElse() {
358369
chain();
359370
return param("if", "else");
360371
}
361372

362373
public Transformation endIf() {
363-
364374
chain();
365375
int transSize = this.transformations.size();
366376
for (int i = transSize - 1; i >= 0; i--) {

cloudinary-core/src/main/java/com/cloudinary/transformation/Condition.java

Lines changed: 67 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
import java.util.ArrayList;
88
import java.util.List;
99
import 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
*/
1416
public 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
}

cloudinary-core/src/test/java/com/cloudinary/TransformationTest.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void withLiteral() throws Exception {
4545

4646
@Test
4747
public void literalWithSpaces() throws Exception {
48-
Map map = ObjectUtils.asMap("if", "w < 200", "crop", "fill", "height", 120, "width", 80);
48+
Map map = ObjectUtils.asMap("if", "width < 200", "crop", "fill", "height", 120, "width", 80);
4949
List<Map> list = new ArrayList<Map>();
5050
list.add(map);
5151
Transformation transformation = new Transformation(list);
@@ -98,34 +98,34 @@ public void chainedConditions() throws Exception {
9898
assertEquals("should translate operators", "if_ar_gt_3:4_and_w_lte_100_or_w_gt_200,c_scale,w_50", transformation.toString());
9999
transformation = new Transformation().ifCondition().aspectRatio(">", "3:4").and().width("<=", 100).or().width(">", 200).then().width(50).crop("scale");
100100
assertEquals("should translate operators", "if_ar_gt_3:4_and_w_lte_100_or_w_gt_200,c_scale,w_50", transformation.toString());
101-
transformation = new Transformation().ifCondition().aspectRatio(">=", "3:4").and().pages(">=", 100).or().pages("!=", 0).then().width(50).crop("scale");
102-
assertEquals("should translate operators", "if_ar_gte_3:4_and_pg_gte_100_or_pg_ne_0,c_scale,w_50", transformation.toString());
101+
transformation = new Transformation().ifCondition().aspectRatio(">=", "3:4").and().pageCount(">=", 100).or().pageCount("!=", 0).then().width(50).crop("scale");
102+
assertEquals("should translate operators", "if_ar_gte_3:4_and_pc_gte_100_or_pc_ne_0,c_scale,w_50", transformation.toString());
103103

104104
}
105105

106106
@Test
107107
public void shouldSupportAndTranslateOperators() {
108108

109109
String allOperators =
110-
"if_" +
111-
"w_eq_0_and" +
112-
"_w_ne_0_or" +
113-
"_w_lt_0_and" +
114-
"_w_gt_0_and" +
115-
"_w_lte_0_and" +
116-
"_w_gte_0" +
110+
"if_" +
111+
"w_eq_0_and" +
112+
"_h_ne_0_or" +
113+
"_ar_lt_0_and" +
114+
"_pc_gt_0_and" +
115+
"_fc_lte_0_and" +
116+
"_w_gte_0" +
117117
",e_grayscale";
118118
assertEquals("should support and translate operators: '=', '!=', '<', '>', '<=', '>=', '&&', '||'",
119119
allOperators, new Transformation().ifCondition()
120120
.width("=", 0).and()
121-
.width("!=", 0).or()
122-
.width("<", 0).and()
123-
.width(">", 0).and()
124-
.width("<=", 0).and()
121+
.height("!=", 0).or()
122+
.aspectRatio("<", 0).and()
123+
.pageCount(">", 0).and()
124+
.faceCount("<=", 0).and()
125125
.width(">=", 0)
126126
.then().effect("grayscale").toString());
127127

128-
assertEquals(allOperators, new Transformation().ifCondition("w = 0 && w != 0 || w < 0 and w > 0 and w <= 0 and w >= 0")
128+
assertEquals(allOperators, new Transformation().ifCondition("w = 0 && height != 0 || aspectRatio < 0 and pageCount > 0 and faceCount <= 0 and width >= 0")
129129
.effect("grayscale")
130130
.toString());
131131
}

0 commit comments

Comments
 (0)