Simplify SQLite lists

This commit is contained in:
2017-06-19 09:23:40 -04:00
parent edeba897fb
commit 6d06e06840
15 changed files with 363 additions and 597 deletions

View File

@@ -221,4 +221,6 @@ public abstract class RepetitionList
add(new Repetition(timestamp, value));
habit.invalidateNewerThan(timestamp);
}
public abstract void removeAll();
}

View File

@@ -22,6 +22,7 @@ package org.isoron.uhabits.core.models.memory;
import android.support.annotation.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.utils.*;
import java.util.*;
@@ -60,8 +61,10 @@ public class MemoryCheckmarkList extends CheckmarkList
Checkmark oldest = getOldestComputed();
if(newest != null) newestTimestamp = newest.getTimestamp();
if(oldest != null) oldestTimestamp = oldest.getTimestamp();
long days = (newestTimestamp - oldestTimestamp) /
DateUtils.millisecondsInOneDay;
List<Checkmark> filtered = new LinkedList<>();
List<Checkmark> filtered = new ArrayList<>((int) days);
for(long time = toTimestamp; time >= fromTimestamp; time -= millisecondsInOneDay)
{
if(time > newestTimestamp || time < oldestTimestamp)

View File

@@ -25,7 +25,10 @@ import org.isoron.uhabits.core.models.*;
import java.util.*;
import static org.isoron.uhabits.core.models.HabitList.Order.*;
import static org.isoron.uhabits.core.models.HabitList.Order.BY_COLOR;
import static org.isoron.uhabits.core.models.HabitList.Order.BY_NAME;
import static org.isoron.uhabits.core.models.HabitList.Order.BY_POSITION;
import static org.isoron.uhabits.core.models.HabitList.Order.BY_SCORE;
/**
* In-memory implementation of {@link HabitList}.
@@ -55,7 +58,8 @@ public class MemoryHabitList extends HabitList
}
@Override
public void add(@NonNull Habit habit) throws IllegalArgumentException
public synchronized void add(@NonNull Habit habit)
throws IllegalArgumentException
{
if (list.contains(habit))
throw new IllegalArgumentException("habit already added");
@@ -70,7 +74,7 @@ public class MemoryHabitList extends HabitList
}
@Override
public Habit getById(long id)
public synchronized Habit getById(long id)
{
for (Habit h : list)
{
@@ -82,14 +86,14 @@ public class MemoryHabitList extends HabitList
@NonNull
@Override
public Habit getByPosition(int position)
public synchronized Habit getByPosition(int position)
{
return list.get(position);
}
@NonNull
@Override
public HabitList getFiltered(HabitMatcher matcher)
public synchronized HabitList getFiltered(HabitMatcher matcher)
{
MemoryHabitList habits = new MemoryHabitList(matcher);
habits.comparator = comparator;
@@ -98,11 +102,19 @@ public class MemoryHabitList extends HabitList
}
@Override
public Order getOrder()
public synchronized Order getOrder()
{
return order;
}
@Override
public synchronized void setOrder(@NonNull Order order)
{
this.order = order;
this.comparator = getComparatorByOrder(order);
resort();
}
@Override
public int indexOf(@NonNull Habit h)
{
@@ -116,27 +128,19 @@ public class MemoryHabitList extends HabitList
}
@Override
public void remove(@NonNull Habit habit)
public synchronized void remove(@NonNull Habit habit)
{
list.remove(habit);
}
@Override
public void reorder(Habit from, Habit to)
public synchronized void reorder(Habit from, Habit to)
{
int toPos = indexOf(to);
list.remove(from);
list.add(toPos, from);
}
@Override
public void setOrder(@NonNull Order order)
{
this.order = order;
this.comparator = getComparatorByOrder(order);
resort();
}
@Override
public int size()
{
@@ -154,14 +158,16 @@ public class MemoryHabitList extends HabitList
Comparator<Habit> nameComparator =
(h1, h2) -> h1.getName().compareTo(h2.getName());
Comparator<Habit> colorComparator = (h1, h2) -> {
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);
};
Comparator<Habit> scoreComparator = (h1, h2) -> {
Comparator<Habit> scoreComparator = (h1, h2) ->
{
double s1 = h1.getScores().getTodayValue();
double s2 = h2.getScores().getTodayValue();
return Double.compare(s2, s1);
@@ -174,7 +180,7 @@ public class MemoryHabitList extends HabitList
throw new IllegalStateException();
}
private void resort()
private synchronized void resort()
{
if (comparator != null) Collections.sort(list, comparator);
}

View File

@@ -30,12 +30,12 @@ import java.util.*;
*/
public class MemoryRepetitionList extends RepetitionList
{
LinkedList<Repetition> list;
ArrayList<Repetition> list;
public MemoryRepetitionList(Habit habit)
{
super(habit);
list = new LinkedList<>();
list = new ArrayList<>();
}
@Override
@@ -48,7 +48,7 @@ public class MemoryRepetitionList extends RepetitionList
@Override
public List<Repetition> getByInterval(long fromTimestamp, long toTimestamp)
{
LinkedList<Repetition> filtered = new LinkedList<>();
ArrayList<Repetition> filtered = new ArrayList<>();
for (Repetition r : list)
{
@@ -57,7 +57,7 @@ public class MemoryRepetitionList extends RepetitionList
}
Collections.sort(filtered,
(r1, r2) -> (int) (r1.getTimestamp() - r2.getTimestamp()));
(r1, r2) -> Long.compare(r1.getTimestamp(), r2.getTimestamp()));
return filtered;
}
@@ -122,4 +122,10 @@ public class MemoryRepetitionList extends RepetitionList
{
return list.size();
}
@Override
public void removeAll()
{
list.clear();
}
}