Skip to main content
summaryrefslogtreecommitdiffstats
blob: b48dd25a01aa229e1a098123bbdb908c5ff1cb93 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*******************************************************************************
 * Copyright (c) 2004, 2007 Mylyn project committers and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

package org.eclipse.mylyn.internal.trac.core.model;

import java.util.ArrayList;
import java.util.List;

/**
 * Represents a search criterion. Each criterion is applied to a field such as milestone or priority. It has a compare
 * operator and a list of values. The compare mode is <code>OR</code> for the operators <code>contains</code>,
 * <code>starts with</code>, <code>ends with</code> and <code>is</code>. The compare mode is <code>AND</code>
 * for all other (negated) operators.
 * 
 * @author Steffen Pingel
 */
public class TracSearchFilter {

	public enum CompareOperator {
		CONTAINS("~"), CONTAINS_NOT("!~"), BEGINS_WITH("^"), NOT_BEGINS_WITH("!^"), ENDS_WITH("$"), NOT_ENDS_WITH("!$"), IS(
				""), IS_NOT("!");

		public static CompareOperator fromUrl(String value) {
			for (CompareOperator operator : values()) {
				if (operator != IS && operator != IS_NOT && value.startsWith(operator.queryValue)) {
					return operator;
				}
			}
			if (value.startsWith(IS_NOT.queryValue)) {
				return IS_NOT;
			}
			return IS;
		}

		/** The string that represent the operator in a Trac query. */
		private String queryValue;

		CompareOperator(String queryValue) {
			this.queryValue = queryValue;
		}

		public String getQueryValue() {
			return queryValue;
		}

		@Override
		public String toString() {
			switch (this) {
			case CONTAINS:
				return "contains";
			case CONTAINS_NOT:
				return "does not contain";
			case BEGINS_WITH:
				return "begins with";
			case NOT_BEGINS_WITH:
				return "does not begin with";
			case ENDS_WITH:
				return "ends with";
			case NOT_ENDS_WITH:
				return "does not end with";
			case IS_NOT:
				return "is not";
			default:
				return "is";
			}
		}

	}

	private String fieldName;

	private CompareOperator operator;

	private List<String> values = new ArrayList<String>();

	public TracSearchFilter(String fieldName) {
		this.fieldName = fieldName;
	}

	public void addValue(String value) {
		values.add(value);
	}

	public String getFieldName() {
		return fieldName;
	}

	public CompareOperator getOperator() {
		return operator;
	}

	public List<String> getValues() {
		return values;
	}

	public void setOperator(CompareOperator operator) {
		this.operator = operator;
	}

}

Back to the top