1 /*******************************************************************************
2 * Copyright (c) 2010, 2015 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
.ITmfTimestamp
;
22 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfNanoTimestamp
;
23 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestampFormat
;
27 * Filter node for the comparison operation
30 * @author Patrick Tasse
32 public class TmfFilterCompareNode
extends TmfFilterAspectNode
{
34 /** compare node name */
35 public static final String NODE_NAME
= "COMPARE"; //$NON-NLS-1$
36 /** not attribute name */
37 public static final String NOT_ATTR
= "not"; //$NON-NLS-1$
38 /** result attribute name */
39 public static final String RESULT_ATTR
= "result"; //$NON-NLS-1$
40 /** type attribute name */
41 public static final String TYPE_ATTR
= "type"; //$NON-NLS-1$
42 /** value attribute name */
43 public static final String VALUE_ATTR
= "value"; //$NON-NLS-1$
46 * Supported comparison types
48 public static enum Type
{
49 /** numerical comparison type */
51 /** alphanumerical comparison type */
53 /** timestamp comparison type */
58 private boolean fNot
= false;
60 private Type fType
= Type
.NUM
;
61 private String fValue
;
62 private transient Number fValueNumber
;
63 private transient ITmfTimestamp fValueTimestamp
;
64 private transient TmfTimestampFormat fTimestampFormat
= new TmfTimestampFormat("T.SSSSSSSSS"); //$NON-NLS-1$
67 * @param parent the parent node
69 public TmfFilterCompareNode(ITmfFilterTreeNode parent
) {
74 * @return the NOT state
76 public boolean isNot() {
81 * @param not the NOT state
83 public void setNot(boolean not
) {
88 * @return the compare result (-1, 0 or 1)
90 public int getResult() {
95 * @param result the compare result (-1, 0 or 1)
97 public void setResult(int result
) {
98 this.fResult
= result
;
102 * @return the comparison type
104 public Type
getType() {
109 * @param type the comparison type
111 public void setType(Type type
) {
117 * @return the comparison value (in seconds for the TIMESTAMP type)
119 public String
getValue() {
124 * @param value the comparison value (in seconds for the TIMESTAMP type)
126 public void setValue(String value
) {
129 fValueTimestamp
= null;
133 if (fType
== Type
.NUM
) {
135 fValueNumber
= NumberFormat
.getInstance().parse(value
).doubleValue();
136 } catch (ParseException e
) {
138 } else if (fType
== Type
.TIMESTAMP
) {
140 fValueTimestamp
= new TmfNanoTimestamp(fTimestampFormat
.parseValue(value
.toString()));
141 } catch (ParseException e
) {
147 * @return true if the value is valid for the comparison type
149 public boolean hasValidValue() {
150 if (fType
== Type
.NUM
) {
151 return fValueNumber
!= null;
152 } else if (fType
== Type
.TIMESTAMP
) {
153 return fValue
!= null && !fValue
.isEmpty() && fValueTimestamp
!= null;
155 return fValue
!= null;
159 public String
getNodeName() {
164 public boolean matches(ITmfEvent event
) {
165 if (event
== null || fEventAspect
== null) {
168 Object value
= fEventAspect
.resolve(event
);
172 if (fType
== Type
.NUM
) {
173 if (fValueNumber
!= null) {
174 if (value
instanceof Number
) {
175 double valueDouble
= ((Number
) value
).doubleValue();
176 return (Double
.compare(valueDouble
, fValueNumber
.doubleValue()) == fResult
) ^ fNot
;
179 double valueDouble
= NumberFormat
.getInstance().parse(value
.toString()).doubleValue();
180 return (Double
.compare(valueDouble
, fValueNumber
.doubleValue()) == fResult
) ^ fNot
;
181 } catch (ParseException e
) {
184 } else if (fType
== Type
.ALPHA
) {
185 String valueString
= value
.toString();
186 int comp
= (int) Math
.signum(valueString
.compareTo(fValue
.toString()));
187 return (comp
== fResult
) ^ fNot
;
188 } else if (fType
== Type
.TIMESTAMP
) {
189 if (fValueTimestamp
!= null) {
190 if (value
instanceof ITmfTimestamp
) {
191 ITmfTimestamp valueTimestamp
= (ITmfTimestamp
) value
;
192 return (valueTimestamp
.compareTo(fValueTimestamp
) == fResult
) ^ fNot
;
195 ITmfTimestamp valueTimestamp
= new TmfNanoTimestamp(fTimestampFormat
.parseValue(value
.toString()));
196 return (valueTimestamp
.compareTo(fValueTimestamp
) == fResult
) ^ fNot
;
197 } catch (ParseException e
) {
205 public List
<String
> getValidChildren() {
206 return new ArrayList
<>(0);
210 public String
toString() {
211 String result
= (fResult
== 0 ?
"= " : fResult
< 0 ?
"< " : "> "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
212 String open
= (fType
== Type
.NUM ?
"" : fType
== Type
.ALPHA ?
"\"" : "["); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
213 String close
= (fType
== Type
.NUM ?
"" : fType
== Type
.ALPHA ?
"\"" : "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
214 String aspectName
= fEventAspect
!= null ? fEventAspect
.getName() : ""; //$NON-NLS-1$
215 return aspectName
+ (fNot ?
" not " : " ") + result
+ open
+ fValue
+ close
; //$NON-NLS-1$ //$NON-NLS-2$
219 public ITmfFilterTreeNode
clone() {
220 TmfFilterCompareNode clone
= (TmfFilterCompareNode
) super.clone();
221 clone
.setValue(fValue
);
226 public int hashCode() {
227 final int prime
= 31;
228 int result
= super.hashCode();
229 result
= prime
* result
+ (fNot ?
1231 : 1237);
230 result
= prime
* result
+ fResult
;
231 result
= prime
* result
+ ((fType
== null) ?
0 : fType
.hashCode());
232 result
= prime
* result
+ ((fValue
== null) ?
0 : fValue
.hashCode());
237 public boolean equals(Object obj
) {
241 if (!super.equals(obj
)) {
244 if (getClass() != obj
.getClass()) {
247 TmfFilterCompareNode other
= (TmfFilterCompareNode
) obj
;
248 if (fNot
!= other
.fNot
) {
251 if (fResult
!= other
.fResult
) {
254 if (fType
!= other
.fType
) {
257 if (fValue
== null) {
258 if (other
.fValue
!= null) {
261 } else if (!fValue
.equals(other
.fValue
)) {