tmf: Fix the actual end time of state system modules
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / event / TmfEventField.java
index 6ae877bbfbe42b78ac2900a34e3b676dbcd65498..1ad95b3a6bf40721c3735ec35c158fd5571d391c 100644 (file)
@@ -17,11 +17,14 @@ package org.eclipse.tracecompass.tmf.core.event;
 
 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
 
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Map;
 
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
+import org.eclipse.tracecompass.common.core.NonNullUtils;
+import org.eclipse.tracecompass.common.core.ObjectUtils;
 
 import com.google.common.base.Joiner;
 import com.google.common.collect.ImmutableMap;
@@ -64,20 +67,16 @@ public class TmfEventField implements ITmfEventField {
      * @throws IllegalArgumentException
      *             If 'name' is null, or if 'fields' has duplicate field names.
      */
-    public TmfEventField(@NonNull String name, @Nullable Object value, @Nullable ITmfEventField[] fields) {
+    public TmfEventField(@NonNull String name, @Nullable Object value, ITmfEventField @Nullable [] fields) {
         fName = name;
         fValue = value;
 
         if (fields == null) {
-            fFields = checkNotNull(ImmutableMap.<String, ITmfEventField> of());
+            fFields = ImmutableMap.of();
         } else {
-            /* Java 8 streams will make this even more simple! */
             ImmutableMap.Builder<String, ITmfEventField> mapBuilder = new ImmutableMap.Builder<>();
-            for (ITmfEventField field : fields) {
-                final String curName = field.getName();
-                mapBuilder.put(curName, field);
-            }
-            fFields = checkNotNull(mapBuilder.build());
+            Arrays.stream(fields).forEach(t -> mapBuilder.put(t.getName(), t));
+            fFields = mapBuilder.build();
         }
     }
 
@@ -110,13 +109,13 @@ public class TmfEventField implements ITmfEventField {
     }
 
     @Override
-    public Collection<String> getFieldNames() {
-        return checkNotNull(fFields.keySet());
+    public final Collection<String> getFieldNames() {
+        return fFields.keySet();
     }
 
     @Override
-    public Collection<ITmfEventField> getFields() {
-        return checkNotNull(fFields.values());
+    public final Collection<ITmfEventField> getFields() {
+        return fFields.values();
     }
 
     @Override
@@ -144,7 +143,7 @@ public class TmfEventField implements ITmfEventField {
      * @param labels the list of labels
      * @return the (flat) root list
      */
-    public static final ITmfEventField makeRoot(final String[] labels) {
+    public static final @NonNull ITmfEventField makeRoot(final String[] labels) {
         final ITmfEventField[] fields = new ITmfEventField[labels.length];
         for (int i = 0; i < labels.length; i++) {
             String label = checkNotNull(labels[i]);
@@ -160,11 +159,10 @@ public class TmfEventField implements ITmfEventField {
 
     @Override
     public int hashCode() {
-        Object value = fValue;
         final int prime = 31;
         int result = 1;
-        result = prime * result + fName.hashCode();
-        result = prime * result + ((value == null) ? 0 : value.hashCode());
+        result = prime * result + getName().hashCode();
+        result = prime * result + ObjectUtils.deepHashCode(getValue());
         result = prime * result + fFields.hashCode();
         return result;
     }
@@ -177,28 +175,29 @@ public class TmfEventField implements ITmfEventField {
         if (obj == null) {
             return false;
         }
-        if (!(obj instanceof TmfEventField)) {
+
+        /* We only consider equals fields of the exact same class. */
+        if (!(this.getClass().equals(obj.getClass()))) {
             return false;
         }
 
         final TmfEventField other = (TmfEventField) obj;
 
-        /* Check that 'fName' is the same */
-        if (!fName.equals(other.fName)) {
+        /* Check that the field names are the same. */
+        if (!NonNullUtils.equalsNullable(getName(), other.getName())) {
             return false;
         }
 
-        /* Check that 'fValue' is the same */
-        Object value = this.fValue;
-        if (value == null) {
-            if (other.fValue != null) {
-                return false;
-            }
-        } else if (!value.equals(other.fValue)) {
+        /*
+         * Check that the field values are the same. We use ObjectUtils to
+         * handle cases where the Object values may be primitive and/or nested
+         * arrays.
+         */
+        if (!ObjectUtils.deepEquals(this.getValue(), other.getValue())) {
             return false;
         }
 
-        /* Check that 'fFields' are the same */
+        /* Check that sub-fields are the same. */
         if (!fFields.equals(other.fFields)) {
             return false;
         }
This page took 0.025374 seconds and 5 git commands to generate.