-/*******************************************************************************\r
- * Copyright (c) 2010 Ericsson\r
- *\r
- * All rights reserved. This program and the accompanying materials are\r
- * made available under the terms of the Eclipse Public License v1.0 which\r
- * accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * Patrick Tasse - Initial API and implementation\r
- *******************************************************************************/\r
-\r
-package org.eclipse.linuxtools.tmf.core.filter.model;\r
-\r
-import java.text.NumberFormat;\r
-import java.text.ParseException;\r
-import java.util.ArrayList;\r
-import java.util.List;\r
-\r
-import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;\r
-import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;\r
-\r
-\r
-/**\r
- * Filter node for the comparison operation\r
- *\r
- * @version 1.0\r
- * @author Patrick Tasse\r
- */\r
-@SuppressWarnings("javadoc")\r
-public class TmfFilterCompareNode extends TmfFilterTreeNode {\r
-\r
- public static final String NODE_NAME = "COMPARE"; //$NON-NLS-1$\r
- public static final String NOT_ATTR = "not"; //$NON-NLS-1$\r
- public static final String FIELD_ATTR = "field"; //$NON-NLS-1$\r
- public static final String RESULT_ATTR = "result"; //$NON-NLS-1$\r
- public static final String TYPE_ATTR = "type"; //$NON-NLS-1$\r
- public static final String VALUE_ATTR = "value"; //$NON-NLS-1$\r
-\r
- /**\r
- * Supported comparison types\r
- */\r
- public static enum Type {\r
- NUM,\r
- ALPHA,\r
- TIMESTAMP\r
- }\r
-\r
- private boolean fNot = false;\r
- private String fField;\r
- private int fResult;\r
- private Type fType = Type.NUM;\r
- private String fValue;\r
- private Number fValueNumber;\r
- private TmfTimestamp fValueTimestamp;\r
-\r
- /**\r
- * @param parent the parent node\r
- */\r
- public TmfFilterCompareNode(ITmfFilterTreeNode parent) {\r
- super(parent);\r
- }\r
-\r
- /**\r
- * @return the NOT state\r
- */\r
- public boolean isNot() {\r
- return fNot;\r
- }\r
-\r
- /**\r
- * @param not the NOT state\r
- */\r
- public void setNot(boolean not) {\r
- this.fNot = not;\r
- }\r
-\r
- /**\r
- * @return the field name\r
- */\r
- public String getField() {\r
- return fField;\r
- }\r
-\r
- /**\r
- * @param field the field name\r
- */\r
- public void setField(String field) {\r
- this.fField = field;\r
- }\r
-\r
- /**\r
- * @return the compare result\r
- */\r
- public int getResult() {\r
- return fResult;\r
- }\r
-\r
- /**\r
- * @param result the compare result\r
- */\r
- public void setResult(int result) {\r
- this.fResult = result;\r
- }\r
-\r
- /**\r
- * @return the comparison type\r
- */\r
- public Type getType() {\r
- return fType;\r
- }\r
-\r
- /**\r
- * @param type the comparison type\r
- */\r
- public void setType(Type type) {\r
- this.fType = type;\r
- setValue(fValue);\r
- }\r
-\r
- /**\r
- * @return the comparison value\r
- */\r
- public String getValue() {\r
- return fValue;\r
- }\r
-\r
- /**\r
- * @param value the comparison value\r
- */\r
- public void setValue(String value) {\r
- this.fValue = value;\r
- fValueNumber = null;\r
- fValueTimestamp = null;\r
- if (value == null) {\r
- return;\r
- }\r
- if (fType == Type.NUM) {\r
- try {\r
- fValueNumber = NumberFormat.getInstance().parse(value).doubleValue();\r
- } catch (ParseException e) {\r
- }\r
- } else if (fType == Type.TIMESTAMP) {\r
- try {\r
- fValueTimestamp = new TmfTimestamp((long) (1E9 * NumberFormat.getInstance().parse(value.toString()).doubleValue()));\r
- } catch (ParseException e) {\r
- }\r
- }\r
- }\r
-\r
- @Override\r
- public String getNodeName() {\r
- return NODE_NAME;\r
- }\r
-\r
- @Override\r
- public boolean matches(ITmfEvent event) {\r
- Object value = getFieldValue(event, fField);\r
- if (value == null) {\r
- return false ^ fNot;\r
- }\r
- if (fType == Type.NUM) {\r
- if (fValueNumber != null) {\r
- if (value instanceof Number) {\r
- Double valueDouble = ((Number) value).doubleValue();\r
- return (valueDouble.compareTo(fValueNumber.doubleValue()) == fResult) ^ fNot;\r
- }\r
- try {\r
- Double valueDouble = NumberFormat.getInstance().parse(value.toString())\r
- .doubleValue();\r
- return (valueDouble.compareTo(fValueNumber.doubleValue()) == fResult)\r
- ^ fNot;\r
- } catch (ParseException e) {\r
- }\r
- }\r
- } else if (fType == Type.ALPHA) {\r
- String valueString = value.toString();\r
- int comp = valueString.compareTo(fValue.toString());\r
- if (comp < -1) {\r
- comp = -1;\r
- } else if (comp > 1) {\r
- comp = 1;\r
- }\r
- return (comp == fResult) ^ fNot;\r
- } else if (fType == Type.TIMESTAMP) {\r
- if (fValueTimestamp != null) {\r
- if (value instanceof TmfTimestamp) {\r
- TmfTimestamp valueTimestamp = (TmfTimestamp) value;\r
- return (valueTimestamp.compareTo(fValueTimestamp, false) == fResult) ^ fNot;\r
- }\r
- try {\r
- TmfTimestamp valueTimestamp = new TmfTimestamp((long) (1E9 * NumberFormat\r
- .getInstance().parse(value.toString()).doubleValue()));\r
- return (valueTimestamp.compareTo(fValueTimestamp, false) == fResult) ^ fNot;\r
- } catch (ParseException e) {\r
- }\r
- }\r
- }\r
- return false ^ fNot;\r
- }\r
-\r
- @Override\r
- public List<String> getValidChildren() {\r
- return new ArrayList<String>(0);\r
- }\r
-\r
- @Override\r
- public String toString() {\r
- String result = (fResult == 0 ? "= " : fResult < 0 ? "< " : "> "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$\r
- String open = (fType == Type.NUM ? "" : fType == Type.ALPHA ? "\"" : "["); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$\r
- String close = (fType == Type.NUM ? "" : fType == Type.ALPHA ? "\"" : "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$\r
- return fField + (fNot ? " not " : " ") + result + open + fValue + close; //$NON-NLS-1$ //$NON-NLS-2$\r
- }\r
-\r
- @Override\r
- public ITmfFilterTreeNode clone() {\r
- TmfFilterCompareNode clone = (TmfFilterCompareNode) super.clone();\r
- clone.fField = fField;\r
- clone.setValue(fValue);\r
- return clone;\r
- }\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2010 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Patrick Tasse - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.tmf.core.filter.model;
+
+import java.text.NumberFormat;
+import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
+import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
+
+
+/**
+ * Filter node for the comparison operation
+ *
+ * @version 1.0
+ * @author Patrick Tasse
+ */
+@SuppressWarnings("javadoc")
+public class TmfFilterCompareNode extends TmfFilterTreeNode {
+
+ public static final String NODE_NAME = "COMPARE"; //$NON-NLS-1$
+ public static final String NOT_ATTR = "not"; //$NON-NLS-1$
+ public static final String FIELD_ATTR = "field"; //$NON-NLS-1$
+ public static final String RESULT_ATTR = "result"; //$NON-NLS-1$
+ public static final String TYPE_ATTR = "type"; //$NON-NLS-1$
+ public static final String VALUE_ATTR = "value"; //$NON-NLS-1$
+
+ /**
+ * Supported comparison types
+ */
+ public static enum Type {
+ NUM,
+ ALPHA,
+ TIMESTAMP
+ }
+
+ private boolean fNot = false;
+ private String fField;
+ private int fResult;
+ private Type fType = Type.NUM;
+ private String fValue;
+ private Number fValueNumber;
+ private TmfTimestamp fValueTimestamp;
+
+ /**
+ * @param parent the parent node
+ */
+ public TmfFilterCompareNode(ITmfFilterTreeNode parent) {
+ super(parent);
+ }
+
+ /**
+ * @return the NOT state
+ */
+ public boolean isNot() {
+ return fNot;
+ }
+
+ /**
+ * @param not the NOT state
+ */
+ public void setNot(boolean not) {
+ this.fNot = not;
+ }
+
+ /**
+ * @return the field name
+ */
+ public String getField() {
+ return fField;
+ }
+
+ /**
+ * @param field the field name
+ */
+ public void setField(String field) {
+ this.fField = field;
+ }
+
+ /**
+ * @return the compare result
+ */
+ public int getResult() {
+ return fResult;
+ }
+
+ /**
+ * @param result the compare result
+ */
+ public void setResult(int result) {
+ this.fResult = result;
+ }
+
+ /**
+ * @return the comparison type
+ */
+ public Type getType() {
+ return fType;
+ }
+
+ /**
+ * @param type the comparison type
+ */
+ public void setType(Type type) {
+ this.fType = type;
+ setValue(fValue);
+ }
+
+ /**
+ * @return the comparison value
+ */
+ public String getValue() {
+ return fValue;
+ }
+
+ /**
+ * @param value the comparison value
+ */
+ public void setValue(String value) {
+ this.fValue = value;
+ fValueNumber = null;
+ fValueTimestamp = null;
+ if (value == null) {
+ return;
+ }
+ if (fType == Type.NUM) {
+ try {
+ fValueNumber = NumberFormat.getInstance().parse(value).doubleValue();
+ } catch (ParseException e) {
+ }
+ } else if (fType == Type.TIMESTAMP) {
+ try {
+ fValueTimestamp = new TmfTimestamp((long) (1E9 * NumberFormat.getInstance().parse(value.toString()).doubleValue()));
+ } catch (ParseException e) {
+ }
+ }
+ }
+
+ @Override
+ public String getNodeName() {
+ return NODE_NAME;
+ }
+
+ @Override
+ public boolean matches(ITmfEvent event) {
+ Object value = getFieldValue(event, fField);
+ if (value == null) {
+ return false ^ fNot;
+ }
+ if (fType == Type.NUM) {
+ if (fValueNumber != null) {
+ if (value instanceof Number) {
+ Double valueDouble = ((Number) value).doubleValue();
+ return (valueDouble.compareTo(fValueNumber.doubleValue()) == fResult) ^ fNot;
+ }
+ try {
+ Double valueDouble = NumberFormat.getInstance().parse(value.toString())
+ .doubleValue();
+ return (valueDouble.compareTo(fValueNumber.doubleValue()) == fResult)
+ ^ fNot;
+ } catch (ParseException e) {
+ }
+ }
+ } else if (fType == Type.ALPHA) {
+ String valueString = value.toString();
+ int comp = valueString.compareTo(fValue.toString());
+ if (comp < -1) {
+ comp = -1;
+ } else if (comp > 1) {
+ comp = 1;
+ }
+ return (comp == fResult) ^ fNot;
+ } else if (fType == Type.TIMESTAMP) {
+ if (fValueTimestamp != null) {
+ if (value instanceof TmfTimestamp) {
+ TmfTimestamp valueTimestamp = (TmfTimestamp) value;
+ return (valueTimestamp.compareTo(fValueTimestamp, false) == fResult) ^ fNot;
+ }
+ try {
+ TmfTimestamp valueTimestamp = new TmfTimestamp((long) (1E9 * NumberFormat
+ .getInstance().parse(value.toString()).doubleValue()));
+ return (valueTimestamp.compareTo(fValueTimestamp, false) == fResult) ^ fNot;
+ } catch (ParseException e) {
+ }
+ }
+ }
+ return false ^ fNot;
+ }
+
+ @Override
+ public List<String> getValidChildren() {
+ return new ArrayList<String>(0);
+ }
+
+ @Override
+ public String toString() {
+ String result = (fResult == 0 ? "= " : fResult < 0 ? "< " : "> "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ String open = (fType == Type.NUM ? "" : fType == Type.ALPHA ? "\"" : "["); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ String close = (fType == Type.NUM ? "" : fType == Type.ALPHA ? "\"" : "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ return fField + (fNot ? " not " : " ") + result + open + fValue + close; //$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ @Override
+ public ITmfFilterTreeNode clone() {
+ TmfFilterCompareNode clone = (TmfFilterCompareNode) super.clone();
+ clone.fField = fField;
+ clone.setValue(fValue);
+ return clone;
+ }
+}