Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Ron McLeod
Jeanne Boyarsky
Sheriffs:
Paul Clapham
Saloon Keepers:
Tim Holloway
Roland Mueller
Bartenders:
Forum:
Beginning Java
Split expression to Operator, rightListExpression and leftListExpression
peter petros
Greenhorn
Posts: 15
posted 1 year ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi, I have a class as follow :
public class ConsistencyRule { public enum Operator { LT("<"), LE("<="), EQ("="), MT(">"), ME(">="); private final String operationSymbol; Operator(String operationSymbol) { this.operationSymbol = operationSymbol; } public static Operator fromValue(String v) { return valueOf(v); } public String getOperationSymbol() { return operationSymbol; } } final private List<String> leftOperands; final private List<String> rightOperands; final private Operator operator; public ConsistencyRule(List<String> leftOperands, Operator operator, List<String> rightOperands) { this.leftOperands = leftOperands; this.operator = operator; this.rightOperands = rightOperands; } public static class Builder { final private List<String> leftOperands = new ArrayList<>(); final private List<String> rightOperands = new ArrayList<>(); private Operator operator; public Builder operator(Operator operator) { this.operator = operator; return this; } public Builder leftOperand(String operand) { leftOperands.add(operand); return this; } public Builder rightOperand(String operand) { rightOperands.add(operand); return this; } public ConsistencyRule build() { Assert.isTrue(CommonUtils.isNotEmpty(leftOperands)); Assert.isTrue(CommonUtils.isNotEmpty(rightOperands)); Assert.notNull(operator); return new ConsistencyRule(leftOperands, operator, rightOperands); } } }
Until now, I had expressions with the operator on penultimate position ie BBB-CCC-DDD-LE-CC
String rule = "BBB-CCC-DDD-LE-CC"; List<String> ruleSteps = Splitter.on("-").omitEmptyStrings().trimResults() .splitToList(rule); String rightOperand = ruleSteps.get(ruleSteps.size() - 1); Operator operator = Operator.fromValue(ruleSteps.get(ruleSteps.size() - 2)); List<String> leftsOperands = new ArrayList<>(); for (int i = 0; i < ruleSteps.size() - 2; i++) { leftsOperands.add(ruleSteps.get(i)); } ConsistencyRule consistencyRule = new ConsistencyRule(leftsOperands, operator, Arrays.asList(rightOperand));
Now the operator can be on previous positions such as BBB-CCC-DDD-LE-AA-CC
Could you help me please ?
Piet Souris
Bartender
Posts: 5663
214
posted 1 year ago
1
Number of slices to send:
Optional 'thank-you' note:
Send
for instance:
class Tester { public static void main(String... args) { String rule = "LE-BBB-CCC-DDD-CC"; var ruleSteps = Arrays.stream(rule.split("-")).toList(); var operators = Arrays.stream(ConsistencyRule.Operator.values()).map(v -> v.toString()).toList(); var operatorsFound = operators.stream() .filter(ruleSteps::contains) .toList() ; if (operatorsFound.isEmpty() || operatorsFound.size() > 1) throw new RuntimeException("wrong operators") ; var operator = operators.get(0); System.out.println("operator: " + operator); } }
You can also determine the index of the operator, to see if you have decent left- and right-operands.
There are three kinds of actuaries: those who can count, and those who can't.
Norm Radder
Rancher
Posts: 5128
38
posted 1 year ago
1
Number of slices to send:
Optional 'thank-you' note:
Send
Very nice.
Should line 14 be:
var operator = operatorsFound.get(0);
Piet Souris
Bartender
Posts: 5663
214
posted 1 year ago
1
Number of slices to send:
Optional 'thank-you' note:
Send
Indeed. Sorry for the mistake!
There are three kinds of actuaries: those who can count, and those who can't.
peter petros
Greenhorn
Posts: 15
posted 1 year ago
1
Number of slices to send:
Optional 'thank-you' note:
Send
Thanks a lot for your post
Piet Souris
Bartender
Posts: 5663
214
posted 1 year ago
Number of slices to send:
Optional 'thank-you' note:
Send
You're welcome!
There are three kinds of actuaries: those who can count, and those who can't.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
When will instanceof return false
need a postfix for calculation
containsAll is not working
beginner
Using Streams to find max value of a List of Objects
More...