package com.growthbeat.analytics.query;

import java.util.Set;

import org.apache.commons.lang3.math.NumberUtils;

import com.google.common.collect.Sets;

public enum Operator {
	equal, greater, less, greater_equal, less_equal, begin_with, exist, in;

	private static final Set<Operator> NUMBER_COMPARABLE_OPERATORS = Sets.immutableEnumSet(equal, greater, less, greater_equal, less_equal);

	public boolean evaluate(String string, String targetString) {

		if (NUMBER_COMPARABLE_OPERATORS.contains(this) && NumberUtils.isNumber(targetString)) {
			if (!NumberUtils.isNumber(string))
				return false;
			return evaluate(NumberUtils.toDouble(string), NumberUtils.toDouble(targetString));
		}

		switch (this) {
		case equal:
			return string.equals(targetString);
		case greater:
			return (string.compareTo(targetString) > 0);
		case less:
			return (string.compareTo(targetString) < 0);
		case greater_equal:
			return (string.compareTo(targetString) >= 0);
		case less_equal:
			return (string.compareTo(targetString) <= 0);
		case begin_with:
			return string.startsWith(targetString);
		case exist:
			return (string != null);
		case in:
			// TODO implement
			throw new IllegalStateException();
		default:
			throw new IllegalStateException();
		}

	}

	public boolean evaluate(double number, double targetNumber) {

		switch (this) {
		case equal:
			return (number == targetNumber);
		case greater:
			return (number > targetNumber);
		case less:
			return (number < targetNumber);
		case greater_equal:
			return (number >= targetNumber);
		case less_equal:
			return (number <= targetNumber);
		case begin_with:
		case exist:
		case in:
		default:
			throw new IllegalStateException();
		}

	}

}

@@ -232,16 +232,22 @@
 in_with,
+ like,
  exist, 
@@ -1099,110 +1099,154 @@
 ase 
-exist:%0A%09%09%09return (string != null);%0A%09%09case in:%0A%09%09%09// TODO implement%0A%09%09%09throw new IllegalStateException(
+like:%0A%09%09%09return string.contains(targetString);%0A%09%09case exist:%0A%09%09%09return (string != null);%0A%09%09case in:%0A%09%09%09return evaluateInQuery(string, targetString
 );%0A%09
@@ -1665,16 +1665,29 @@
 n_with:%0A
+%09%09case like:%0A
 %09%09case e
@@ -1761,10 +1761,520 @@
 %09%7D%0A%0A%09%7D%0A%0A
+%09private boolean evaluateInQuery(String value, String inValues) %7B%0A%09%09String escapedValue = escapeComma(value);%0A%0A%09%09for (String splited : convertEscapedComma(inValues).split(%22,%22)) %7B%0A%09%09%09if (splited.equals(escapedValue))%0A%09%09%09%09return true;%0A%09%09%7D%0A%0A%09%09return false;%0A%0A%09%7D%0A%0A%09private String escapeComma(String src) %7B%0A%09%09return src.replace(%22,%22, %22%7B%7Besc_com%7D%7D%22);%0A%09%7D%0A%0A%09private String convertEscapedComma(String src) %7B%0A%09%09return src.replace(%22%5C%5C%5C%5C%22, %22%7B%7Besc_dbs%7D%7D%22).replace(%22%5C%5C,%22, %22%7B%7Besc_com%7D%7D%22)%0A%09%09%09%09.replace(%22%7B%7Besc_dbs%7D%7D%22, %22%5C%5C%22);%0A%09%7D%0A%0A
 %7D%0A

Update operator