From 1c696e25611bb2f03f98591a6c1b93709bf88a5e Mon Sep 17 00:00:00 2001 From: "Alinson S. Xavier" Date: Tue, 29 Dec 2020 09:08:08 -0600 Subject: [PATCH] Remove MemoryScoreList --- .../isoron/uhabits/core/models/ScoreList.java | 62 ++++++++++-- .../core/models/memory/MemoryModelFactory.kt | 2 +- .../core/models/memory/MemoryScoreList.java | 98 ------------------- .../core/models/sqlite/SQLModelFactory.kt | 2 +- 4 files changed, 55 insertions(+), 109 deletions(-) delete mode 100644 android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/memory/MemoryScoreList.java diff --git a/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/ScoreList.java b/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/ScoreList.java index 1a1949207..a9ea7caf5 100644 --- a/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/ScoreList.java +++ b/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/ScoreList.java @@ -29,8 +29,10 @@ import java.util.*; import static org.isoron.uhabits.core.models.Entry.*; -public abstract class ScoreList implements Iterable +public class ScoreList implements Iterable { + ArrayList list = new ArrayList<>(); + protected Habit habit; public void setHabit(Habit habit) @@ -46,7 +48,12 @@ public abstract class ScoreList implements Iterable * * @param scores the scores to add. */ - public abstract void add(List scores); + public void add(List scores) + { + list.addAll(scores); + Collections.sort(list, + (s1, s2) -> s2.getTimestamp().compareTo(s1.getTimestamp())); + } /** * Returns the value of the score for today. @@ -88,8 +95,23 @@ public abstract class ScoreList implements Iterable * @return the list of scores within the interval. */ @NonNull - public abstract List getByInterval(@NonNull Timestamp fromTimestamp, - @NonNull Timestamp toTimestamp); + public List getByInterval(@NonNull Timestamp fromTimestamp, + @NonNull Timestamp toTimestamp) + { + compute(fromTimestamp, toTimestamp); + + List filtered = new LinkedList<>(); + + for (Score s : list) + { + if (s.getTimestamp().isNewerThan(toTimestamp) || + s.getTimestamp().isOlderThan(fromTimestamp)) continue; + filtered.add(s); + } + + return filtered; + + } /** * Returns the values of the scores that fall inside a certain interval @@ -124,7 +146,10 @@ public abstract class ScoreList implements Iterable return scores; } - public abstract void recompute(); + public void recompute() + { + list.clear(); + } @Override public Iterator iterator() @@ -141,7 +166,11 @@ public abstract class ScoreList implements Iterable * * @return list of scores */ - public abstract List toList(); + public List toList() + { + computeAll(); + return new LinkedList<>(list); + } public void writeCSV(Writer out) throws IOException { @@ -216,21 +245,36 @@ public abstract class ScoreList implements Iterable * @return the score with given timestamp, or null not yet computed. */ @Nullable - protected abstract Score getComputedByTimestamp(Timestamp timestamp); + protected Score getComputedByTimestamp(Timestamp timestamp) + { + for (Score s : list) + if (s.getTimestamp().equals(timestamp)) return s; + + return null; + } /** * Returns the most recent score that has already been computed. If no score * has been computed yet, returns null. */ @Nullable - protected abstract Score getNewestComputed(); + protected Score getNewestComputed() + { + if (list.isEmpty()) return null; + return list.get(0); + + } /** * Returns oldest score already computed. If no score has been computed yet, * returns null. */ @Nullable - protected abstract Score getOldestComputed(); + protected Score getOldestComputed() + { + if (list.isEmpty()) return null; + return list.get(list.size() - 1); + } /** * Computes and stores one score for each day inside the given interval. diff --git a/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/memory/MemoryModelFactory.kt b/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/memory/MemoryModelFactory.kt index 30dab0a4e..2bb27c489 100644 --- a/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/memory/MemoryModelFactory.kt +++ b/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/memory/MemoryModelFactory.kt @@ -24,7 +24,7 @@ class MemoryModelFactory : ModelFactory { override fun buildComputedEntries() = EntryList() override fun buildOriginalEntries() = EntryList() override fun buildHabitList() = MemoryHabitList() - override fun buildScoreList() = MemoryScoreList() + override fun buildScoreList() = ScoreList() override fun buildStreakList() = MemoryStreakList() override fun buildHabitListRepository() = throw NotImplementedError() override fun buildRepetitionListRepository() = throw NotImplementedError() diff --git a/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/memory/MemoryScoreList.java b/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/memory/MemoryScoreList.java deleted file mode 100644 index 0f7e5caca..000000000 --- a/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/memory/MemoryScoreList.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright (C) 2016 Álinson Santos Xavier - * - * 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 . - */ - -package org.isoron.uhabits.core.models.memory; - -import androidx.annotation.*; - -import org.isoron.uhabits.core.models.*; - -import java.util.*; - -public class MemoryScoreList extends ScoreList -{ - ArrayList list = new ArrayList<>(); - - @Override - public void add(List scores) - { - list.addAll(scores); - Collections.sort(list, - (s1, s2) -> s2.getTimestamp().compareTo(s1.getTimestamp())); - } - - @NonNull - @Override - public List getByInterval(@NonNull Timestamp fromTimestamp, - @NonNull Timestamp toTimestamp) - { - compute(fromTimestamp, toTimestamp); - - List filtered = new LinkedList<>(); - - for (Score s : list) - { - if (s.getTimestamp().isNewerThan(toTimestamp) || - s.getTimestamp().isOlderThan(fromTimestamp)) continue; - filtered.add(s); - } - - return filtered; - } - - @Nullable - @Override - public Score getComputedByTimestamp(Timestamp timestamp) - { - for (Score s : list) - if (s.getTimestamp().equals(timestamp)) return s; - - return null; - } - - @Override - public void recompute() - { - list.clear(); - } - - @Override - @NonNull - public List toList() - { - computeAll(); - return new LinkedList<>(list); - } - - @Nullable - @Override - protected Score getNewestComputed() - { - if (list.isEmpty()) return null; - return list.get(0); - } - - @Nullable - @Override - protected Score getOldestComputed() - { - if (list.isEmpty()) return null; - return list.get(list.size() - 1); - } -} diff --git a/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/sqlite/SQLModelFactory.kt b/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/sqlite/SQLModelFactory.kt index 7a45d0268..537b6938a 100644 --- a/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/sqlite/SQLModelFactory.kt +++ b/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/sqlite/SQLModelFactory.kt @@ -36,7 +36,7 @@ class SQLModelFactory override fun buildOriginalEntries() = SQLiteEntryList(database) override fun buildComputedEntries() = EntryList() override fun buildHabitList() = SQLiteHabitList(this) - override fun buildScoreList() = MemoryScoreList() + override fun buildScoreList() = ScoreList() override fun buildStreakList() = MemoryStreakList() override fun buildHabitListRepository() =