Merge branch 'master' into lttng_2_0_control_dev
[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.ITmfEvent;
21 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
22
23
24 public class TmfFilterCompareNode extends TmfFilterTreeNode {
25
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$
32
33 public static enum Type {
34 NUM,
35 ALPHA,
36 TIMESTAMP
37 }
38
39 private boolean fNot = false;
40 private String fField;
41 private int fResult;
42 private Type fType = Type.NUM;
43 private String fValue;
44 private Number fValueNumber;
45 private TmfTimestamp fValueTimestamp;
46
47 public TmfFilterCompareNode(ITmfFilterTreeNode parent) {
48 super(parent);
49 }
50
51 public boolean isNot() {
52 return fNot;
53 }
54
55 public void setNot(boolean not) {
56 this.fNot = not;
57 }
58
59 public String getField() {
60 return fField;
61 }
62
63 public void setField(String field) {
64 this.fField = field;
65 }
66
67 public int getResult() {
68 return fResult;
69 }
70
71 public void setResult(int result) {
72 this.fResult = result;
73 }
74
75 public Type getType() {
76 return fType;
77 }
78
79 public void setType(Type type) {
80 this.fType = type;
81 setValue(fValue);
82 }
83
84 public String getValue() {
85 return fValue;
86 }
87
88 public void setValue(String value) {
89 this.fValue = value;
90 fValueNumber = null;
91 fValueTimestamp = null;
92 if (value == null) {
93 return;
94 }
95 if (fType == Type.NUM) {
96 try {
97 fValueNumber = NumberFormat.getInstance().parse(value).doubleValue();
98 } catch (ParseException e) {
99 }
100 } else if (fType == Type.TIMESTAMP) {
101 try {
102 fValueTimestamp = new TmfTimestamp((long) (1E9 * NumberFormat.getInstance().parse(value.toString()).doubleValue()));
103 } catch (ParseException e) {
104 }
105 }
106 }
107
108 @Override
109 public String getNodeName() {
110 return NODE_NAME;
111 }
112
113 @Override
114 public boolean matches(ITmfEvent event) {
115 Object value = getFieldValue(event, fField);
116 if (value == null) {
117 return false ^ fNot;
118 }
119 if (fType == Type.NUM) {
120 if (fValueNumber instanceof Number) {
121 if (value instanceof Number) {
122 Double valueDouble = ((Number) value).doubleValue();
123 return (valueDouble.compareTo(fValueNumber.doubleValue()) == fResult) ^ fNot;
124 } else {
125 try {
126 Double valueDouble = NumberFormat.getInstance().parse(value.toString())
127 .doubleValue();
128 return (valueDouble.compareTo(fValueNumber.doubleValue()) == fResult)
129 ^ 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
145 .getInstance().parse(value.toString()).doubleValue()));
146 return (valueTimestamp.compareTo(fValueTimestamp, false) == fResult) ^ fNot;
147 } catch (ParseException e) {
148 }
149 }
150 }
151 }
152 return false ^ fNot;
153 }
154
155 @Override
156 public List<String> getValidChildren() {
157 return new ArrayList<String>(0);
158 }
159
160 @Override
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$
166 }
167
168 @Override
169 public ITmfFilterTreeNode clone() {
170 TmfFilterCompareNode clone = (TmfFilterCompareNode) super.clone();
171 clone.fField = new String(fField);
172 clone.setValue(new String(fValue));
173 return clone;
174 }
175 }
This page took 0.037146 seconds and 6 git commands to generate.