Skip to content

Commit 84b9d5a

Browse files
author
RTLcoil
authored
Add support for pow operator in expressions (#198)
1 parent 50099c3 commit 84b9d5a

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public abstract class BaseExpression<T extends BaseExpression> {
2626
"*", "mul",
2727
"/", "div",
2828
"+", "add",
29-
"-", "sub"
29+
"-", "sub",
30+
"^", "pow"
3031
);
3132
public static final Map<String, String> PREDEFINED_VARS = ObjectUtils.asMap(
3233
"width", "w",
@@ -245,6 +246,29 @@ public T sub() {
245246
return (T) this;
246247
}
247248

249+
/**
250+
* Utility shortcut method which invokes on this Expression instance {@link #pow()} method, takes its result and
251+
* invokes {@link #value(Object)} method on it. Effectively, invocation of this shortcut results in
252+
* "to the power of value" sub-expression added to the end of current expression instance.
253+
*
254+
* @param value argument for {@link #value(Object)} call
255+
* @return result of {@link #value(Object)} call
256+
*/
257+
public T pow(Object value) {
258+
return (T) pow().value(value);
259+
}
260+
261+
/**
262+
* Adds "to the power of" sub-expression to the end of the list of already present sub-expressions in this
263+
* expression instance.
264+
*
265+
* @return this expression instance
266+
*/
267+
public T pow() {
268+
expressions.add("pow");
269+
return (T) this;
270+
}
271+
248272
public T value(Object value) {
249273
expressions.add(String.valueOf(value));
250274
return (T) this;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,12 @@ public void testSupportStringInterpolation() {
210210
).fontFamily("Arial").fontSize(18));
211211
assertEquals("c_scale,l_text:Arial_18:$(start)Hello%20$(name)$(ext)%252C%20%24%28no%20%29%20%24%28%20no%29$(end)", t.generate());
212212
}
213+
214+
@Test
215+
public void testShouldSupportPowOperator() {
216+
Transformation t = new Transformation()
217+
.variables(variable("$small", 150), variable("$big", "$small ^ 1.5"));
218+
219+
assertEquals("$small_150,$big_$small_pow_1.5", t.generate());
220+
}
213221
}

0 commit comments

Comments
 (0)