Given this class
public abstract class TLog<T> implements Serializable, Comparable<TLog> {
private LocalDateTime moment;
private String origin;
private String type;
private TableName table; // This is a simple enum
private Comparator<TLog> comparator;
private T message; // Message's type defined in child classes
public TLog() {
// comparator initialization
}
/* Getters & Setters omitted */
@Override
public int compareTo(TLog other) {
return comparator.compare(this, other);
}
}
This Comparator
initialization does compile
this.comparator = Comparator.comparing(TLog::getMoment);
this.comparator = this.getComparator()
.thenComparing(TLog::getTable)
.thenComparing(TLog::getOrigin)
.thenComparing(TLog::getType);
But this one doesn't
this.comparator = Comparator.comparing(TLog::getMoment)
.thenComparing(TLog::getTable)
.thenComparing(TLog::getOrigin)
.thenComparing(TLog::getType);
And I don't understand why.
Here's the related compilation error on Comparator.comparing()
Non-static method cannot be referenced from a static context
Edit 1 : Hard-typing comparing
does work
this.comparator = Comparator.<TLog, LocalDateTime>comparing(TLog::getMoment)
.thenComparing(TLog::getTable)
.thenComparing(TLog::getOrigin)
.thenComparing(TLog::getType);
But I still don't get why my previous implementation doesn't work