1 /*******************************************************************************
2 * Copyright (c) 2010 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
.event
.TmfTimestamp
;
24 public class TmfFilterCompareNode
extends TmfFilterTreeNode
{
26 public static final String NODE_NAME
= "COMPARE"; //$NON-NLS-1$
27 public static final String NOT_ATTR
= "not"; //$NON-NLS-1$
28 public static final String FIELD_ATTR
= "field"; //$NON-NLS-1$
29 public static final String RESULT_ATTR
= "result"; //$NON-NLS-1$
30 public static final String TYPE_ATTR
= "type"; //$NON-NLS-1$
31 public static final String VALUE_ATTR
= "value"; //$NON-NLS-1$
33 public static enum Type
{
39 private boolean fNot
= false;
40 private String fField
;
42 private Type fType
= Type
.NUM
;
43 private String fValue
;
44 private Number fValueNumber
;
45 private TmfTimestamp fValueTimestamp
;
47 public TmfFilterCompareNode(ITmfFilterTreeNode parent
) {
51 public boolean isNot() {
55 public void setNot(boolean not
) {
59 public String
getField() {
63 public void setField(String field
) {
67 public int getResult() {
71 public void setResult(int result
) {
72 this.fResult
= result
;
75 public Type
getType() {
79 public void setType(Type type
) {
84 public String
getValue() {
88 public void setValue(String value
) {
91 fValueTimestamp
= null;
95 if (fType
== Type
.NUM
) {
97 fValueNumber
= NumberFormat
.getInstance().parse(value
).doubleValue();
98 } catch (ParseException e
) {
100 } else if (fType
== Type
.TIMESTAMP
) {
102 fValueTimestamp
= new TmfTimestamp((long) (1E9
* NumberFormat
.getInstance().parse(value
.toString()).doubleValue()));
103 } catch (ParseException e
) {
109 public String
getNodeName() {
114 public boolean matches(ITmfEvent event
) {
115 Object value
= getFieldValue(event
, fField
);
119 if (fType
== Type
.NUM
) {
120 if (fValueNumber
!= null) {
121 if (value
instanceof Number
) {
122 Double valueDouble
= ((Number
) value
).doubleValue();
123 return (valueDouble
.compareTo(fValueNumber
.doubleValue()) == fResult
) ^ fNot
;
126 Double valueDouble
= NumberFormat
.getInstance().parse(value
.toString())
128 return (valueDouble
.compareTo(fValueNumber
.doubleValue()) == fResult
)
130 } catch (ParseException e
) {
134 } else if (fType
== Type
.ALPHA
) {
135 String valueString
= value
.toString();
136 return (valueString
.compareTo(fValue
.toString()) == fResult
) ^ fNot
;
137 } else if (fType
== Type
.TIMESTAMP
) {
138 if (fValueTimestamp
!= null) {
139 if (value
instanceof TmfTimestamp
) {
140 TmfTimestamp valueTimestamp
= (TmfTimestamp
) value
;
141 return (valueTimestamp
.compareTo(fValueTimestamp
, false) == fResult
) ^ fNot
;
144 TmfTimestamp valueTimestamp
= new TmfTimestamp((long) (1E9
* NumberFormat
145 .getInstance().parse(value
.toString()).doubleValue()));
146 return (valueTimestamp
.compareTo(fValueTimestamp
, false) == fResult
) ^ fNot
;
147 } catch (ParseException e
) {
156 public List
<String
> getValidChildren() {
157 return new ArrayList
<String
>(0);
161 public String
toString() {
162 String result
= (fResult
== 0 ?
"= " : fResult
< 0 ?
"< " : "> "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
163 String open
= (fType
== Type
.NUM ?
"" : fType
== Type
.ALPHA ?
"\"" : "["); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
164 String close
= (fType
== Type
.NUM ?
"" : fType
== Type
.ALPHA ?
"\"" : "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
165 return fField
+ (fNot ?
" not " : " ") + result
+ open
+ fValue
+ close
; //$NON-NLS-1$ //$NON-NLS-2$
169 public ITmfFilterTreeNode
clone() {
170 TmfFilterCompareNode clone
= (TmfFilterCompareNode
) super.clone();
171 clone
.fField
= fField
;
172 clone
.setValue(fValue
);