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;
* @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();
}
}
}
@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
* @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]);
@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;
}
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;
}