ss: Move plugins to Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / filter / model / TmfFilterCompareNode.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2014 Ericsson
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
13 package org.eclipse.linuxtools.tmf.core.filter.model;
14
15 import java.text.NumberFormat;
16 import java.text.ParseException;
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
21 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
22
23
24 /**
25 * Filter node for the comparison operation
26 *
27 * @version 1.0
28 * @author Patrick Tasse
29 */
30 @SuppressWarnings("javadoc")
31 public class TmfFilterCompareNode extends TmfFilterTreeNode {
32
33 public static final String NODE_NAME = "COMPARE"; //$NON-NLS-1$
34 public static final String NOT_ATTR = "not"; //$NON-NLS-1$
35 public static final String FIELD_ATTR = "field"; //$NON-NLS-1$
36 public static final String RESULT_ATTR = "result"; //$NON-NLS-1$
37 public static final String TYPE_ATTR = "type"; //$NON-NLS-1$
38 public static final String VALUE_ATTR = "value"; //$NON-NLS-1$
39
40 /**
41 * Supported comparison types
42 */
43 public static enum Type {
44 NUM,
45 ALPHA,
46 TIMESTAMP
47 }
48
49 private boolean fNot = false;
50 private String fField;
51 private int fResult;
52 private Type fType = Type.NUM;
53 private String fValue;
54 private transient Number fValueNumber;
55 private transient TmfTimestamp fValueTimestamp;
56
57 /**
58 * @param parent the parent node
59 */
60 public TmfFilterCompareNode(ITmfFilterTreeNode parent) {
61 super(parent);
62 }
63
64 /**
65 * @return the NOT state
66 */
67 public boolean isNot() {
68 return fNot;
69 }
70
71 /**
72 * @param not the NOT state
73 */
74 public void setNot(boolean not) {
75 this.fNot = not;
76 }
77
78 /**
79 * @return the field name
80 */
81 public String getField() {
82 return fField;
83 }
84
85 /**
86 * @param field the field name
87 */
88 public void setField(String field) {
89 this.fField = field;
90 }
91
92 /**
93 * @return the compare result
94 */
95 public int getResult() {
96 return fResult;
97 }
98
99 /**
100 * @param result the compare result
101 */
102 public void setResult(int result) {
103 this.fResult = result;
104 }
105
106 /**
107 * @return the comparison type
108 */
109 public Type getType() {
110 return fType;
111 }
112
113 /**
114 * @param type the comparison type
115 */
116 public void setType(Type type) {
117 this.fType = type;
118 setValue(fValue);
119 }
120
121 /**
122 * @return the comparison value
123 */
124 public String getValue() {
125 return fValue;
126 }
127
128 /**
129 * @param value the comparison value
130 */
131 public void setValue(String value) {
132 this.fValue = value;
133 fValueNumber = null;
134 fValueTimestamp = null;
135 if (value == null) {
136 return;
137 }
138 if (fType == Type.NUM) {
139 try {
140 fValueNumber = NumberFormat.getInstance().parse(value).doubleValue();
141 } catch (ParseException e) {
142 }
143 } else if (fType == Type.TIMESTAMP) {
144 try {
145 fValueTimestamp = new TmfTimestamp((long) (1E9 * NumberFormat.getInstance().parse(value.toString()).doubleValue()));
146 } catch (ParseException e) {
147 }
148 }
149 }
150
151 @Override
152 public String getNodeName() {
153 return NODE_NAME;
154 }
155
156 @Override
157 public boolean matches(ITmfEvent event) {
158 Object value = getFieldValue(event, fField);
159 if (value == null) {
160 return false ^ fNot;
161 }
162 if (fType == Type.NUM) {
163 if (fValueNumber != null) {
164 if (value instanceof Number) {
165 double valueDouble = ((Number) value).doubleValue();
166 return (Double.compare(valueDouble, fValueNumber.doubleValue()) == fResult) ^ fNot;
167 }
168 try {
169 double valueDouble = NumberFormat.getInstance().parse(value.toString()).doubleValue();
170 return (Double.compare(valueDouble, fValueNumber.doubleValue()) == fResult) ^ fNot;
171 } catch (ParseException e) {
172 }
173 }
174 } else if (fType == Type.ALPHA) {
175 String valueString = value.toString();
176 int comp = valueString.compareTo(fValue.toString());
177 if (comp < -1) {
178 comp = -1;
179 } else if (comp > 1) {
180 comp = 1;
181 }
182 return (comp == fResult) ^ fNot;
183 } else if (fType == Type.TIMESTAMP) {
184 if (fValueTimestamp != null) {
185 if (value instanceof TmfTimestamp) {
186 TmfTimestamp valueTimestamp = (TmfTimestamp) value;
187 return (valueTimestamp.compareTo(fValueTimestamp, false) == fResult) ^ fNot;
188 }
189 try {
190 TmfTimestamp valueTimestamp = new TmfTimestamp((long) (1E9 * NumberFormat
191 .getInstance().parse(value.toString()).doubleValue()));
192 return (valueTimestamp.compareTo(fValueTimestamp, false) == fResult) ^ fNot;
193 } catch (ParseException e) {
194 }
195 }
196 }
197 return false ^ fNot;
198 }
199
200 @Override
201 public List<String> getValidChildren() {
202 return new ArrayList<>(0);
203 }
204
205 @Override
206 public String toString() {
207 String result = (fResult == 0 ? "= " : fResult < 0 ? "< " : "> "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
208 String open = (fType == Type.NUM ? "" : fType == Type.ALPHA ? "\"" : "["); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
209 String close = (fType == Type.NUM ? "" : fType == Type.ALPHA ? "\"" : "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
210 return fField + (fNot ? " not " : " ") + result + open + fValue + close; //$NON-NLS-1$ //$NON-NLS-2$
211 }
212
213 @Override
214 public ITmfFilterTreeNode clone() {
215 TmfFilterCompareNode clone = (TmfFilterCompareNode) super.clone();
216 clone.fField = fField;
217 clone.setValue(fValue);
218 return clone;
219 }
220
221 @Override
222 public int hashCode() {
223 final int prime = 31;
224 int result = super.hashCode();
225 result = prime * result + ((fField == null) ? 0 : fField.hashCode());
226 result = prime * result + (fNot ? 1231 : 1237);
227 result = prime * result + fResult;
228 result = prime * result + ((fType == null) ? 0 : fType.hashCode());
229 result = prime * result + ((fValue == null) ? 0 : fValue.hashCode());
230 return result;
231 }
232
233 @Override
234 public boolean equals(Object obj) {
235 if (this == obj) {
236 return true;
237 }
238 if (!super.equals(obj)) {
239 return false;
240 }
241 if (getClass() != obj.getClass()) {
242 return false;
243 }
244 TmfFilterCompareNode other = (TmfFilterCompareNode) obj;
245 if (fField == null) {
246 if (other.fField != null) {
247 return false;
248 }
249 } else if (!fField.equals(other.fField)) {
250 return false;
251 }
252 if (fNot != other.fNot) {
253 return false;
254 }
255 if (fResult != other.fResult) {
256 return false;
257 }
258 if (fType != other.fType) {
259 return false;
260 }
261 if (fValue == null) {
262 if (other.fValue != null) {
263 return false;
264 }
265 } else if (!fValue.equals(other.fValue)) {
266 return false;
267 }
268 return true;
269 }
270 }
This page took 0.039861 seconds and 5 git commands to generate.