title: Java: Build logical expressions and then validate them
body: <p>I have a little functionality where I need to determine whether a rule that a user is creating is syntactically valid.</p>
  <p>That being said the structure of what I'm building are like the following:</p>
  <ul>
  <li>1 == 1</li>
  <li>1 + 1 == 1</li>
  <li>1 + 1 == 1 OR 1 == 1</li>
  <li>More combinations of the above examples</li>
  </ul>
  <p>These expressions are saved in a string variable, e.g:</p>
  <pre><code>String expression = &quot;&quot;;
  
  while(items.hasNext())
  {
      String currentItem = items.next();
      expression += currentItem.value();
  }
  
  //Check if the expression is valid
  </code></pre>
  <p><strong>VALID EXPRESSIONS</strong></p>
  <p>Valid expressions are the ones that have a logic operator (&lt;, &lt;=, ==, =&gt;, &gt;) and the output would be true or false (doesn't matter which)</p>
  <ul>
  <li>1 == 1</li>
  <li>1 &lt; 2</li>
  <li>1 == 1 OR 1 &lt; 4</li>
  <li>4 == 9 OR 9 == 3</li>
  </ul>
  <p><strong>INVALID EXPRESSIONS</strong></p>
  <p>Invalid expressions are the ones that doesn't have the proper structure in order to determine whether that expression is true or false.</p>
  <ul>
  <li>1</li>
  <li>1+1</li>
  <li>1===</li>
  <li>==1</li>
  <li>1</li>
  <li>11 (that is number then number)</li>
  </ul>
  <p><strong>NOTE</strong></p>
  <p>I've tried using</p>
  <blockquote>
  <p>Boolean.valueOf(String)</p>
  <p>Boolean.parse(String)</p>
  <p>Other types of Boolean methods</p>
  </blockquote>