Move alltests plugin to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core / src / org / eclipse / linuxtools / tmf / core / filter / model / TmfFilterMatchesNode.java
CommitLineData
be222f56 1/*******************************************************************************
61759503 2 * Copyright (c) 2010, 2013 Ericsson
be222f56
PT
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
12
13package org.eclipse.linuxtools.tmf.core.filter.model;
14
15import java.util.ArrayList;
16import java.util.List;
17import java.util.regex.Pattern;
18import java.util.regex.PatternSyntaxException;
19
20import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
21
22/**
23 * Filter node for the regex match
24 *
25 * @version 1.0
26 * @author Patrick Tasse
27 */
28@SuppressWarnings("javadoc")
29public class TmfFilterMatchesNode extends TmfFilterTreeNode {
30
31 public static final String NODE_NAME = "MATCHES"; //$NON-NLS-1$
41b5c37f
AM
32 public static final String NOT_ATTR = "not"; //$NON-NLS-1$
33 public static final String FIELD_ATTR = "field"; //$NON-NLS-1$
34 public static final String REGEX_ATTR = "regex"; //$NON-NLS-1$
35
36 private boolean fNot = false;
37 private String fField;
38 private String fRegex;
b64984c4 39 private transient Pattern fPattern;
41b5c37f
AM
40
41 /**
42 * @param parent
43 * the parent node
44 */
45 public TmfFilterMatchesNode(ITmfFilterTreeNode parent) {
46 super(parent);
47 }
48
49 /**
50 * @return the NOT state
51 */
52 public boolean isNot() {
53 return fNot;
54 }
55
56 /**
57 * @param not
58 * the NOT state
59 */
60 public void setNot(boolean not) {
61 this.fNot = not;
62 }
63
64 /**
65 * @return the field name
66 */
67 public String getField() {
68 return fField;
69 }
70
71 /**
72 * @param field
73 * the field name
74 */
75 public void setField(String field) {
76 this.fField = field;
77 }
78
79 /**
80 * @return the regular expression
81 */
82 public String getRegex() {
83 return fRegex;
84 }
85
86 /**
87 * @param regex
88 * the regular expression
89 */
90 public void setRegex(String regex) {
91 this.fRegex = regex;
ef906471
XR
92 if (regex != null) {
93 try {
94 this.fPattern = Pattern.compile(regex, Pattern.DOTALL);
95 } catch (PatternSyntaxException e) {
96 this.fPattern = null;
97 }
41b5c37f
AM
98 }
99 }
100
101 @Override
102 public String getNodeName() {
103 return NODE_NAME;
104 }
105
106 @Override
107 public boolean matches(ITmfEvent event) {
be222f56
PT
108 if (fPattern == null) {
109 return false ^ fNot;
110 }
111
112 Object value = getFieldValue(event, fField);
113 if (value == null) {
114 return false ^ fNot;
115 }
116 String valueString = value.toString();
117
118 return fPattern.matcher(valueString).matches() ^ fNot;
41b5c37f
AM
119 }
120
121 @Override
122 public List<String> getValidChildren() {
a4524c1b 123 return new ArrayList<>(0);
41b5c37f
AM
124 }
125
126 @Override
127 public String toString() {
128 return fField + (fNot ? " not" : "") + " matches \"" + fRegex + "\""; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
129 }
130
131 @Override
132 public ITmfFilterTreeNode clone() {
133 TmfFilterMatchesNode clone = (TmfFilterMatchesNode) super.clone();
134 clone.fField = fField;
135 clone.setRegex(fRegex);
136 return clone;
137 }
138
139 /**
140 * @param pattern
141 * the rough regex pattern
142 * @return the compliant regex
143 */
144 public static String regexFix(String pattern) {
145 String ret = pattern;
146 // if the pattern does not contain one of the expressions .* !^
147 // (at the beginning) $ (at the end), then a .* is added at the
148 // beginning and at the end of the pattern
149 if (!(ret.indexOf(".*") >= 0 || ret.charAt(0) == '^' || ret.charAt(ret.length() - 1) == '$')) { //$NON-NLS-1$
150 ret = ".*" + ret + ".*"; //$NON-NLS-1$ //$NON-NLS-2$
151 }
152 return ret;
153 }
b64984c4
VP
154
155 @Override
156 public int hashCode() {
157 final int prime = 31;
158 int result = super.hashCode();
159 result = prime * result + ((fField == null) ? 0 : fField.hashCode());
160 result = prime * result + (fNot ? 1231 : 1237);
161 result = prime * result + ((fRegex == null) ? 0 : fRegex.hashCode());
162 return result;
163 }
164
165 @Override
166 public boolean equals(Object obj) {
167 if (this == obj) {
168 return true;
169 }
170 if (!super.equals(obj)) {
171 return false;
172 }
173 if (getClass() != obj.getClass()) {
174 return false;
175 }
176 TmfFilterMatchesNode other = (TmfFilterMatchesNode) obj;
177 if (fField == null) {
178 if (other.fField != null) {
179 return false;
180 }
181 } else if (!fField.equals(other.fField)) {
182 return false;
183 }
184 if (fNot != other.fNot) {
185 return false;
186 }
187 if (fRegex == null) {
188 if (other.fRegex != null) {
189 return false;
190 }
191 } else if (!fRegex.equals(other.fRegex)) {
192 return false;
193 }
194 return true;
195 }
be222f56 196}
This page took 0.072949 seconds and 5 git commands to generate.