tmf: Fix most Number unboxing problems
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / filter / model / TmfFilterCompareNode.java
index fb9bb56c185bc3fe194ec5d6e91317fcfef7f68a..debb79522478fed6e4a836c356f856dcf03a24e9 100644 (file)
-/*******************************************************************************\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.TmfEvent;\r
-import org.eclipse.linuxtools.tmf.core.event.TmfNoSuchFieldException;\r
-import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;\r
-\r
-\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
-       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
-       public TmfFilterCompareNode(ITmfFilterTreeNode parent) {\r
-               super(parent);\r
-       }\r
-\r
-       public boolean isNot() {\r
-               return fNot;\r
-       }\r
-       \r
-       public void setNot(boolean not) {\r
-               this.fNot = not;\r
-       }\r
-       \r
-       public String getField() {\r
-               return fField;\r
-       }\r
-\r
-       public void setField(String field) {\r
-               this.fField = field;\r
-       }\r
-\r
-       public int getResult() {\r
-               return fResult;\r
-       }\r
-       \r
-       public void setResult(int result) {\r
-               this.fResult = result;\r
-       }\r
-       \r
-       public Type getType() {\r
-               return fType;\r
-       }\r
-\r
-       public void setType(Type type) {\r
-               this.fType = type;\r
-               setValue(fValue);\r
-       }\r
-\r
-       public String getValue() {\r
-               return fValue;\r
-       }\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(TmfEvent event) {\r
-               try {\r
-                       Object value = event.getContent().getField(fField);\r
-                       if (value == null) {\r
-                               return false ^ fNot;\r
-                       }\r
-                       if (fType == Type.NUM) {\r
-                               if (fValueNumber instanceof Number) {\r
-                                       if (value instanceof Number) {\r
-                                               Double valueDouble = ((Number) value).doubleValue();\r
-                                               return (valueDouble.compareTo(fValueNumber.doubleValue()) == fResult) ^ fNot;\r
-                                       } else {\r
-                                               try {\r
-                                                       Double valueDouble = NumberFormat.getInstance().parse(value.toString()).doubleValue();\r
-                                                       return (valueDouble.compareTo(fValueNumber.doubleValue()) == fResult) ^ fNot;\r
-                                               } catch (ParseException e) {\r
-                                               }\r
-                                       }\r
-                               }\r
-                       } else if (fType == Type.ALPHA) {\r
-                               String valueString = value.toString();\r
-                               return (valueString.compareTo(fValue.toString()) == fResult) ^ fNot;\r
-                       } else if (fType == Type.TIMESTAMP) {\r
-                               if (fValueTimestamp instanceof TmfTimestamp) {\r
-                                       if (value instanceof TmfTimestamp) {\r
-                                               TmfTimestamp valueTimestamp = (TmfTimestamp) value;\r
-                                               return (valueTimestamp.compareTo(fValueTimestamp, false) == fResult) ^ fNot;\r
-                                       } else {\r
-                                               try {\r
-                                                       TmfTimestamp valueTimestamp = new TmfTimestamp((long) (1E9 * NumberFormat.getInstance().parse(value.toString()).doubleValue()));\r
-                                                       return (valueTimestamp.compareTo(fValueTimestamp, false) == fResult) ^ fNot;\r
-                                               } catch (ParseException e) {\r
-                                               }\r
-                                       }\r
-                               }\r
-                       }\r
-               } catch (TmfNoSuchFieldException e) {\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 = new String(fField);\r
-               clone.setValue(new String(fValue));\r
-               return clone;\r
-       }\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2010, 2013 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.timestamp.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 (Double.compare(valueDouble, fValueNumber.doubleValue()) == fResult) ^ fNot;
+                }
+                try {
+                    double valueDouble = NumberFormat.getInstance().parse(value.toString()).doubleValue();
+                    return (Double.compare(valueDouble, 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<>(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;
+       }
+}
This page took 0.043632 seconds and 5 git commands to generate.