|
|
@ -35,10 +35,13 @@ public class MemoryHabitList extends HabitList
|
|
|
|
@NonNull
|
|
|
|
@NonNull
|
|
|
|
private LinkedList<Habit> list = new LinkedList<>();
|
|
|
|
private LinkedList<Habit> list = new LinkedList<>();
|
|
|
|
|
|
|
|
|
|
|
|
private Comparator<Habit> comparator = null;
|
|
|
|
@NonNull
|
|
|
|
|
|
|
|
private Order primaryOrder = Order.BY_POSITION;
|
|
|
|
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@NonNull
|
|
|
|
private Order order = Order.BY_POSITION;
|
|
|
|
private Order secondaryOrder = Order.BY_NAME_ASC;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Comparator<Habit> comparator = getComposedComparatorByOrder(primaryOrder, secondaryOrder);
|
|
|
|
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Nullable
|
|
|
|
private MemoryHabitList parent = null;
|
|
|
|
private MemoryHabitList parent = null;
|
|
|
@ -55,7 +58,8 @@ public class MemoryHabitList extends HabitList
|
|
|
|
super(matcher);
|
|
|
|
super(matcher);
|
|
|
|
this.parent = parent;
|
|
|
|
this.parent = parent;
|
|
|
|
this.comparator = comparator;
|
|
|
|
this.comparator = comparator;
|
|
|
|
this.order = parent.order;
|
|
|
|
this.primaryOrder = parent.primaryOrder;
|
|
|
|
|
|
|
|
this.secondaryOrder = parent.secondaryOrder;
|
|
|
|
parent.getObservable().addListener(this::loadFromParent);
|
|
|
|
parent.getObservable().addListener(this::loadFromParent);
|
|
|
|
loadFromParent();
|
|
|
|
loadFromParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -105,58 +109,88 @@ public class MemoryHabitList extends HabitList
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public synchronized Order getOrder()
|
|
|
|
public synchronized Order getPrimaryOrder()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return primaryOrder;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public synchronized Order getSecondaryOrder()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return order;
|
|
|
|
return secondaryOrder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public synchronized void setOrder(@NonNull Order order)
|
|
|
|
public synchronized void setPrimaryOrder(@NonNull Order order)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
this.order = order;
|
|
|
|
this.primaryOrder = order;
|
|
|
|
this.comparator = getComparatorByOrder(order);
|
|
|
|
this.comparator = getComposedComparatorByOrder(this.primaryOrder, this.secondaryOrder);
|
|
|
|
resort();
|
|
|
|
resort();
|
|
|
|
getObservable().notifyListeners();
|
|
|
|
getObservable().notifyListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private Comparator<Habit> getComparatorByOrder(Order order)
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void setSecondaryOrder(@NonNull Order order)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Comparator<Habit> nameComparatorAsc =
|
|
|
|
this.secondaryOrder = order;
|
|
|
|
(h1, h2) -> h1.getName().compareTo(h2.getName());
|
|
|
|
this.comparator = getComposedComparatorByOrder(this.primaryOrder, this.secondaryOrder);
|
|
|
|
|
|
|
|
resort();
|
|
|
|
Comparator<Habit> nameComparatorDesc =
|
|
|
|
getObservable().notifyListeners();
|
|
|
|
(h1, h2) -> nameComparatorAsc.compare(h2, h1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Comparator<Habit> colorComparatorAsc = (h1, h2) ->
|
|
|
|
private Comparator<Habit> getComposedComparatorByOrder(Order firstOrder, Order secondOrder)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Integer c1 = h1.getColor();
|
|
|
|
return (h1, h2) -> {
|
|
|
|
Integer c2 = h2.getColor();
|
|
|
|
int firstResult = getComparatorByOrder(firstOrder).compare(h1, h2);
|
|
|
|
if (c1.equals(c2)) return nameComparatorAsc.compare(h1, h2);
|
|
|
|
|
|
|
|
else return c1.compareTo(c2);
|
|
|
|
if (firstResult != 0 || secondOrder == null) {
|
|
|
|
|
|
|
|
return firstResult;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return getComparatorByOrder(secondOrder).compare(h1, h2);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Comparator<Habit> getComparatorByOrder(Order order) {
|
|
|
|
|
|
|
|
Comparator<Habit> nameComparatorAsc = (h1, h2) ->
|
|
|
|
|
|
|
|
h1.getName().compareTo(h2.getName());
|
|
|
|
|
|
|
|
|
|
|
|
Comparator<Habit> colorComparatorDesc =
|
|
|
|
Comparator<Habit> nameComparatorDesc = (h1, h2) ->
|
|
|
|
(h1, h2) -> colorComparatorAsc.compare(h2, h1);
|
|
|
|
nameComparatorAsc.compare(h2, h1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Comparator<Habit> colorComparatorAsc = (h1, h2) ->
|
|
|
|
|
|
|
|
h1.getColor().compareTo(h2.getColor());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Comparator<Habit> colorComparatorDesc = (h1, h2) ->
|
|
|
|
|
|
|
|
colorComparatorAsc.compare(h2, h1);
|
|
|
|
|
|
|
|
|
|
|
|
Comparator<Habit> scoreComparatorDesc = (h1, h2) ->
|
|
|
|
Comparator<Habit> scoreComparatorDesc = (h1, h2) ->
|
|
|
|
{
|
|
|
|
Double.compare(h1.getScores().getTodayValue(), h2.getScores().getTodayValue());
|
|
|
|
Double s1 = h1.getScores().getTodayValue();
|
|
|
|
|
|
|
|
Double s2 = h2.getScores().getTodayValue();
|
|
|
|
|
|
|
|
if (s1.equals(s2)) return nameComparatorAsc.compare(h1, h2);
|
|
|
|
|
|
|
|
else return s2.compareTo(s1);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Comparator<Habit> scoreComparatorAsc =
|
|
|
|
Comparator<Habit> scoreComparatorAsc = (h1, h2) ->
|
|
|
|
(h1, h2) -> scoreComparatorDesc.compare(h2, h1);
|
|
|
|
scoreComparatorDesc.compare(h2, h1);
|
|
|
|
|
|
|
|
|
|
|
|
Comparator<Habit> positionComparator = (h1, h2) ->
|
|
|
|
Comparator<Habit> positionComparator = (h1, h2) ->
|
|
|
|
|
|
|
|
h1.getPosition().compareTo(h2.getPosition());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Comparator<Habit> statusComparatorDesc = (h1, h2) ->
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Integer p1 = h1.getPosition();
|
|
|
|
if (h1.isCompletedToday() != h2.isCompletedToday()) {
|
|
|
|
Integer p2 = h2.getPosition();
|
|
|
|
return h1.isCompletedToday() ? -1 : 1;
|
|
|
|
if (p1.equals(p2)) return nameComparatorAsc.compare(h1, h2);
|
|
|
|
}
|
|
|
|
else return p1.compareTo(p2);
|
|
|
|
|
|
|
|
|
|
|
|
if (h1.isNumerical() != h2.isNumerical()) {
|
|
|
|
|
|
|
|
return h1.isNumerical() ? -1 : 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Integer v1 = Objects.requireNonNull(h1.getCheckmarks().getToday()).getValue();
|
|
|
|
|
|
|
|
Integer v2 = Objects.requireNonNull(h2.getCheckmarks().getToday()).getValue();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return v2.compareTo(v1);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Comparator<Habit> statusComparatorAsc = (h1, h2) -> statusComparatorDesc.compare(h2, h1);
|
|
|
|
|
|
|
|
|
|
|
|
if (order == BY_POSITION) return positionComparator;
|
|
|
|
if (order == BY_POSITION) return positionComparator;
|
|
|
|
if (order == BY_NAME_ASC) return nameComparatorAsc;
|
|
|
|
if (order == BY_NAME_ASC) return nameComparatorAsc;
|
|
|
|
if (order == BY_NAME_DESC) return nameComparatorDesc;
|
|
|
|
if (order == BY_NAME_DESC) return nameComparatorDesc;
|
|
|
@ -164,6 +198,8 @@ public class MemoryHabitList extends HabitList
|
|
|
|
if (order == BY_COLOR_DESC) return colorComparatorDesc;
|
|
|
|
if (order == BY_COLOR_DESC) return colorComparatorDesc;
|
|
|
|
if (order == BY_SCORE_DESC) return scoreComparatorDesc;
|
|
|
|
if (order == BY_SCORE_DESC) return scoreComparatorDesc;
|
|
|
|
if (order == BY_SCORE_ASC) return scoreComparatorAsc;
|
|
|
|
if (order == BY_SCORE_ASC) return scoreComparatorAsc;
|
|
|
|
|
|
|
|
if (order == BY_STATUS_DESC) return statusComparatorDesc;
|
|
|
|
|
|
|
|
if (order == BY_STATUS_ASC) return statusComparatorAsc;
|
|
|
|
throw new IllegalStateException();
|
|
|
|
throw new IllegalStateException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -192,7 +228,7 @@ public class MemoryHabitList extends HabitList
|
|
|
|
public synchronized void reorder(@NonNull Habit from, @NonNull Habit to)
|
|
|
|
public synchronized void reorder(@NonNull Habit from, @NonNull Habit to)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
throwIfHasParent();
|
|
|
|
throwIfHasParent();
|
|
|
|
if (order != BY_POSITION) throw new IllegalStateException(
|
|
|
|
if (primaryOrder != BY_POSITION) throw new IllegalStateException(
|
|
|
|
"cannot reorder automatically sorted list");
|
|
|
|
"cannot reorder automatically sorted list");
|
|
|
|
|
|
|
|
|
|
|
|
if (indexOf(from) < 0) throw new IllegalArgumentException(
|
|
|
|
if (indexOf(from) < 0) throw new IllegalArgumentException(
|
|
|
|