1 /*******************************************************************************
2 * Copyright (c) 2010, 2013 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
.linuxtools
.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
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
21 import org
.eclipse
.linuxtools
.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 Number fValueNumber
;
55 private 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 (valueDouble
.compareTo(fValueNumber
.doubleValue()) == fResult
) ^ fNot
;
169 Double valueDouble
= NumberFormat
.getInstance().parse(value
.toString())
171 return (valueDouble
.compareTo(fValueNumber
.doubleValue()) == fResult
)
173 } catch (ParseException e
) {
176 } else if (fType
== Type
.ALPHA
) {
177 String valueString
= value
.toString();
178 int comp
= valueString
.compareTo(fValue
.toString());
181 } else if (comp
> 1) {
184 return (comp
== fResult
) ^ fNot
;
185 } else if (fType
== Type
.TIMESTAMP
) {
186 if (fValueTimestamp
!= null) {
187 if (value
instanceof TmfTimestamp
) {
188 TmfTimestamp valueTimestamp
= (TmfTimestamp
) value
;
189 return (valueTimestamp
.compareTo(fValueTimestamp
, false) == fResult
) ^ fNot
;
192 TmfTimestamp valueTimestamp
= new TmfTimestamp((long) (1E9
* NumberFormat
193 .getInstance().parse(value
.toString()).doubleValue()));
194 return (valueTimestamp
.compareTo(fValueTimestamp
, false) == fResult
) ^ fNot
;
195 } catch (ParseException e
) {
203 public List
<String
> getValidChildren() {
204 return new ArrayList
<>(0);
208 public String
toString() {
209 String result
= (fResult
== 0 ?
"= " : fResult
< 0 ?
"< " : "> "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
210 String open
= (fType
== Type
.NUM ?
"" : fType
== Type
.ALPHA ?
"\"" : "["); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
211 String close
= (fType
== Type
.NUM ?
"" : fType
== Type
.ALPHA ?
"\"" : "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
212 return fField
+ (fNot ?
" not " : " ") + result
+ open
+ fValue
+ close
; //$NON-NLS-1$ //$NON-NLS-2$
216 public ITmfFilterTreeNode
clone() {
217 TmfFilterCompareNode clone
= (TmfFilterCompareNode
) super.clone();
218 clone
.fField
= fField
;
219 clone
.setValue(fValue
);