Remove MemoryRepetitionList

pull/699/head
Alinson S. Xavier 5 years ago
parent 7fcad7b5c5
commit d9be39b839

@ -25,11 +25,13 @@ import org.isoron.uhabits.core.utils.*;
import java.util.*;
public abstract class RepetitionList
public class RepetitionList
{
@NonNull
protected final Habit habit;
private final ArrayList<Entry> list = new ArrayList<>();
public RepetitionList(@NonNull Habit habit)
{
this.habit = habit;
@ -43,7 +45,10 @@ public abstract class RepetitionList
*
* @param entry the checkmark to be added.
*/
public abstract void add(Entry entry);
public void add(Entry entry)
{
list.add(entry);
}
/**
* Returns the list of checkmarks that happened within the given time
@ -57,8 +62,22 @@ public abstract class RepetitionList
* @param toTimestamp timestamp of the end of the interval
* @return list of checkmarks within given time interval
*/
public abstract List<Entry> getByInterval(Timestamp fromTimestamp,
Timestamp toTimestamp);
public List<Entry> getByInterval(Timestamp fromTimestamp, Timestamp toTimestamp)
{
ArrayList<Entry> filtered = new ArrayList<>();
for (Entry r : list)
{
Timestamp t = r.getTimestamp();
if (t.isOlderThan(fromTimestamp) || t.isNewerThan(toTimestamp)) continue;
filtered.add(r);
}
Collections.sort(filtered,
(r1, r2) -> r1.getTimestamp().compare(r2.getTimestamp()));
return filtered;
}
/**
* Returns the checkmark that has the given timestamp, or null if none
@ -68,7 +87,13 @@ public abstract class RepetitionList
* @return the checkmark that has the given timestamp.
*/
@Nullable
public abstract Entry getByTimestamp(Timestamp timestamp);
public Entry getByTimestamp(Timestamp timestamp)
{
for (Entry r : list)
if (r.getTimestamp().equals(timestamp)) return r;
return null;
}
/**
* If a checkmark with the given timestamp exists, return its value. Otherwise, returns
@ -91,9 +116,23 @@ public abstract class RepetitionList
* @return oldest checkmark in the list, or null if list is empty.
*/
@Nullable
public abstract Entry getOldest();
public Entry getOldest()
{
Timestamp oldestTimestamp = Timestamp.ZERO.plus(1000000);
Entry oldestRep = null;
for (Entry rep : list)
{
if (rep.getTimestamp().isOlderThan(oldestTimestamp))
{
oldestRep = rep;
oldestTimestamp = rep.getTimestamp();
}
}
return oldestRep;
}
@Nullable
/**
* Returns the newest checkmark in the list.
* <p>
@ -102,7 +141,23 @@ public abstract class RepetitionList
*
* @return newest checkmark in the list, or null if list is empty.
*/
public abstract Entry getNewest();
@Nullable
public Entry getNewest()
{
Timestamp newestTimestamp = Timestamp.ZERO;
Entry newestRep = null;
for (Entry rep : list)
{
if (rep.getTimestamp().isNewerThan(newestTimestamp))
{
newestRep = rep;
newestTimestamp = rep.getTimestamp();
}
}
return newestRep;
}
/**
* Returns the total number of successful checkmarks for each month, from the first
@ -159,9 +214,20 @@ public abstract class RepetitionList
*
* @param entry the checkmark to be removed
*/
public abstract void remove(@NonNull Entry entry);
public void remove(@NonNull Entry entry)
{
list.remove(entry);
}
public abstract long getTotalCount();
public long getTotalCount()
{
int count = 0;
for (Entry rep : list)
if (rep.getValue() == Entry.YES_MANUAL)
count++;
return count;
}
public void setValue(Timestamp timestamp, int value)
{
@ -171,5 +237,8 @@ public abstract class RepetitionList
habit.invalidateNewerThan(timestamp);
}
public abstract void removeAll();
public void removeAll()
{
list.clear();
}
}

@ -40,7 +40,7 @@ public class MemoryModelFactory implements ModelFactory
@Override
public RepetitionList buildRepetitionList(Habit habit)
{
return new MemoryRepetitionList(habit);
return new RepetitionList(habit);
}
@Override

@ -1,134 +0,0 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.core.models.memory;
import androidx.annotation.*;
import org.isoron.uhabits.core.models.*;
import java.util.*;
/**
* In-memory implementation of {@link RepetitionList}.
*/
public class MemoryRepetitionList extends RepetitionList
{
ArrayList<Entry> list;
public MemoryRepetitionList(Habit habit)
{
super(habit);
list = new ArrayList<>();
}
@Override
public void add(Entry repetition)
{
list.add(repetition);
}
@Override
public List<Entry> getByInterval(Timestamp fromTimestamp, Timestamp toTimestamp)
{
ArrayList<Entry> filtered = new ArrayList<>();
for (Entry r : list)
{
Timestamp t = r.getTimestamp();
if (t.isOlderThan(fromTimestamp) || t.isNewerThan(toTimestamp)) continue;
filtered.add(r);
}
Collections.sort(filtered,
(r1, r2) -> r1.getTimestamp().compare(r2.getTimestamp()));
return filtered;
}
@Nullable
@Override
public Entry getByTimestamp(Timestamp timestamp)
{
for (Entry r : list)
if (r.getTimestamp().equals(timestamp)) return r;
return null;
}
@Nullable
@Override
public Entry getOldest()
{
Timestamp oldestTimestamp = Timestamp.ZERO.plus(1000000);
Entry oldestRep = null;
for (Entry rep : list)
{
if (rep.getTimestamp().isOlderThan(oldestTimestamp))
{
oldestRep = rep;
oldestTimestamp = rep.getTimestamp();
}
}
return oldestRep;
}
@Nullable
@Override
public Entry getNewest()
{
Timestamp newestTimestamp = Timestamp.ZERO;
Entry newestRep = null;
for (Entry rep : list)
{
if (rep.getTimestamp().isNewerThan(newestTimestamp))
{
newestRep = rep;
newestTimestamp = rep.getTimestamp();
}
}
return newestRep;
}
@Override
public void remove(@NonNull Entry repetition)
{
list.remove(repetition);
}
@Override
public long getTotalCount()
{
int count = 0;
for (Entry rep : list)
if (rep.getValue() == Entry.YES_MANUAL)
count++;
return count;
}
@Override
public void removeAll()
{
list.clear();
}
}

@ -21,12 +21,11 @@
package org.isoron.uhabits.core.models.sqlite;
import androidx.annotation.*;
import androidx.annotation.Nullable;
import androidx.annotation.*;
import org.isoron.uhabits.core.database.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.models.memory.*;
import org.isoron.uhabits.core.models.sqlite.records.*;
import org.jetbrains.annotations.*;
@ -39,8 +38,6 @@ public class SQLiteRepetitionList extends RepetitionList
{
private final Repository<RepetitionRecord> repository;
private final MemoryRepetitionList list;
private boolean loaded = false;
public SQLiteRepetitionList(@NonNull Habit habit,
@ -48,7 +45,6 @@ public class SQLiteRepetitionList extends RepetitionList
{
super(habit);
repository = modelFactory.buildRepetitionListRepository();
list = new MemoryRepetitionList(habit);
}
private void loadRecords()
@ -62,14 +58,14 @@ public class SQLiteRepetitionList extends RepetitionList
habit.getId().toString());
for (RepetitionRecord rec : records)
list.add(rec.toCheckmark());
super.add(rec.toCheckmark());
}
@Override
public void add(Entry entry)
{
loadRecords();
list.add(entry);
super.add(entry);
check(habit.getId());
RepetitionRecord record = new RepetitionRecord();
record.habit_id = habit.getId();
@ -81,7 +77,7 @@ public class SQLiteRepetitionList extends RepetitionList
public List<Entry> getByInterval(Timestamp timeFrom, Timestamp timeTo)
{
loadRecords();
return list.getByInterval(timeFrom, timeTo);
return super.getByInterval(timeFrom, timeTo);
}
@Override
@ -89,28 +85,28 @@ public class SQLiteRepetitionList extends RepetitionList
public Entry getByTimestamp(Timestamp timestamp)
{
loadRecords();
return list.getByTimestamp(timestamp);
return super.getByTimestamp(timestamp);
}
@Override
public Entry getOldest()
{
loadRecords();
return list.getOldest();
return super.getOldest();
}
@Override
public Entry getNewest()
{
loadRecords();
return list.getNewest();
return super.getNewest();
}
@Override
public void remove(@NonNull Entry entry)
{
loadRecords();
list.remove(entry);
super.remove(entry);
check(habit.getId());
repository.execSQL(
"delete from repetitions where habit = ? and timestamp = ?",
@ -120,7 +116,7 @@ public class SQLiteRepetitionList extends RepetitionList
public void removeAll()
{
loadRecords();
list.removeAll();
super.removeAll();
check(habit.getId());
repository.execSQL("delete from repetitions where habit = ?",
habit.getId());
@ -130,12 +126,7 @@ public class SQLiteRepetitionList extends RepetitionList
public long getTotalCount()
{
loadRecords();
return list.getTotalCount();
}
public void reload()
{
loaded = false;
return super.getTotalCount();
}
@Contract("null -> fail")

Loading…
Cancel
Save