1 /*******************************************************************************
2 * Copyright (c) 2010, 2014 Ericsson
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
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.core
.filter
.model
;
15 import java
.text
.NumberFormat
;
16 import java
.text
.ParseException
;
17 import java
.util
.ArrayList
;
18 import java
.util
.List
;
20 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
21 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
25 * Filter node for the comparison operation
28 * @author Patrick Tasse
30 @SuppressWarnings("javadoc")
31 public class TmfFilterCompareNode
extends TmfFilterTreeNode
{
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$
41 * Supported comparison types
43 public static enum Type
{
49 private boolean fNot
= false;
50 private String fField
;
52 private Type fType
= Type
.NUM
;
53 private String fValue
;
54 private transient Number fValueNumber
;
55 private transient TmfTimestamp fValueTimestamp
;
58 * @param parent the parent node
60 public TmfFilterCompareNode(ITmfFilterTreeNode parent
) {
65 * @return the NOT state
67 public boolean isNot() {
72 * @param not the NOT state
74 public void setNot(boolean not
) {
79 * @return the field name
81 public String
getField() {
86 * @param field the field name
88 public void setField(String field
) {
93 * @return the compare result
95 public int getResult() {
100 * @param result the compare result
102 public void setResult(int result
) {
103 this.fResult
= result
;
107 * @return the comparison type
109 public Type
getType() {
114 * @param type the comparison type
116 public void setType(Type type
) {
122 * @return the comparison value
124 public String
getValue() {
129 * @param value the comparison value
131 public void setValue(String value
) {
134 fValueTimestamp
= null;
138 if (fType
== Type
.NUM
) {
140 fValueNumber
= NumberFormat
.getInstance().parse(value
).doubleValue();
141 } catch (ParseException e
) {
143 } else if (fType
== Type
.TIMESTAMP
) {
145 fValueTimestamp
= new TmfTimestamp((long) (1E9
* NumberFormat
.getInstance().parse(value
.toString()).doubleValue()));
146 } catch (ParseException e
) {
152 public String
getNodeName() {
157 public boolean matches(ITmfEvent event
) {
158 Object value
= getFieldValue(event
, fField
);
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
;
169 double valueDouble
= NumberFormat
.getInstance().parse(value
.toString()).doubleValue();
170 return (Double
.compare(valueDouble
, fValueNumber
.doubleValue()) == fResult
) ^ fNot
;
171 } catch (ParseException e
) {
174 } else if (fType
== Type
.ALPHA
) {
175 String valueString
= value
.toString();
176 int comp
= valueString
.compareTo(fValue
.toString());
179 } else if (comp
> 1) {
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
) == fResult
) ^ fNot
;
190 TmfTimestamp valueTimestamp
= new TmfTimestamp((long) (1E9
* NumberFormat
191 .getInstance().parse(value
.toString()).doubleValue()));
192 return (valueTimestamp
.compareTo(fValueTimestamp
) == fResult
) ^ fNot
;
193 } catch (ParseException e
) {
201 public List
<String
> getValidChildren() {
202 return new ArrayList
<>(0);
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$
214 public ITmfFilterTreeNode
clone() {
215 TmfFilterCompareNode clone
= (TmfFilterCompareNode
) super.clone();
216 clone
.fField
= fField
;
217 clone
.setValue(fValue
);
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());
234 public boolean equals(Object obj
) {
238 if (!super.equals(obj
)) {
241 if (getClass() != obj
.getClass()) {
244 TmfFilterCompareNode other
= (TmfFilterCompareNode
) obj
;
245 if (fField
== null) {
246 if (other
.fField
!= null) {
249 } else if (!fField
.equals(other
.fField
)) {
252 if (fNot
!= other
.fNot
) {
255 if (fResult
!= other
.fResult
) {
258 if (fType
!= other
.fType
) {
261 if (fValue
== null) {
262 if (other
.fValue
!= null) {
265 } else if (!fValue
.equals(other
.fValue
)) {