Commit | Line | Data |
---|---|---|
46ab8ce3 FC |
1 | /*******************************************************************************\r |
2 | * Copyright (c) 2010 Ericsson\r | |
9b749023 | 3 | *\r |
46ab8ce3 FC |
4 | * All rights reserved. This program and the accompanying materials are\r |
5 | * made available under the terms of the Eclipse Public License v1.0 which\r | |
6 | * accompanies this distribution, and is available at\r | |
7 | * http://www.eclipse.org/legal/epl-v10.html\r | |
9b749023 | 8 | *\r |
46ab8ce3 FC |
9 | * Contributors:\r |
10 | * Patrick Tasse - Initial API and implementation\r | |
11 | *******************************************************************************/\r | |
12 | \r | |
6c13869b | 13 | package org.eclipse.linuxtools.tmf.core.filter.model;\r |
46ab8ce3 FC |
14 | \r |
15 | import java.text.NumberFormat;\r | |
16 | import java.text.ParseException;\r | |
17 | import java.util.ArrayList;\r | |
18 | import java.util.List;\r | |
19 | \r | |
a96d4804 | 20 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;\r |
6c13869b | 21 | import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;\r |
46ab8ce3 FC |
22 | \r |
23 | \r | |
d37aaa7f FC |
24 | /**\r |
25 | * Filter node for the comparison operation\r | |
9b749023 | 26 | *\r |
d37aaa7f FC |
27 | * @version 1.0\r |
28 | * @author Patrick Tasse\r | |
29 | */\r | |
0283f7ff | 30 | @SuppressWarnings("javadoc")\r |
46ab8ce3 FC |
31 | public class TmfFilterCompareNode extends TmfFilterTreeNode {\r |
32 | \r | |
0283f7ff | 33 | public static final String NODE_NAME = "COMPARE"; //$NON-NLS-1$\r |
46ab8ce3 FC |
34 | public static final String NOT_ATTR = "not"; //$NON-NLS-1$\r |
35 | public static final String FIELD_ATTR = "field"; //$NON-NLS-1$\r | |
36 | public static final String RESULT_ATTR = "result"; //$NON-NLS-1$\r | |
37 | public static final String TYPE_ATTR = "type"; //$NON-NLS-1$\r | |
38 | public static final String VALUE_ATTR = "value"; //$NON-NLS-1$\r | |
9b749023 | 39 | \r |
d37aaa7f FC |
40 | /**\r |
41 | * Supported comparison types\r | |
42 | */\r | |
46ab8ce3 FC |
43 | public static enum Type {\r |
44 | NUM,\r | |
45 | ALPHA,\r | |
46 | TIMESTAMP\r | |
47 | }\r | |
9b749023 | 48 | \r |
46ab8ce3 FC |
49 | private boolean fNot = false;\r |
50 | private String fField;\r | |
51 | private int fResult;\r | |
52 | private Type fType = Type.NUM;\r | |
53 | private String fValue;\r | |
54 | private Number fValueNumber;\r | |
55 | private TmfTimestamp fValueTimestamp;\r | |
9b749023 | 56 | \r |
0283f7ff FC |
57 | /**\r |
58 | * @param parent the parent node\r | |
59 | */\r | |
46ab8ce3 FC |
60 | public TmfFilterCompareNode(ITmfFilterTreeNode parent) {\r |
61 | super(parent);\r | |
62 | }\r | |
63 | \r | |
0283f7ff FC |
64 | /**\r |
65 | * @return the NOT state\r | |
66 | */\r | |
46ab8ce3 FC |
67 | public boolean isNot() {\r |
68 | return fNot;\r | |
69 | }\r | |
9b749023 | 70 | \r |
0283f7ff FC |
71 | /**\r |
72 | * @param not the NOT state\r | |
73 | */\r | |
46ab8ce3 FC |
74 | public void setNot(boolean not) {\r |
75 | this.fNot = not;\r | |
76 | }\r | |
9b749023 | 77 | \r |
0283f7ff FC |
78 | /**\r |
79 | * @return the field name\r | |
80 | */\r | |
46ab8ce3 FC |
81 | public String getField() {\r |
82 | return fField;\r | |
83 | }\r | |
84 | \r | |
0283f7ff FC |
85 | /**\r |
86 | * @param field the field name\r | |
87 | */\r | |
46ab8ce3 FC |
88 | public void setField(String field) {\r |
89 | this.fField = field;\r | |
90 | }\r | |
91 | \r | |
0283f7ff FC |
92 | /**\r |
93 | * @return the compare result\r | |
94 | */\r | |
46ab8ce3 FC |
95 | public int getResult() {\r |
96 | return fResult;\r | |
97 | }\r | |
9b749023 | 98 | \r |
0283f7ff FC |
99 | /**\r |
100 | * @param result the compare result\r | |
101 | */\r | |
46ab8ce3 FC |
102 | public void setResult(int result) {\r |
103 | this.fResult = result;\r | |
104 | }\r | |
9b749023 | 105 | \r |
0283f7ff FC |
106 | /**\r |
107 | * @return the comparison type\r | |
108 | */\r | |
46ab8ce3 FC |
109 | public Type getType() {\r |
110 | return fType;\r | |
111 | }\r | |
112 | \r | |
0283f7ff FC |
113 | /**\r |
114 | * @param type the comparison type\r | |
115 | */\r | |
46ab8ce3 FC |
116 | public void setType(Type type) {\r |
117 | this.fType = type;\r | |
118 | setValue(fValue);\r | |
119 | }\r | |
120 | \r | |
0283f7ff FC |
121 | /**\r |
122 | * @return the comparison value\r | |
123 | */\r | |
46ab8ce3 FC |
124 | public String getValue() {\r |
125 | return fValue;\r | |
126 | }\r | |
9b749023 | 127 | \r |
0283f7ff FC |
128 | /**\r |
129 | * @param value the comparison value\r | |
130 | */\r | |
46ab8ce3 FC |
131 | public void setValue(String value) {\r |
132 | this.fValue = value;\r | |
133 | fValueNumber = null;\r | |
134 | fValueTimestamp = null;\r | |
135 | if (value == null) {\r | |
136 | return;\r | |
137 | }\r | |
138 | if (fType == Type.NUM) {\r | |
139 | try {\r | |
140 | fValueNumber = NumberFormat.getInstance().parse(value).doubleValue();\r | |
141 | } catch (ParseException e) {\r | |
142 | }\r | |
143 | } else if (fType == Type.TIMESTAMP) {\r | |
144 | try {\r | |
145 | fValueTimestamp = new TmfTimestamp((long) (1E9 * NumberFormat.getInstance().parse(value.toString()).doubleValue()));\r | |
146 | } catch (ParseException e) {\r | |
147 | }\r | |
148 | }\r | |
149 | }\r | |
9b749023 | 150 | \r |
46ab8ce3 FC |
151 | @Override\r |
152 | public String getNodeName() {\r | |
153 | return NODE_NAME;\r | |
154 | }\r | |
155 | \r | |
156 | @Override\r | |
a96d4804 FC |
157 | public boolean matches(ITmfEvent event) {\r |
158 | Object value = getFieldValue(event, fField);\r | |
4c564a2d FC |
159 | if (value == null) {\r |
160 | return false ^ fNot;\r | |
161 | }\r | |
162 | if (fType == Type.NUM) {\r | |
6e85c58d | 163 | if (fValueNumber != null) {\r |
4c564a2d FC |
164 | if (value instanceof Number) {\r |
165 | Double valueDouble = ((Number) value).doubleValue();\r | |
166 | return (valueDouble.compareTo(fValueNumber.doubleValue()) == fResult) ^ fNot;\r | |
9b749023 AM |
167 | }\r |
168 | try {\r | |
169 | Double valueDouble = NumberFormat.getInstance().parse(value.toString())\r | |
170 | .doubleValue();\r | |
171 | return (valueDouble.compareTo(fValueNumber.doubleValue()) == fResult)\r | |
172 | ^ fNot;\r | |
173 | } catch (ParseException e) {\r | |
4c564a2d FC |
174 | }\r |
175 | }\r | |
176 | } else if (fType == Type.ALPHA) {\r | |
177 | String valueString = value.toString();\r | |
8c35f309 | 178 | int comp = valueString.compareTo(fValue.toString());\r |
9b749023 AM |
179 | if (comp < -1) {\r |
180 | comp = -1;\r | |
181 | } else if (comp > 1) {\r | |
182 | comp = 1;\r | |
183 | }\r | |
8c35f309 | 184 | return (comp == fResult) ^ fNot;\r |
4c564a2d | 185 | } else if (fType == Type.TIMESTAMP) {\r |
6e85c58d | 186 | if (fValueTimestamp != null) {\r |
4c564a2d FC |
187 | if (value instanceof TmfTimestamp) {\r |
188 | TmfTimestamp valueTimestamp = (TmfTimestamp) value;\r | |
189 | return (valueTimestamp.compareTo(fValueTimestamp, false) == fResult) ^ fNot;\r | |
9b749023 AM |
190 | }\r |
191 | try {\r | |
192 | TmfTimestamp valueTimestamp = new TmfTimestamp((long) (1E9 * NumberFormat\r | |
193 | .getInstance().parse(value.toString()).doubleValue()));\r | |
194 | return (valueTimestamp.compareTo(fValueTimestamp, false) == fResult) ^ fNot;\r | |
195 | } catch (ParseException e) {\r | |
4c564a2d FC |
196 | }\r |
197 | }\r | |
198 | }\r | |
199 | return false ^ fNot;\r | |
46ab8ce3 FC |
200 | }\r |
201 | \r | |
202 | @Override\r | |
203 | public List<String> getValidChildren() {\r | |
204 | return new ArrayList<String>(0);\r | |
205 | }\r | |
206 | \r | |
207 | @Override\r | |
208 | public String toString() {\r | |
209 | String result = (fResult == 0 ? "= " : fResult < 0 ? "< " : "> "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$\r | |
210 | String open = (fType == Type.NUM ? "" : fType == Type.ALPHA ? "\"" : "["); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$\r | |
211 | String close = (fType == Type.NUM ? "" : fType == Type.ALPHA ? "\"" : "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$\r | |
212 | return fField + (fNot ? " not " : " ") + result + open + fValue + close; //$NON-NLS-1$ //$NON-NLS-2$\r | |
213 | }\r | |
214 | \r | |
215 | @Override\r | |
216 | public ITmfFilterTreeNode clone() {\r | |
217 | TmfFilterCompareNode clone = (TmfFilterCompareNode) super.clone();\r | |
6e85c58d FC |
218 | clone.fField = fField;\r |
219 | clone.setValue(fValue);\r | |
46ab8ce3 FC |
220 | return clone;\r |
221 | }\r | |
222 | }\r |