mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Implement automatic sorting for MemoryHabitList
This commit is contained in:
@@ -33,6 +33,13 @@ import java.util.*;
|
||||
*/
|
||||
public abstract class HabitList implements Iterable<Habit>
|
||||
{
|
||||
public enum Order
|
||||
{
|
||||
BY_NAME,
|
||||
BY_COLOR,
|
||||
BY_POSITION
|
||||
}
|
||||
|
||||
private ModelObservable observable;
|
||||
|
||||
@NonNull
|
||||
@@ -157,6 +164,13 @@ public abstract class HabitList implements Iterable<Habit>
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the order of the elements on the list.
|
||||
*
|
||||
* @param order the new order criterea
|
||||
*/
|
||||
public abstract void setOrder(Order order);
|
||||
|
||||
/**
|
||||
* Returns the number of habits in this list.
|
||||
*
|
||||
|
||||
@@ -25,6 +25,8 @@ import org.isoron.uhabits.models.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static org.isoron.uhabits.models.HabitList.Order.*;
|
||||
|
||||
/**
|
||||
* In-memory implementation of {@link HabitList}.
|
||||
*/
|
||||
@@ -33,6 +35,8 @@ public class MemoryHabitList extends HabitList
|
||||
@NonNull
|
||||
private LinkedList<Habit> list;
|
||||
|
||||
private Comparator<Habit> comparator = null;
|
||||
|
||||
public MemoryHabitList()
|
||||
{
|
||||
super();
|
||||
@@ -57,6 +61,7 @@ public class MemoryHabitList extends HabitList
|
||||
|
||||
if (id == null) habit.setId((long) list.size());
|
||||
list.addLast(habit);
|
||||
resort();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -82,6 +87,7 @@ public class MemoryHabitList extends HabitList
|
||||
public HabitList getFiltered(HabitMatcher matcher)
|
||||
{
|
||||
MemoryHabitList habits = new MemoryHabitList(matcher);
|
||||
habits.comparator = comparator;
|
||||
for (Habit h : this) if (matcher.matches(h)) habits.add(h);
|
||||
return habits;
|
||||
}
|
||||
@@ -112,6 +118,13 @@ public class MemoryHabitList extends HabitList
|
||||
list.add(toPos, from);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setOrder(Order order)
|
||||
{
|
||||
this.comparator = getComparatorByOrder(order);
|
||||
resort();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size()
|
||||
{
|
||||
@@ -123,4 +136,27 @@ public class MemoryHabitList extends HabitList
|
||||
{
|
||||
// NOP
|
||||
}
|
||||
|
||||
private Comparator<Habit> getComparatorByOrder(Order order)
|
||||
{
|
||||
Comparator<Habit> nameComparator =
|
||||
(h1, h2) -> h1.getName().compareTo(h2.getName());
|
||||
|
||||
Comparator<Habit> colorComparator = (h1, h2) -> {
|
||||
Integer c1 = h1.getColor();
|
||||
Integer c2 = h2.getColor();
|
||||
if (c1.equals(c2)) return nameComparator.compare(h1, h2);
|
||||
else return c1.compareTo(c2);
|
||||
};
|
||||
|
||||
if (order == BY_POSITION) return null;
|
||||
if (order == BY_NAME) return nameComparator;
|
||||
if (order == BY_COLOR) return colorComparator;
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
|
||||
private void resort()
|
||||
{
|
||||
if (comparator != null) Collections.sort(list, comparator);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -289,4 +289,10 @@ public class SQLiteHabitList extends HabitList
|
||||
super.repair();
|
||||
rebuildOrder();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setOrder(Order order)
|
||||
{
|
||||
throw new NotImplementedException("Not implemented");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ import static junit.framework.Assert.*;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.hamcrest.MatcherAssert.*;
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
import static org.isoron.uhabits.models.HabitList.Order.*;
|
||||
|
||||
@SuppressWarnings("JavaDoc")
|
||||
public class HabitListTest extends BaseUnitTest
|
||||
@@ -70,7 +71,7 @@ public class HabitListTest extends BaseUnitTest
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSize()
|
||||
public void test_size()
|
||||
{
|
||||
assertThat(habitList.size(), equalTo(10));
|
||||
}
|
||||
@@ -179,4 +180,52 @@ public class HabitListTest extends BaseUnitTest
|
||||
|
||||
MatcherAssert.assertThat(writer.toString(), equalTo(expectedCSV));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_ordering()
|
||||
{
|
||||
HabitList list = modelFactory.buildHabitList();
|
||||
Habit h1 = fixtures.createEmptyHabit();
|
||||
h1.setName("A Habit");
|
||||
h1.setColor(2);
|
||||
|
||||
Habit h2 = fixtures.createEmptyHabit();
|
||||
h2.setName("B Habit");
|
||||
h2.setColor(2);
|
||||
|
||||
Habit h3 = fixtures.createEmptyHabit();
|
||||
h3.setName("C Habit");
|
||||
h3.setColor(0);
|
||||
|
||||
Habit h4 = fixtures.createEmptyHabit();
|
||||
h4.setName("D Habit");
|
||||
h4.setColor(1);
|
||||
|
||||
list.add(h3);
|
||||
list.add(h1);
|
||||
list.add(h4);
|
||||
list.add(h2);
|
||||
|
||||
list.setOrder(BY_POSITION);
|
||||
assertThat(list.getByPosition(0), equalTo(h3));
|
||||
assertThat(list.getByPosition(1), equalTo(h1));
|
||||
assertThat(list.getByPosition(2), equalTo(h4));
|
||||
assertThat(list.getByPosition(3), equalTo(h2));
|
||||
|
||||
list.setOrder(BY_NAME);
|
||||
assertThat(list.getByPosition(0), equalTo(h1));
|
||||
assertThat(list.getByPosition(1), equalTo(h2));
|
||||
assertThat(list.getByPosition(2), equalTo(h3));
|
||||
assertThat(list.getByPosition(3), equalTo(h4));
|
||||
|
||||
list.remove(h1);
|
||||
list.add(h1);
|
||||
assertThat(list.getByPosition(0), equalTo(h1));
|
||||
|
||||
list.setOrder(BY_COLOR);
|
||||
assertThat(list.getByPosition(0), equalTo(h3));
|
||||
assertThat(list.getByPosition(1), equalTo(h4));
|
||||
assertThat(list.getByPosition(2), equalTo(h1));
|
||||
assertThat(list.getByPosition(3), equalTo(h2));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user