Skip to content

Commit ba5fe89

Browse files
authored
CAMEL-22868: camel-core - Add not variant to startsWith and endsWith operator in simple language (#20862)
1 parent cfc5e8b commit ba5fe89

File tree

6 files changed

+95
-18
lines changed

6 files changed

+95
-18
lines changed

components/camel-csimple-joor/src/test/java/org/apache/camel/language/csimple/joor/OriginalSimpleOperatorTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,20 @@ public void testStartsWith() {
761761
assertPredicate("${in.body} startsWith 'Hi'", false);
762762
}
763763

764+
@Test
765+
public void testNotStartsWith() {
766+
exchange.getIn().setBody("Hello there");
767+
assertPredicate("${in.body} !startsWith 'Bye'", true);
768+
assertPredicate("${in.body} !startsWith 'Hello'", false);
769+
assertPredicate("${in.body} !startsWith 'B'", true);
770+
assertPredicate("${in.body} !startsWith 'H'", false);
771+
assertPredicate("${in.body} !startsWith 'Bye there'", true);
772+
assertPredicate("${in.body} !startsWith 'Hello there'", false);
773+
assertPredicate("${in.body} !startsWith 'Hello ther'", false);
774+
assertPredicate("${in.body} !startsWith 'ello there'", true);
775+
assertPredicate("${in.body} !startsWith 'Hi'", true);
776+
}
777+
764778
@Test
765779
public void testEndsWith() {
766780
exchange.getIn().setBody("Hello there");
@@ -778,6 +792,19 @@ public void testEndsWith() {
778792
assertPredicate("${in.body} endsWith 'Hi'", false);
779793
}
780794

795+
@Test
796+
public void testNotEndsWith() {
797+
exchange.getIn().setBody("Hello there");
798+
assertPredicate("${in.body} !endsWith 'B'", true);
799+
assertPredicate("${in.body} !endsWith 'world'", true);
800+
assertPredicate("${in.body} !endsWith 'there'", false);
801+
assertPredicate("${in.body} !endsWith 're'", false);
802+
assertPredicate("${in.body} !endsWith ' there'", false);
803+
assertPredicate("${in.body} !endsWith 'Hello there'", false);
804+
assertPredicate("${in.body} !endsWith 'Hello ther'", true);
805+
assertPredicate("${in.body} !endsWith 'Hi'", true);
806+
}
807+
781808
@Override
782809
protected String getLanguageName() {
783810
return "csimple";

core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -629,16 +629,17 @@ is a set of four values with an empty value and then the three medals.
629629
|!is |For matching if the left-hand side type is not an instance of the value.
630630

631631
|range |For matching if the left-hand side is within a range of values defined
632-
as numbers: `from..to`..
632+
as numbers: `from..to`.
633633

634634
|!range |For matching if the left-hand side is not within a range of values
635-
defined as numbers: `from..to`. .
635+
defined as numbers: `from..to`.
636636

637-
|startsWith |For testing if the left-hand side string starts
638-
with the right-hand string.
637+
|startsWith |For testing if the left-hand side string starts with the right-hand string.
638+
|!startsWith |For testing if the left-hand side string does not start with the right-hand string.
639+
640+
|endsWith |For testing if the left-hand side string ends with the right-hand string.
641+
|!endsWith |For testing if the left-hand side string does not end with the right-hand string.
639642

640-
|endsWith |For testing if the left-hand side string ends with
641-
the right-hand string.
642643
|===
643644

644645
And the following unary operators can be used:

core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleTokenizer.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
public final class SimpleTokenizer {
2828

2929
// keep this number in sync with tokens list
30-
private static final int NUMBER_OF_TOKENS = 47;
30+
private static final int NUMBER_OF_TOKENS = 49;
3131

3232
private static final SimpleTokenType[] KNOWN_TOKENS = new SimpleTokenType[NUMBER_OF_TOKENS];
3333

@@ -79,22 +79,24 @@ public final class SimpleTokenizer {
7979
KNOWN_TOKENS[36] = new SimpleTokenType(TokenType.binaryOperator, "!range");
8080
KNOWN_TOKENS[37] = new SimpleTokenType(TokenType.binaryOperator, "range");
8181
KNOWN_TOKENS[38] = new SimpleTokenType(TokenType.binaryOperator, "startsWith");
82-
KNOWN_TOKENS[39] = new SimpleTokenType(TokenType.binaryOperator, "starts with"); // deprecated
83-
KNOWN_TOKENS[40] = new SimpleTokenType(TokenType.binaryOperator, "endsWith");
84-
KNOWN_TOKENS[41] = new SimpleTokenType(TokenType.binaryOperator, "ends with"); // deprecated
82+
KNOWN_TOKENS[39] = new SimpleTokenType(TokenType.binaryOperator, "starts with");
83+
KNOWN_TOKENS[40] = new SimpleTokenType(TokenType.binaryOperator, "!startsWith");
84+
KNOWN_TOKENS[41] = new SimpleTokenType(TokenType.binaryOperator, "endsWith");
85+
KNOWN_TOKENS[42] = new SimpleTokenType(TokenType.binaryOperator, "ends with");
86+
KNOWN_TOKENS[43] = new SimpleTokenType(TokenType.binaryOperator, "!endsWith");
8587

8688
// unary operators
87-
KNOWN_TOKENS[42] = new SimpleTokenType(TokenType.unaryOperator, "++");
88-
KNOWN_TOKENS[43] = new SimpleTokenType(TokenType.unaryOperator, "--");
89+
KNOWN_TOKENS[44] = new SimpleTokenType(TokenType.unaryOperator, "++");
90+
KNOWN_TOKENS[45] = new SimpleTokenType(TokenType.unaryOperator, "--");
8991

9092
// logical operators
91-
KNOWN_TOKENS[44] = new SimpleTokenType(TokenType.logicalOperator, "&&");
92-
KNOWN_TOKENS[45] = new SimpleTokenType(TokenType.logicalOperator, "||");
93+
KNOWN_TOKENS[46] = new SimpleTokenType(TokenType.logicalOperator, "&&");
94+
KNOWN_TOKENS[47] = new SimpleTokenType(TokenType.logicalOperator, "||");
9395

9496
//binary operator
9597
// it is added as the last item because unary -- has the priority
9698
// if unary not found it is highly possible - operator is run into.
97-
KNOWN_TOKENS[46] = new SimpleTokenType(TokenType.minusValue, "-");
99+
KNOWN_TOKENS[48] = new SimpleTokenType(TokenType.minusValue, "-");
98100
}
99101

100102
private SimpleTokenizer() {

core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/BinaryExpression.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,14 @@ public Expression createExpression(CamelContext camelContext, String expression)
127127
return createRangeExpression(camelContext, expression, leftExp, rightExp);
128128
} else if (operator == BinaryOperatorType.STARTS_WITH) {
129129
return createExpression(camelContext, leftExp, rightExp, PredicateBuilder.startsWith(leftExp, rightExp));
130+
} else if (operator == BinaryOperatorType.NOT_STARTS_WITH) {
131+
return createExpression(camelContext, leftExp, rightExp,
132+
PredicateBuilder.not(PredicateBuilder.startsWith(leftExp, rightExp)));
130133
} else if (operator == BinaryOperatorType.ENDS_WITH) {
131134
return createExpression(camelContext, leftExp, rightExp, PredicateBuilder.endsWith(leftExp, rightExp));
135+
} else if (operator == BinaryOperatorType.NOT_ENDS_WITH) {
136+
return createExpression(camelContext, leftExp, rightExp,
137+
PredicateBuilder.not(PredicateBuilder.endsWith(leftExp, rightExp)));
132138
}
133139

134140
throw new SimpleParserException("Unknown binary operator " + operator, token.getIndex());
@@ -351,8 +357,12 @@ private String doCreateCode(CamelContext camelContext, String expression) throws
351357
return "!range(exchange, " + leftExp + ", " + rightExp + ")";
352358
} else if (operator == BinaryOperatorType.STARTS_WITH) {
353359
return "startsWith(exchange, " + leftExp + ", " + rightExp + ")";
360+
} else if (operator == BinaryOperatorType.NOT_STARTS_WITH) {
361+
return "!startsWith(exchange, " + leftExp + ", " + rightExp + ")";
354362
} else if (operator == BinaryOperatorType.ENDS_WITH) {
355363
return "endsWith(exchange, " + leftExp + ", " + rightExp + ")";
364+
} else if (operator == BinaryOperatorType.NOT_ENDS_WITH) {
365+
return "!endsWith(exchange, " + leftExp + ", " + rightExp + ")";
356366
}
357367

358368
throw new SimpleParserException("Unknown binary operator " + operator, token.getIndex());

core/camel-core-languages/src/main/java/org/apache/camel/language/simple/types/BinaryOperatorType.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ public enum BinaryOperatorType {
4545
RANGE,
4646
NOT_RANGE,
4747
STARTS_WITH,
48-
ENDS_WITH;
48+
NOT_STARTS_WITH,
49+
ENDS_WITH,
50+
NOT_ENDS_WITH;
4951

5052
private static final Logger LOG = LoggerFactory.getLogger(BinaryOperatorType.class);
5153

@@ -115,6 +117,10 @@ public static BinaryOperatorType asOperator(String text) {
115117
} else if ("ends with".equals(text)) {
116118
LOG.warn("Simple operator `ends with` is deprecated, use `endsWith` instead");
117119
return ENDS_WITH;
120+
} else if ("!startsWith".equals(text)) {
121+
return NOT_STARTS_WITH;
122+
} else if ("!endsWith".equals(text)) {
123+
return NOT_ENDS_WITH;
118124
}
119125
throw new IllegalArgumentException("Operator not supported: " + text);
120126
}
@@ -162,8 +168,12 @@ public static String getOperatorText(BinaryOperatorType operator) {
162168
return "!range";
163169
} else if (operator == STARTS_WITH) {
164170
return "startsWith";
171+
} else if (operator == NOT_STARTS_WITH) {
172+
return "!startsWith";
165173
} else if (operator == ENDS_WITH) {
166174
return "endsWith";
175+
} else if (operator == NOT_ENDS_WITH) {
176+
return "!endsWith";
167177
}
168178
return "";
169179
}
@@ -264,9 +274,9 @@ public static ParameterType[] supportedParameterTypes(BinaryOperatorType operato
264274
return new ParameterType[] { ParameterType.LiteralWithFunction, ParameterType.Function };
265275
} else if (operator == NOT_RANGE) {
266276
return new ParameterType[] { ParameterType.LiteralWithFunction, ParameterType.Function };
267-
} else if (operator == STARTS_WITH) {
277+
} else if (operator == STARTS_WITH || operator == NOT_STARTS_WITH) {
268278
return null;
269-
} else if (operator == ENDS_WITH) {
279+
} else if (operator == ENDS_WITH || operator == NOT_ENDS_WITH) {
270280
return null;
271281
}
272282
return null;

core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleOperatorTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,20 @@ public void testStartsWithTextAsNumeric() {
783783
assertPredicate("${in.body} starts with 01234", false);
784784
}
785785

786+
@Test
787+
public void testNotStartsWith() {
788+
exchange.getIn().setBody("Hello there");
789+
assertPredicate("${in.body} !startsWith 'Bye'", true);
790+
assertPredicate("${in.body} !startsWith 'Hello'", false);
791+
assertPredicate("${in.body} !startsWith 'B'", true);
792+
assertPredicate("${in.body} !startsWith 'H'", false);
793+
assertPredicate("${in.body} !startsWith 'Bye there'", true);
794+
assertPredicate("${in.body} !startsWith 'Hello there'", false);
795+
assertPredicate("${in.body} !startsWith 'Hello ther'", false);
796+
assertPredicate("${in.body} !startsWith 'ello there'", true);
797+
assertPredicate("${in.body} !startsWith 'Hi'", true);
798+
}
799+
786800
@Test
787801
public void testEndsWith() {
788802
exchange.getIn().setBody("Hello there");
@@ -800,6 +814,19 @@ public void testEndsWith() {
800814
assertPredicate("${in.body} endsWith 'Hi'", false);
801815
}
802816

817+
@Test
818+
public void testNotEndsWith() {
819+
exchange.getIn().setBody("Hello there");
820+
assertPredicate("${in.body} !endsWith 'B'", true);
821+
assertPredicate("${in.body} !endsWith 'world'", true);
822+
assertPredicate("${in.body} !endsWith 'there'", false);
823+
assertPredicate("${in.body} !endsWith 're'", false);
824+
assertPredicate("${in.body} !endsWith ' there'", false);
825+
assertPredicate("${in.body} !endsWith 'Hello there'", false);
826+
assertPredicate("${in.body} !endsWith 'Hello ther'", true);
827+
assertPredicate("${in.body} !endsWith 'Hi'", true);
828+
}
829+
803830
@Override
804831
protected String getLanguageName() {
805832
return "simple";

0 commit comments

Comments
 (0)