Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
S → SS
S → (S)
S → ()
S → key KEY
KEY → operator OPERATOR
OPERATOR → value VALUE
OPERATOR → value
VALUE → value VALUE
VALUE → value
VALUE → and AND
VALUE → or OR
AND → S
OR → S

Psuedocode

Criteria

...

A wrapper around an in-order collection of symbols. A Criteria is generated from the completion of a state machine that comprises a builder.

Code Block
public class Criteria {
 
	// Static Methods
	public static Builder builder();  {
	// Variables
	privatereturn finalnew List<Symbol> symbolsStartState(new Criteria());
 	}
	// Constructorprivate final List<Symbol> symbols = Lists.newArrayList();
	protected Criteria();
 
	// Methods
	protected void addSymbol(Symbol symbol); 
 
	public static final Builder {
 
		// Static methods
		protected static Builder newBuilder();
 
		// Variables
		private final Criteria criteria;
		private State state;
 
		// Constructor
		private Builder(Criteria criteria);
		 
	}
 
}

...

 
}

State

The base class and marker for any valid state for the builder. Each state is passed the current Criteria and holds a reference. For any method called from the state, a symbol is added to the Criteria or the Criteria is returned.

Code Block
abstract class State {
	Criteria criteria;
} 

StartState

The state that marks the beginning of a new Criteria.

Code Block
class StartState extends State {
	StartState group(Criteria criteria);
	KeyState key(String key);
	Criteria build();
}

KeyState

The state that expects the next symbol/token to be a key specification.

Code Block
class KeyState extends State {
	OperatorState operator(Operator operator);
}

OperatorState

The state that expects the next symbol/token to be a value specification.

Code Block
class OperatorState extends State {
	ValueState value(Object value);
}

ValueState

The state that expects the current symbol/token to be the last or the next symbol/token to be value or conjunction specification.

Code Block
class ValueState extends State {
	ValueState value(Object value);
	StartState and();
	StartState or();
	Criteria build();
}

Symbol

A marker interface to indicate an object that can be included in a Criteria

...

Code Block
class Key implements Symbol {
	
	public String getKey();
}

...

Code Block
class Value implements Symbol {
	
	public Object getValue();
}

State

A state 

Code Block
abstract class State {
 
	Criteria criteria; 
}

 

 

Code Block
interface Symbol { } //marker interface 
 
KeySymbol implements Symbol {
	String key;
}
 
ValueSymbol implements Symbol {
	Object value; 
}
 
OperatorSymbol implements Symbol {
	Operator operator;
}

...

// When the State creates this symbol it must convert the object to a TObject so that it can be passed over the wire via Thrift
	public TObject getValue();
}

Order of Operations

Given validly formed grouped criteria, we must process everything with the correct order of operations. For this, we should use the Shunting-yard algorithm. I've written this algorithm in PHP so we should port it to Java.

...