001: package snow.sortabletable;
002:
003: import java.util.regex.Pattern;
004:
005: /**
006: * Describes a search query for the sortable table
007: * IMMUTABLE !
008: */
009: public class Query {
010: // existing boolean operators to combine multiple query objects
011: public static final int OPERATION_AND = 0;
012: public static final int OPERATION_OR = 1;
013: public static final int OPERATION_AND_NOT = 2;
014: public static final int OPERATION_OR_NOT = 3;
015: //public static final int OPERATION_XOR = 4;
016:
017: public final static String[] booleanCombineNames = new String[] {
018: "and", "or", "and not", "or not" };
019:
020: public enum Combine {
021: And, Or, AndNot, OrNot;
022: public static Combine forIndex(int i) {
023: switch (i) {
024: case 0:
025: return And;
026: case 1:
027: return Or;
028: case 2:
029: return AndNot;
030: case 3:
031: return OrNot;
032: }
033: return And;
034: }
035: }
036:
037: public final static String[] comparisonTypeNames = new String[] {
038: "contains", "equals", "starts with", "ends with", "regEx" };
039:
040: public enum Comparison {
041: Contains, Equals, StartsWith, EndsWith, RegEx;
042: public static Comparison forIndex(int i) {
043: switch (i) {
044: case 0:
045: return Contains;
046: case 1:
047: return Equals;
048: case 2:
049: return StartsWith;
050: case 3:
051: return EndsWith;
052: case 4:
053: return RegEx; // matcher matches (fully), case insensitive.
054: }
055: return RegEx;
056: }
057: }
058:
059: public Comparison comparison = Comparison.Contains;
060: public final Pattern pattern;
061: public final int column; // = -1; // -1 means all columns
062: public final String searchTxt;
063: public final Combine boolOp; // = OPERATION_AND;
064:
065: public Query(int _column, String _searchTxt, Combine _boolOp) {
066: this (_column, _searchTxt, _boolOp, Comparison.Contains);
067: }
068:
069: public Query(int _column, String _searchTxt, Combine _boolOp,
070: Comparison _comparison) {
071: this .column = _column;
072: this .searchTxt = _searchTxt.toUpperCase();
073: this .boolOp = _boolOp;
074: this .comparison = _comparison;
075:
076: if (_comparison == Comparison.RegEx) {
077: // use the arg, NOT the uppercased, because "\s" is NOT "\S" !!!
078: pattern = Pattern.compile(_searchTxt,
079: Pattern.CASE_INSENSITIVE);
080: } else {
081: pattern = null;
082: }
083: }
084:
085: public boolean isEmpty() {
086: return searchTxt.length() == 0;
087: }
088:
089: public boolean isNegation() {
090: return boolOp == Combine.AndNot || boolOp == Combine.OrNot;
091: }
092:
093: public static Query[] simpleSearchQuery(String txt) {
094: return new Query[] { new Query(-1, txt, Combine.And,
095: Comparison.Contains) };
096: }
097:
098: public static Query[] emptySearchQuery() {
099: return new Query[] { new Query(-1, "", Combine.And,
100: Comparison.Contains) };
101: }
102:
103: @Override
104: public final String toString() {
105: if (this .isEmpty())
106: return "Query empty";
107: StringBuilder sb = new StringBuilder(50);
108: sb.append("Query '");
109: sb.append(searchTxt);
110: sb.append("' in col ");
111: sb.append(column);
112: sb.append(" bool ");
113: sb.append(boolOp);
114: return sb.toString();
115: }
116: }
|