Repetition: Replace toggle by setValue

pull/699/head
Alinson S. Xavier 5 years ago
parent d45281d137
commit f97fed3b9b

@ -22,6 +22,8 @@ package org.isoron.uhabits;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.utils.DateUtils;
import static org.isoron.uhabits.core.models.Checkmark.*;
public class HabitFixtures
{
public boolean LONG_HABIT_CHECKS[] = {
@ -73,7 +75,7 @@ public class HabitFixtures
81, 83, 89, 90, 91, 95, 102, 103, 108, 109, 120};
for (int mark : marks)
habit.getRepetitions().toggle(today.minus(mark));
habit.getRepetitions().setValue(today.minus(mark), YES_MANUAL);
return habit;
}
@ -107,7 +109,7 @@ public class HabitFixtures
582, 583, 584, 586, 589};
for (int mark : marks)
habit.getRepetitions().toggle(today.minus(mark));
habit.getRepetitions().setValue(today.minus(mark), YES_MANUAL);
return habit;
}
@ -145,7 +147,7 @@ public class HabitFixtures
Timestamp timestamp = DateUtils.getToday();
for (boolean c : LONG_HABIT_CHECKS)
{
if (c) habit.getRepetitions().toggle(timestamp);
if (c) habit.getRepetitions().setValue(timestamp, YES_MANUAL);
timestamp = timestamp.minus(1);
}

@ -64,7 +64,7 @@ public class WidgetControllerTest extends BaseAndroidJVMTest
@Test
public void testOnAddRepetition_whenChecked() throws Exception
{
habit.getRepetitions().toggle(today);
habit.getRepetitions().setValue(today, YES_MANUAL);
int todayValue = habit.getCheckmarks().getTodayValue();
assertThat(todayValue, equalTo(YES_MANUAL));
controller.onAddRepetition(habit, today);
@ -84,7 +84,7 @@ public class WidgetControllerTest extends BaseAndroidJVMTest
@Test
public void testOnRemoveRepetition_whenChecked() throws Exception
{
habit.getRepetitions().toggle(today);
habit.getRepetitions().setValue(today, YES_MANUAL);
int todayValue = habit.getCheckmarks().getTodayValue();
assertThat(todayValue, equalTo(YES_MANUAL));
controller.onRemoveRepetition(habit, today);

@ -31,6 +31,8 @@ import java.util.*;
import javax.inject.*;
import static org.isoron.uhabits.core.models.Checkmark.*;
/**
* Class that imports data from HabitBull CSV files.
@ -93,8 +95,7 @@ public class HabitBullCSVImporter extends AbstractImporter
map.put(name, h);
}
if (!h.getRepetitions().containsTimestamp(timestamp))
h.getRepetitions().toggle(timestamp);
h.getRepetitions().setValue(timestamp, YES_MANUAL);
}
}
}

@ -109,7 +109,7 @@ public class LoopDBImporter extends AbstractImporter
habitRecord.id.toString());
for (RepetitionRecord r : reps)
h.getRepetitions().toggle(new Timestamp(r.timestamp), r.value);
h.getRepetitions().setValue(new Timestamp(r.timestamp), r.value);
}
}
}

@ -30,6 +30,8 @@ import java.util.*;
import javax.inject.*;
import static org.isoron.uhabits.core.models.Checkmark.*;
/**
* Class that imports database files exported by Rewire.
*/
@ -165,7 +167,7 @@ public class RewireDBImporter extends AbstractImporter
GregorianCalendar cal = DateUtils.getStartOfTodayCalendar();
cal.set(year, month - 1, day);
habit.getRepetitions().toggle(new Timestamp(cal));
habit.getRepetitions().setValue(new Timestamp(cal), YES_MANUAL);
} while (c.moveToNext());
}
finally

@ -30,6 +30,8 @@ import java.util.*;
import javax.inject.*;
import static org.isoron.uhabits.core.models.Checkmark.*;
/**
* Class that imports data from database files exported by Tickmate.
*/
@ -100,7 +102,7 @@ public class TickmateDBImporter extends AbstractImporter
GregorianCalendar cal = DateUtils.getStartOfTodayCalendar();
cal.set(year, month, day);
habit.getRepetitions().toggle(new Timestamp(cal));
habit.getRepetitions().setValue(new Timestamp(cal), YES_MANUAL);
} while (c.moveToNext());
}
finally

@ -52,19 +52,6 @@ public abstract class RepetitionList
*/
public abstract void add(Repetition repetition);
/**
* Returns true if the list contains a repetition that has the given
* timestamp.
*
* @param timestamp the timestamp to find.
* @return true if list contains repetition with given timestamp, false
* otherwise.
*/
public boolean containsTimestamp(Timestamp timestamp)
{
return (getByTimestamp(timestamp) != null);
}
/**
* Returns the list of repetitions that happened within the given time
* interval.
@ -90,6 +77,18 @@ public abstract class RepetitionList
@Nullable
public abstract Repetition getByTimestamp(Timestamp timestamp);
/**
* If a repetition with the given timestamp exists, return its value. Otherwise, returns
* Checkmark.NO for boolean habits and zero for numerical habits.
*/
@NonNull
public int getValue(Timestamp timestamp)
{
Repetition rep = getByTimestamp(timestamp);
if (rep == null) return Checkmark.NO;
return rep.getValue();
}
@NonNull
public ModelObservable getObservable()
{
@ -175,39 +174,9 @@ public abstract class RepetitionList
*/
public abstract void remove(@NonNull Repetition repetition);
/**
* Adds or remove a repetition at a certain timestamp.
* <p>
* If there exists a repetition on the list with the given timestamp, the
* method removes this repetition from the list and returns it. If there are
* no repetitions with the given timestamp, creates and adds one to the
* list, then returns it.
*
* @param timestamp the timestamp for the timestamp that should be added or
* removed.
* @return the repetition that has been added or removed.
*/
@NonNull
public synchronized Repetition toggle(Timestamp timestamp)
{
if (habit.isNumerical())
throw new IllegalStateException("habit must NOT be numerical");
Repetition rep = getByTimestamp(timestamp);
if (rep != null) remove(rep);
else
{
rep = new Repetition(timestamp, Checkmark.YES_MANUAL);
add(rep);
}
habit.invalidateNewerThan(timestamp);
return rep;
}
public abstract long getTotalCount();
public void toggle(Timestamp timestamp, int value)
public void setValue(Timestamp timestamp, int value)
{
Repetition rep = getByTimestamp(timestamp);
if (rep != null) remove(rep);

@ -23,6 +23,8 @@ import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.models.sqlite.*;
import org.isoron.uhabits.core.utils.*;
import static org.isoron.uhabits.core.models.Checkmark.*;
public class HabitFixtures
{
public boolean NON_DAILY_HABIT_CHECKS[] = {
@ -63,7 +65,7 @@ public class HabitFixtures
81, 83, 89, 90, 91, 95, 102, 103, 108, 109, 120};
for (int mark : marks)
habit.getRepetitions().toggle(today.minus(mark));
habit.getRepetitions().setValue(today.minus(mark), YES_MANUAL);
return habit;
}
@ -140,7 +142,7 @@ public class HabitFixtures
Timestamp timestamp = DateUtils.getToday();
for (boolean c : NON_DAILY_HABIT_CHECKS)
{
if (c) habit.getRepetitions().toggle(timestamp);
if (c) habit.getRepetitions().setValue(timestamp, YES_MANUAL);
timestamp = timestamp.minus(1);
}

@ -30,6 +30,7 @@ import java.util.*;
import static junit.framework.TestCase.assertFalse;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.*;
import static org.isoron.uhabits.core.models.Checkmark.*;
import static org.isoron.uhabits.core.models.Frequency.*;
import static org.junit.Assert.assertTrue;
@ -54,9 +55,9 @@ public class ImportTest extends BaseUnitTest
assertThat(habit.getName(), equalTo("Breed dragons"));
assertThat(habit.getDescription(), equalTo("with love and fire"));
assertThat(habit.getFrequency(), equalTo(Frequency.DAILY));
assertTrue(containsRepetition(habit, 2016, 3, 18));
assertTrue(containsRepetition(habit, 2016, 3, 19));
assertFalse(containsRepetition(habit, 2016, 3, 20));
assertTrue(isChecked(habit, 2016, 3, 18));
assertTrue(isChecked(habit, 2016, 3, 19));
assertFalse(isChecked(habit, 2016, 3, 20));
}
@Test
@ -69,9 +70,9 @@ public class ImportTest extends BaseUnitTest
Habit habit = habitList.getByPosition(0);
assertThat(habit.getName(), equalTo("Wake up early"));
assertThat(habit.getFrequency(), equalTo(THREE_TIMES_PER_WEEK));
assertTrue(containsRepetition(habit, 2016, 3, 14));
assertTrue(containsRepetition(habit, 2016, 3, 16));
assertFalse(containsRepetition(habit, 2016, 3, 17));
assertTrue(isChecked(habit, 2016, 3, 14));
assertTrue(isChecked(habit, 2016, 3, 16));
assertFalse(isChecked(habit, 2016, 3, 17));
}
@Test
@ -85,10 +86,10 @@ public class ImportTest extends BaseUnitTest
assertThat(habit.getName(), equalTo("Wake up early"));
assertThat(habit.getFrequency(), equalTo(THREE_TIMES_PER_WEEK));
assertFalse(habit.hasReminder());
assertFalse(containsRepetition(habit, 2015, 12, 31));
assertTrue(containsRepetition(habit, 2016, 1, 18));
assertTrue(containsRepetition(habit, 2016, 1, 28));
assertFalse(containsRepetition(habit, 2016, 3, 10));
assertFalse(isChecked(habit, 2015, 12, 31));
assertTrue(isChecked(habit, 2016, 1, 18));
assertTrue(isChecked(habit, 2016, 1, 28));
assertFalse(isChecked(habit, 2016, 3, 10));
habit = habitList.getByPosition(2);
assertThat(habit.getName(), equalTo("brush teeth"));
@ -111,17 +112,18 @@ public class ImportTest extends BaseUnitTest
Habit h = habitList.getByPosition(2);
assertThat(h.getName(), equalTo("Vegan"));
assertTrue(containsRepetition(h, 2016, 1, 24));
assertTrue(containsRepetition(h, 2016, 2, 5));
assertTrue(containsRepetition(h, 2016, 3, 18));
assertFalse(containsRepetition(h, 2016, 3, 14));
assertTrue(isChecked(h, 2016, 1, 24));
assertTrue(isChecked(h, 2016, 2, 5));
assertTrue(isChecked(h, 2016, 3, 18));
assertFalse(isChecked(h, 2016, 3, 14));
}
private boolean containsRepetition(Habit h, int year, int month, int day)
private boolean isChecked(Habit h, int year, int month, int day)
{
GregorianCalendar date = DateUtils.getStartOfTodayCalendar();
date.set(year, month - 1, day);
return h.getRepetitions().containsTimestamp(new Timestamp(date));
Timestamp timestamp = new Timestamp(date);
return h.getRepetitions().getValue(timestamp) == YES_MANUAL;
}
private void importFromFile(String assetFilename) throws IOException

@ -89,7 +89,7 @@ public class HabitTest extends BaseUnitTest
{
Habit h = modelFactory.buildHabit();
assertFalse(h.isCompletedToday());
h.getRepetitions().toggle(getToday());
h.getRepetitions().setValue(getToday(), Checkmark.YES_MANUAL);
assertTrue(h.isCompletedToday());
}
@ -102,19 +102,19 @@ public class HabitTest extends BaseUnitTest
h.setTargetValue(100.0);
assertFalse(h.isCompletedToday());
h.getRepetitions().toggle(getToday(), 200_000);
h.getRepetitions().setValue(getToday(), 200_000);
assertTrue(h.isCompletedToday());
h.getRepetitions().toggle(getToday(), 100_000);
h.getRepetitions().setValue(getToday(), 100_000);
assertTrue(h.isCompletedToday());
h.getRepetitions().toggle(getToday(), 50_000);
h.getRepetitions().setValue(getToday(), 50_000);
assertFalse(h.isCompletedToday());
h.setTargetType(Habit.AT_MOST);
h.getRepetitions().toggle(getToday(), 200_000);
h.getRepetitions().setValue(getToday(), 200_000);
assertFalse(h.isCompletedToday());
h.getRepetitions().toggle(getToday(), 100_000);
h.getRepetitions().setValue(getToday(), 100_000);
assertTrue(h.isCompletedToday());
h.getRepetitions().toggle(getToday(), 50_000);
h.getRepetitions().setValue(getToday(), 50_000);
assertTrue(h.isCompletedToday());
}

@ -28,11 +28,9 @@ import org.junit.*;
import java.util.*;
import static java.util.Calendar.*;
import static junit.framework.TestCase.assertFalse;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.core.IsEqual.*;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.isoron.uhabits.core.models.Checkmark.*;
import static org.mockito.Mockito.*;
public class RepetitionListTest extends BaseUnitTest
@ -60,11 +58,11 @@ public class RepetitionListTest extends BaseUnitTest
today = DateUtils.getToday();
reps.toggle(today.minus(3));
reps.toggle(today.minus(2));
reps.toggle(today);
reps.toggle(today.minus(7));
reps.toggle(today.minus(5));
reps.setValue(today.minus(3), YES_MANUAL);
reps.setValue(today.minus(2), YES_MANUAL);
reps.setValue(today, YES_MANUAL);
reps.setValue(today.minus(7), YES_MANUAL);
reps.setValue(today.minus(5), YES_MANUAL);
listener = mock(ModelObservable.Listener.class);
reps.getObservable().addListener(listener);
@ -78,17 +76,6 @@ public class RepetitionListTest extends BaseUnitTest
super.tearDown();
}
@Test
public void test_contains()
{
assertTrue(reps.containsTimestamp(today));
assertTrue(reps.containsTimestamp(today.minus(2)));
assertTrue(reps.containsTimestamp(today.minus(3)));
assertFalse(reps.containsTimestamp(today.minus(1)));
assertFalse(reps.containsTimestamp(today.minus(4)));
}
@Test
public void test_getOldest()
{
@ -126,7 +113,7 @@ public class RepetitionListTest extends BaseUnitTest
// Leave the month of March empty, to check that it returns null
if (month == MARCH) continue;
reps.toggle(new Timestamp(day));
reps.setValue(new Timestamp(day), YES_MANUAL);
// Repetitions in December should not be counted
if (month == DECEMBER) continue;
@ -155,32 +142,22 @@ public class RepetitionListTest extends BaseUnitTest
}
@Test
public void test_toggle()
public void test_setValue()
{
assertTrue(reps.containsTimestamp(today));
reps.toggle(today);
assertFalse(reps.containsTimestamp(today));
verify(listener).onModelChange();
reset(listener);
assertFalse(reps.containsTimestamp(today.minus(1)));
reps.toggle(today.minus(1));
assertTrue(reps.containsTimestamp(today.minus(1)));
verify(listener).onModelChange();
assertThat(reps.getValue(today), equalTo(YES_MANUAL));
reps.setValue(today, NO);
assertThat(reps.getValue(today), equalTo(NO));
verify(listener, times(2)).onModelChange();
reset(listener);
habit.setType(Habit.NUMBER_HABIT);
reps.toggle(today, 100);
Repetition check = reps.getByTimestamp(today);
assertNotNull(check);
assertThat(check.getValue(), equalTo(100));
verify(listener).onModelChange();
reps.setValue(today, 100);
assertThat(reps.getValue(today), equalTo(100));
verify(listener, times(2)).onModelChange();
reset(listener);
reps.toggle(today, 500);
check = reps.getByTimestamp(today);
assertNotNull(check);
assertThat(check.getValue(), equalTo(500));
reps.setValue(today, 500);
assertThat(reps.getValue(today), equalTo(500));
verify(listener, times(2)).onModelChange();
reset(listener);
}

@ -49,7 +49,7 @@ public class ScoreListTest extends BaseUnitTest
@Test
public void test_getAll()
{
toggle(0, 20);
check(0, 20);
double expectedValues[] = {
0.655747,
@ -82,7 +82,7 @@ public class ScoreListTest extends BaseUnitTest
@Test
public void test_getTodayValue()
{
toggle(0, 20);
check(0, 20);
double actual = habit.getScores().getTodayValue();
assertThat(actual, closeTo(0.655747, E));
}
@ -90,7 +90,7 @@ public class ScoreListTest extends BaseUnitTest
@Test
public void test_getValue()
{
toggle(0, 20);
check(0, 20);
double expectedValues[] = {
0.655747,
@ -124,7 +124,7 @@ public class ScoreListTest extends BaseUnitTest
@Test
public void test_getValueWithSkip()
{
toggle(0, 20);
check(0, 20);
addSkip(5);
addSkip(10);
addSkip(11);
@ -161,7 +161,7 @@ public class ScoreListTest extends BaseUnitTest
@Test
public void test_getValueWithSkip2()
{
toggle(5);
check(5);
addSkip(4);
double[] expectedValues = {
@ -180,7 +180,7 @@ public class ScoreListTest extends BaseUnitTest
@Test
public void test_getValues()
{
toggle(0, 20);
check(0, 20);
Timestamp today = DateUtils.getToday();
Timestamp from = today.minus(4);
@ -214,7 +214,7 @@ public class ScoreListTest extends BaseUnitTest
values.add(NO);
values.add(NO);
}
toggle(values);
check(values);
assertThat(habit.getScores().getTodayValue(), closeTo(2/3.0, E));
// Missing 2 repetitions out of 4 per week, the score should converge to 50%
@ -249,7 +249,7 @@ public class ScoreListTest extends BaseUnitTest
values.add(NO);
values.add(YES_MANUAL);
}
toggle(values);
check(values);
assertThat(habit.getScores().getTodayValue(), closeTo(1.0, 1e-3));
}
@ -259,18 +259,18 @@ public class ScoreListTest extends BaseUnitTest
// Daily habits should achieve at least 99% in 3 months
habit = fixtures.createEmptyHabit();
habit.setFrequency(Frequency.DAILY);
for (int i = 0; i < 90; i++) toggle(i);
for (int i = 0; i < 90; i++) check(i);
assertThat(habit.getScores().getTodayValue(), greaterThan(0.99));
// Weekly habits should achieve at least 99% in 9 months
habit = fixtures.createEmptyHabit();
habit.setFrequency(Frequency.WEEKLY);
for (int i = 0; i < 39; i++) toggle(7 * i);
for (int i = 0; i < 39; i++) check(7 * i);
assertThat(habit.getScores().getTodayValue(), greaterThan(0.99));
// Monthly habits should achieve at least 99% in 18 months
habit.setFrequency(new Frequency(1, 30));
for (int i = 0; i < 18; i++) toggle(30 * i);
for (int i = 0; i < 18; i++) check(30 * i);
assertThat(habit.getScores().getTodayValue(), greaterThan(0.99));
}
@ -292,7 +292,7 @@ public class ScoreListTest extends BaseUnitTest
{
assertThat(habit.getScores().getTodayValue(), closeTo(0.0, E));
toggle(0, 2);
check(0, 2);
assertThat(habit.getScores().getTodayValue(), closeTo(0.101149, E));
habit.setFrequency(new Frequency(1, 2));
@ -324,36 +324,36 @@ public class ScoreListTest extends BaseUnitTest
assertThat(writer.toString(), equalTo(expectedCSV));
}
private void toggle(final int offset)
private void check(final int offset)
{
RepetitionList reps = habit.getRepetitions();
Timestamp today = DateUtils.getToday();
reps.toggle(today.minus(offset));
reps.setValue(today.minus(offset), YES_MANUAL);
}
private void toggle(final int from, final int to)
private void check(final int from, final int to)
{
RepetitionList reps = habit.getRepetitions();
Timestamp today = DateUtils.getToday();
for (int i = from; i < to; i++)
reps.toggle(today.minus(i));
reps.setValue(today.minus(i), YES_MANUAL);
}
private void toggle(ArrayList<Integer> values)
private void check(ArrayList<Integer> values)
{
RepetitionList reps = habit.getRepetitions();
Timestamp today = DateUtils.getToday();
for (int i = 0; i < values.size(); i++)
if (values.get(i) == YES_MANUAL)
reps.toggle(today.minus(i));
reps.setValue(today.minus(i), YES_MANUAL);
}
private void addSkip(final int day)
{
RepetitionList reps = habit.getRepetitions();
Timestamp today = DateUtils.getToday();
reps.toggle(today.minus(day), Checkmark.SKIP);
reps.setValue(today.minus(day), Checkmark.SKIP);
}
private void checkScoreValues(double[] expectedValues)

Loading…
Cancel
Save