Added sorting: Comparable<DataType>, compareTo, hashCode, equals
package io.github.mzmine.datamodel.data.types;
import java.util.List;
+import java.util.Objects;
import java.util.Optional;
+import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import io.github.mzmine.datamodel.RawDataFile;
import io.github.mzmine.datamodel.data.DataTypeMap;
@@ -42,7 +44,7 @@ import javafx.scene.control.TreeTableColumn;
*
* @param <T>
*/
-public abstract class DataType<T> {
+public abstract class DataType<T> implements Comparable<DataType<T>> {
protected final T value;
@@ -50,6 +52,13 @@ public abstract class DataType<T> {
this.value = value;
}
+ /**
+ * The actual value. Might be null in some cases. However, DataTypes should try to return "empty"
+ * objects or lists instead.
+ *
+ * @return
+ */
+ @Nullable
public T getValue() {
return value;
}
@@ -57,8 +66,9 @@ public abstract class DataType<T> {
/**
* A formatted string representation of the value
*
- * @return
+ * @return the formatted representation of the value (or an empty String)
*/
+ @Nonnull
public String getFormattedString() {
if (value != null)
return value.toString();
@@ -71,16 +81,20 @@ public abstract class DataType<T> {
*
* @return
*/
+ @Nonnull
public abstract String getHeaderString();
/**
- * Create a TreeTableColumn
+ * Creates a TreeTableColumn or null if the value is not represented in a column. A
+ * {@link SubColumnsFactory} DataType can also add multiple sub columns to the main column
+ * generated by this class.
*
* @param raw null if this is a FeatureListRow column. For Feature columns: the raw data file
* specifies the feature.
*
- * @return
+ * @return the TreeTableColumn or null if this DataType.value is not represented in a column
*/
+ @Nullable
public TreeTableColumn<ModularFeatureListRow, ? extends DataType> createColumn(
final @Nullable RawDataFile raw) {
// create column
@@ -151,4 +165,32 @@ public abstract class DataType<T> {
}
return col;
}
+
+
+ @Override
+ public int compareTo(DataType<T> o) {
+ if (o == null || this.value == null || o.value == null)
+ return 0;
+ if (this.value instanceof Comparable)
+ return ((Comparable) this.getValue()).compareTo(o.getValue());
+ else
+ return getFormattedString().compareTo(o.getFormattedString());
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(getHeaderString(), value);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (!(obj instanceof DataType))
+ return false;
+ DataType other = (DataType) obj;
+ return Objects.equals(getHeaderString(), other.getHeaderString())
+ && Objects.equals(value, other.value);
+ }
+
}
