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 abstract class HabitList implements Iterable<Habit>
|
||||||
{
|
{
|
||||||
|
public enum Order
|
||||||
|
{
|
||||||
|
BY_NAME,
|
||||||
|
BY_COLOR,
|
||||||
|
BY_POSITION
|
||||||
|
}
|
||||||
|
|
||||||
private ModelObservable observable;
|
private ModelObservable observable;
|
||||||
|
|
||||||
@NonNull
|
@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.
|
* Returns the number of habits in this list.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -25,6 +25,8 @@ import org.isoron.uhabits.models.*;
|
|||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
import static org.isoron.uhabits.models.HabitList.Order.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* In-memory implementation of {@link HabitList}.
|
* In-memory implementation of {@link HabitList}.
|
||||||
*/
|
*/
|
||||||
@@ -33,6 +35,8 @@ public class MemoryHabitList extends HabitList
|
|||||||
@NonNull
|
@NonNull
|
||||||
private LinkedList<Habit> list;
|
private LinkedList<Habit> list;
|
||||||
|
|
||||||
|
private Comparator<Habit> comparator = null;
|
||||||
|
|
||||||
public MemoryHabitList()
|
public MemoryHabitList()
|
||||||
{
|
{
|
||||||
super();
|
super();
|
||||||
@@ -57,6 +61,7 @@ public class MemoryHabitList extends HabitList
|
|||||||
|
|
||||||
if (id == null) habit.setId((long) list.size());
|
if (id == null) habit.setId((long) list.size());
|
||||||
list.addLast(habit);
|
list.addLast(habit);
|
||||||
|
resort();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -82,7 +87,8 @@ public class MemoryHabitList extends HabitList
|
|||||||
public HabitList getFiltered(HabitMatcher matcher)
|
public HabitList getFiltered(HabitMatcher matcher)
|
||||||
{
|
{
|
||||||
MemoryHabitList habits = new MemoryHabitList(matcher);
|
MemoryHabitList habits = new MemoryHabitList(matcher);
|
||||||
for(Habit h : this) if (matcher.matches(h)) habits.add(h);
|
habits.comparator = comparator;
|
||||||
|
for (Habit h : this) if (matcher.matches(h)) habits.add(h);
|
||||||
return habits;
|
return habits;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,6 +118,13 @@ public class MemoryHabitList extends HabitList
|
|||||||
list.add(toPos, from);
|
list.add(toPos, from);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setOrder(Order order)
|
||||||
|
{
|
||||||
|
this.comparator = getComparatorByOrder(order);
|
||||||
|
resort();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int size()
|
public int size()
|
||||||
{
|
{
|
||||||
@@ -123,4 +136,27 @@ public class MemoryHabitList extends HabitList
|
|||||||
{
|
{
|
||||||
// NOP
|
// 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();
|
super.repair();
|
||||||
rebuildOrder();
|
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.CoreMatchers.*;
|
||||||
import static org.hamcrest.MatcherAssert.*;
|
import static org.hamcrest.MatcherAssert.*;
|
||||||
import static org.hamcrest.core.IsEqual.equalTo;
|
import static org.hamcrest.core.IsEqual.equalTo;
|
||||||
|
import static org.isoron.uhabits.models.HabitList.Order.*;
|
||||||
|
|
||||||
@SuppressWarnings("JavaDoc")
|
@SuppressWarnings("JavaDoc")
|
||||||
public class HabitListTest extends BaseUnitTest
|
public class HabitListTest extends BaseUnitTest
|
||||||
@@ -70,7 +71,7 @@ public class HabitListTest extends BaseUnitTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSize()
|
public void test_size()
|
||||||
{
|
{
|
||||||
assertThat(habitList.size(), equalTo(10));
|
assertThat(habitList.size(), equalTo(10));
|
||||||
}
|
}
|
||||||
@@ -179,4 +180,52 @@ public class HabitListTest extends BaseUnitTest
|
|||||||
|
|
||||||
MatcherAssert.assertThat(writer.toString(), equalTo(expectedCSV));
|
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