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.*; import java.util.*;
public abstract class RepetitionList public class RepetitionList
{ {
@NonNull @NonNull
protected final Habit habit; protected final Habit habit;
private final ArrayList<Entry> list = new ArrayList<>();
public RepetitionList(@NonNull Habit habit) public RepetitionList(@NonNull Habit habit)
{ {
this.habit = habit; this.habit = habit;
@ -43,7 +45,10 @@ public abstract class RepetitionList
* *
* @param entry the checkmark to be added. * @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 * 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 * @param toTimestamp timestamp of the end of the interval
* @return list of checkmarks within given time interval * @return list of checkmarks within given time interval
*/ */
public abstract List<Entry> getByInterval(Timestamp fromTimestamp, public List<Entry> getByInterval(Timestamp fromTimestamp, Timestamp toTimestamp)
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 * 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. * @return the checkmark that has the given timestamp.
*/ */
@Nullable @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 * 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. * @return oldest checkmark in the list, or null if list is empty.
*/ */
@Nullable @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. * Returns the newest checkmark in the list.
* <p> * <p>
@ -102,7 +141,23 @@ public abstract class RepetitionList
* *
* @return newest checkmark in the list, or null if list is empty. * @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 * 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 * @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) public void setValue(Timestamp timestamp, int value)
{ {
@ -171,5 +237,8 @@ public abstract class RepetitionList
habit.invalidateNewerThan(timestamp); habit.invalidateNewerThan(timestamp);
} }
public abstract void removeAll(); public void removeAll()
{
list.clear();
}
} }

@ -40,7 +40,7 @@ public class MemoryModelFactory implements ModelFactory
@Override @Override
public RepetitionList buildRepetitionList(Habit habit) public RepetitionList buildRepetitionList(Habit habit)
{ {
return new MemoryRepetitionList(habit); return new RepetitionList(habit);
} }
@Override @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; package org.isoron.uhabits.core.models.sqlite;
import androidx.annotation.*;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.*;
import org.isoron.uhabits.core.database.*; import org.isoron.uhabits.core.database.*;
import org.isoron.uhabits.core.models.*; import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.models.memory.*;
import org.isoron.uhabits.core.models.sqlite.records.*; import org.isoron.uhabits.core.models.sqlite.records.*;
import org.jetbrains.annotations.*; import org.jetbrains.annotations.*;
@ -39,8 +38,6 @@ public class SQLiteRepetitionList extends RepetitionList
{ {
private final Repository<RepetitionRecord> repository; private final Repository<RepetitionRecord> repository;
private final MemoryRepetitionList list;
private boolean loaded = false; private boolean loaded = false;
public SQLiteRepetitionList(@NonNull Habit habit, public SQLiteRepetitionList(@NonNull Habit habit,
@ -48,7 +45,6 @@ public class SQLiteRepetitionList extends RepetitionList
{ {
super(habit); super(habit);
repository = modelFactory.buildRepetitionListRepository(); repository = modelFactory.buildRepetitionListRepository();
list = new MemoryRepetitionList(habit);
} }
private void loadRecords() private void loadRecords()
@ -58,18 +54,18 @@ public class SQLiteRepetitionList extends RepetitionList
check(habit.getId()); check(habit.getId());
List<RepetitionRecord> records = List<RepetitionRecord> records =
repository.findAll("where habit = ? order by timestamp", repository.findAll("where habit = ? order by timestamp",
habit.getId().toString()); habit.getId().toString());
for (RepetitionRecord rec : records) for (RepetitionRecord rec : records)
list.add(rec.toCheckmark()); super.add(rec.toCheckmark());
} }
@Override @Override
public void add(Entry entry) public void add(Entry entry)
{ {
loadRecords(); loadRecords();
list.add(entry); super.add(entry);
check(habit.getId()); check(habit.getId());
RepetitionRecord record = new RepetitionRecord(); RepetitionRecord record = new RepetitionRecord();
record.habit_id = habit.getId(); record.habit_id = habit.getId();
@ -81,7 +77,7 @@ public class SQLiteRepetitionList extends RepetitionList
public List<Entry> getByInterval(Timestamp timeFrom, Timestamp timeTo) public List<Entry> getByInterval(Timestamp timeFrom, Timestamp timeTo)
{ {
loadRecords(); loadRecords();
return list.getByInterval(timeFrom, timeTo); return super.getByInterval(timeFrom, timeTo);
} }
@Override @Override
@ -89,53 +85,48 @@ public class SQLiteRepetitionList extends RepetitionList
public Entry getByTimestamp(Timestamp timestamp) public Entry getByTimestamp(Timestamp timestamp)
{ {
loadRecords(); loadRecords();
return list.getByTimestamp(timestamp); return super.getByTimestamp(timestamp);
} }
@Override @Override
public Entry getOldest() public Entry getOldest()
{ {
loadRecords(); loadRecords();
return list.getOldest(); return super.getOldest();
} }
@Override @Override
public Entry getNewest() public Entry getNewest()
{ {
loadRecords(); loadRecords();
return list.getNewest(); return super.getNewest();
} }
@Override @Override
public void remove(@NonNull Entry entry) public void remove(@NonNull Entry entry)
{ {
loadRecords(); loadRecords();
list.remove(entry); super.remove(entry);
check(habit.getId()); check(habit.getId());
repository.execSQL( repository.execSQL(
"delete from repetitions where habit = ? and timestamp = ?", "delete from repetitions where habit = ? and timestamp = ?",
habit.getId(), entry.getTimestamp().getUnixTime()); habit.getId(), entry.getTimestamp().getUnixTime());
} }
public void removeAll() public void removeAll()
{ {
loadRecords(); loadRecords();
list.removeAll(); super.removeAll();
check(habit.getId()); check(habit.getId());
repository.execSQL("delete from repetitions where habit = ?", repository.execSQL("delete from repetitions where habit = ?",
habit.getId()); habit.getId());
} }
@Override @Override
public long getTotalCount() public long getTotalCount()
{ {
loadRecords(); loadRecords();
return list.getTotalCount(); return super.getTotalCount();
}
public void reload()
{
loaded = false;
} }
@Contract("null -> fail") @Contract("null -> fail")

Loading…
Cancel
Save