Java – parses user input about search criteria

I'm looking for a way to parse some user input The input should show which searches must be performed and how they must be combined

>1 and 2 > (3 and 2) or 1 > (3 and 2) or (1 and 4) > ((3 or 4) and 1) or 2 > etc

The first example should combine the results of search 1 and 2 in and The second example should combine the results of search 3 and 2 in and mode, and combine the combined results into the results of search 1 in or mode wait.

Any ideas on how to do this?

Solution

Treat your result as an object, which provides and / or similar methods in the following interface:

public interface AndOrCapable<T> {
  public T and(T anOtherResult);
  public T or(T anOtherResult);
}

You can then convert user input to:

Result total = r2.or(r1.and(r3.or(r4))); // your fourth example

This is just to clarify the concept - in your case, you need a dynamic evaluator because you use user input

Therefore, you still need a validator / parser to convert user input into a (Syntax) tree, which will be the model you use to calculate the total

I hope it helps!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>