fb9bb56c185bc3fe194ec5d6e91317fcfef7f68a
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / filter / model / TmfFilterCompareNode.java
1 /*******************************************************************************
2 * Copyright (c) 2010 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.TmfEvent;
21 import org.eclipse.linuxtools.tmf.core.event.TmfNoSuchFieldException;
22 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
23
24
25 public class TmfFilterCompareNode extends TmfFilterTreeNode {
26
27 public static final String NODE_NAME = "COMPARE"; //$NON-NLS-1$
28 public static final String NOT_ATTR = "not"; //$NON-NLS-1$
29 public static final String FIELD_ATTR = "field"; //$NON-NLS-1$
30 public static final String RESULT_ATTR = "result"; //$NON-NLS-1$
31 public static final String TYPE_ATTR = "type"; //$NON-NLS-1$
32 public static final String VALUE_ATTR = "value"; //$NON-NLS-1$
33
34 public static enum Type {
35 NUM,
36 ALPHA,
37 TIMESTAMP
38 }
39
40 private boolean fNot = false;
41 private String fField;
42 private int fResult;
43 private Type fType = Type.NUM;
44 private String fValue;
45 private Number fValueNumber;
46 private TmfTimestamp fValueTimestamp;
47
48 public TmfFilterCompareNode(ITmfFilterTreeNode parent) {
49 super(parent);
50 }
51
52 public boolean isNot() {
53 return fNot;
54 }
55
56 public void setNot(boolean not) {
57 this.fNot = not;
58 }
59
60 public String getField() {
61 return fField;
62 }
63
64 public void setField(String field) {
65 this.fField = field;
66 }
67
68 public int getResult() {
69 return fResult;
70 }
71
72 public void setResult(int result) {
73 this.fResult = result;
74 }
75
76 public Type getType() {
77 return fType;
78 }
79
80 public void setType(Type type) {
81 this.fType = type;
82 setValue(fValue);
83 }
84
85 public String getValue() {
86 return fValue;
87 }
88
89 public void setValue(String value) {
90 this.fValue = value;
91 fValueNumber = null;
92 fValueTimestamp = null;
93 if (value == null) {
94 return;
95 }
96 if (fType == Type.NUM) {
97 try {
98 fValueNumber = NumberFormat.getInstance().parse(value).doubleValue();
99 } catch (ParseException e) {
100 }
101 } else if (fType == Type.TIMESTAMP) {
102 try {
103 fValueTimestamp = new TmfTimestamp((long) (1E9 * NumberFormat.getInstance().parse(value.toString()).doubleValue()));
104 } catch (ParseException e) {
105 }
106 }
107 }
108
109 @Override
110 public String getNodeName() {
111 return NODE_NAME;
112 }
113
114 @Override
115 public boolean matches(TmfEvent event) {
116 try {
117 Object value = event.getContent().getField(fField);
118 if (value == null) {
119 return false ^ fNot;
120 }
121 if (fType == Type.NUM) {
122 if (fValueNumber instanceof Number) {
123 if (value instanceof Number) {
124 Double valueDouble = ((Number) value).doubleValue();
125 return (valueDouble.compareTo(fValueNumber.doubleValue()) == fResult) ^ fNot;
126 } else {
127 try {
128 Double valueDouble = NumberFormat.getInstance().parse(value.toString()).doubleValue();
129 return (valueDouble.compareTo(fValueNumber.doubleValue()) == fResult) ^ fNot;
130 } catch (ParseException e) {
131 }
132 }
133 }
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 instanceof TmfTimestamp) {
139 if (value instanceof TmfTimestamp) {
140 TmfTimestamp valueTimestamp = (TmfTimestamp) value;
141 return (valueTimestamp.compareTo(fValueTimestamp, false) == fResult) ^ fNot;
142 } else {
143 try {
144 TmfTimestamp valueTimestamp = new TmfTimestamp((long) (1E9 * NumberFormat.getInstance().parse(value.toString()).doubleValue()));
145 return (valueTimestamp.compareTo(fValueTimestamp, false) == fResult) ^ fNot;
146 } catch (ParseException e) {
147 }
148 }
149 }
150 }
151 } catch (TmfNoSuchFieldException e) {
152 }
153 return false ^ fNot;
154 }
155
156 @Override
157 public List<String> getValidChildren() {
158 return new ArrayList<String>(0);
159 }
160
161 @Override
162 public String toString() {
163 String result = (fResult == 0 ? "= " : fResult < 0 ? "< " : "> "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
164 String open = (fType == Type.NUM ? "" : fType == Type.ALPHA ? "\"" : "["); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
165 String close = (fType == Type.NUM ? "" : fType == Type.ALPHA ? "\"" : "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
166 return fField + (fNot ? " not " : " ") + result + open + fValue + close; //$NON-NLS-1$ //$NON-NLS-2$
167 }
168
169 @Override
170 public ITmfFilterTreeNode clone() {
171 TmfFilterCompareNode clone = (TmfFilterCompareNode) super.clone();
172 clone.fField = new String(fField);
173 clone.setValue(new String(fValue));
174 return clone;
175 }
176 }
This page took 0.034363 seconds and 5 git commands to generate.