mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
CheckmarkList: Implement getThisIntervalValue
This commit is contained in:
@@ -222,6 +222,35 @@ public abstract class CheckmarkList
|
||||
else return UNCHECKED;
|
||||
}
|
||||
|
||||
public synchronized int getThisWeekValue(int firstWeekday)
|
||||
{
|
||||
return getThisIntervalValue(DateUtils.TruncateField.WEEK_NUMBER, firstWeekday);
|
||||
}
|
||||
|
||||
public synchronized int getThisMonthValue()
|
||||
{
|
||||
return getThisIntervalValue(DateUtils.TruncateField.MONTH, Calendar.SATURDAY);
|
||||
}
|
||||
|
||||
|
||||
public synchronized int getThisQuarterValue()
|
||||
{
|
||||
return getThisIntervalValue(DateUtils.TruncateField.QUARTER, Calendar.SATURDAY);
|
||||
}
|
||||
|
||||
|
||||
public synchronized int getThisYearValue()
|
||||
{
|
||||
return getThisIntervalValue(DateUtils.TruncateField.YEAR, Calendar.SATURDAY);
|
||||
}
|
||||
|
||||
private int getThisIntervalValue(DateUtils.TruncateField truncateField, int firstWeekday)
|
||||
{
|
||||
List<Checkmark> groups = habit.getCheckmarks().groupBy(truncateField, firstWeekday, 1);
|
||||
if (groups.isEmpty()) return 0;
|
||||
return groups.get(0).getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the values of the checkmarks that fall inside a certain interval
|
||||
* of time.
|
||||
@@ -328,7 +357,7 @@ public abstract class CheckmarkList
|
||||
|
||||
private void computeNumerical(Repetition[] reps)
|
||||
{
|
||||
if (reps.length == 0) throw new IllegalArgumentException();
|
||||
if (reps.length == 0) return;
|
||||
|
||||
Timestamp today = DateUtils.getToday();
|
||||
Timestamp begin = reps[0].getTimestamp();
|
||||
@@ -419,23 +448,36 @@ public abstract class CheckmarkList
|
||||
|
||||
@NonNull
|
||||
public List<Checkmark> groupBy(DateUtils.TruncateField field, int firstWeekday)
|
||||
{
|
||||
return groupBy(field, firstWeekday, 0);
|
||||
}
|
||||
|
||||
|
||||
@NonNull
|
||||
public List<Checkmark> groupBy(DateUtils.TruncateField field,
|
||||
int firstWeekday,
|
||||
int maxGroups)
|
||||
{
|
||||
List<Checkmark> checks = getAll();
|
||||
|
||||
int count = 0;
|
||||
Timestamp truncatedTimestamps[] = new Timestamp[checks.size()];
|
||||
int values[] = new int[checks.size()];
|
||||
Timestamp[] truncatedTimestamps = new Timestamp[checks.size()];
|
||||
int[] values = new int[checks.size()];
|
||||
|
||||
for (Checkmark rep : checks)
|
||||
{
|
||||
Timestamp tt = rep.getTimestamp().truncate(field, firstWeekday);
|
||||
if (count == 0 || !truncatedTimestamps[count - 1].equals(tt))
|
||||
{
|
||||
if (maxGroups > 0 && count >= maxGroups) break;
|
||||
truncatedTimestamps[count++] = tt;
|
||||
}
|
||||
|
||||
if(habit.isNumerical())
|
||||
values[count - 1] += rep.getValue();
|
||||
else if(rep.getValue() == Checkmark.CHECKED_EXPLICITLY)
|
||||
values[count - 1] += 1000;
|
||||
|
||||
}
|
||||
|
||||
ArrayList<Checkmark> groupedCheckmarks = new ArrayList<>();
|
||||
|
||||
@@ -28,9 +28,8 @@ import java.util.*;
|
||||
|
||||
import nl.jqno.equalsverifier.*;
|
||||
|
||||
import static java.util.Calendar.JANUARY;
|
||||
import static java.util.Calendar.JULY;
|
||||
import static java.util.Calendar.JUNE;
|
||||
import static java.util.Calendar.*;
|
||||
import static junit.framework.TestCase.*;
|
||||
import static org.hamcrest.MatcherAssert.*;
|
||||
import static org.hamcrest.core.IsEqual.*;
|
||||
import static org.isoron.uhabits.core.models.Checkmark.*;
|
||||
@@ -70,11 +69,11 @@ public class CheckmarkListTest extends BaseUnitTest
|
||||
public void test_buildCheckmarksFromIntervals_1() throws Exception
|
||||
{
|
||||
Repetition reps[] = new Repetition[]{
|
||||
new Repetition(day(10), CHECKED_EXPLICITLY),
|
||||
new Repetition(day(5), CHECKED_EXPLICITLY),
|
||||
new Repetition(day(2), CHECKED_EXPLICITLY),
|
||||
new Repetition(day(1), CHECKED_EXPLICITLY),
|
||||
};
|
||||
new Repetition(day(10), CHECKED_EXPLICITLY),
|
||||
new Repetition(day(5), CHECKED_EXPLICITLY),
|
||||
new Repetition(day(2), CHECKED_EXPLICITLY),
|
||||
new Repetition(day(1), CHECKED_EXPLICITLY),
|
||||
};
|
||||
|
||||
ArrayList<CheckmarkList.Interval> intervals = new ArrayList<>();
|
||||
intervals.add(new CheckmarkList.Interval(day(10), day(8), day(8)));
|
||||
@@ -95,7 +94,7 @@ public class CheckmarkListTest extends BaseUnitTest
|
||||
expected.add(new Checkmark(day(10), CHECKED_EXPLICITLY));
|
||||
|
||||
List<Checkmark> actual =
|
||||
CheckmarkList.buildCheckmarksFromIntervals(reps, intervals);
|
||||
CheckmarkList.buildCheckmarksFromIntervals(reps, intervals);
|
||||
assertThat(actual, equalTo(expected));
|
||||
}
|
||||
|
||||
@@ -103,8 +102,8 @@ public class CheckmarkListTest extends BaseUnitTest
|
||||
public void test_buildCheckmarksFromIntervals_2() throws Exception
|
||||
{
|
||||
Repetition reps[] = new Repetition[]{
|
||||
new Repetition(day(0), CHECKED_EXPLICITLY),
|
||||
};
|
||||
new Repetition(day(0), CHECKED_EXPLICITLY),
|
||||
};
|
||||
|
||||
ArrayList<CheckmarkList.Interval> intervals = new ArrayList<>();
|
||||
intervals.add(new CheckmarkList.Interval(day(0), day(0), day(-10)));
|
||||
@@ -113,7 +112,7 @@ public class CheckmarkListTest extends BaseUnitTest
|
||||
expected.add(new Checkmark(day(0), CHECKED_EXPLICITLY));
|
||||
|
||||
List<Checkmark> actual =
|
||||
CheckmarkList.buildCheckmarksFromIntervals(reps, intervals);
|
||||
CheckmarkList.buildCheckmarksFromIntervals(reps, intervals);
|
||||
assertThat(actual, equalTo(expected));
|
||||
}
|
||||
|
||||
@@ -121,10 +120,10 @@ public class CheckmarkListTest extends BaseUnitTest
|
||||
public void test_buildIntervals_1() throws Exception
|
||||
{
|
||||
Repetition reps[] = new Repetition[]{
|
||||
new Repetition(day(23), CHECKED_EXPLICITLY),
|
||||
new Repetition(day(18), CHECKED_EXPLICITLY),
|
||||
new Repetition(day(8), CHECKED_EXPLICITLY),
|
||||
};
|
||||
new Repetition(day(23), CHECKED_EXPLICITLY),
|
||||
new Repetition(day(18), CHECKED_EXPLICITLY),
|
||||
new Repetition(day(8), CHECKED_EXPLICITLY),
|
||||
};
|
||||
|
||||
ArrayList<CheckmarkList.Interval> expected = new ArrayList<>();
|
||||
expected.add(new CheckmarkList.Interval(day(23), day(23), day(17)));
|
||||
@@ -140,10 +139,10 @@ public class CheckmarkListTest extends BaseUnitTest
|
||||
public void test_buildIntervals_2() throws Exception
|
||||
{
|
||||
Repetition reps[] = new Repetition[]{
|
||||
new Repetition(day(23), CHECKED_EXPLICITLY),
|
||||
new Repetition(day(18), CHECKED_EXPLICITLY),
|
||||
new Repetition(day(8), CHECKED_EXPLICITLY),
|
||||
};
|
||||
new Repetition(day(23), CHECKED_EXPLICITLY),
|
||||
new Repetition(day(18), CHECKED_EXPLICITLY),
|
||||
new Repetition(day(8), CHECKED_EXPLICITLY),
|
||||
};
|
||||
|
||||
ArrayList<CheckmarkList.Interval> expected = new ArrayList<>();
|
||||
expected.add(new CheckmarkList.Interval(day(23), day(23), day(23)));
|
||||
@@ -159,12 +158,12 @@ public class CheckmarkListTest extends BaseUnitTest
|
||||
public void test_buildIntervals_3() throws Exception
|
||||
{
|
||||
Repetition reps[] = new Repetition[]{
|
||||
new Repetition(day(23), CHECKED_EXPLICITLY),
|
||||
new Repetition(day(22), CHECKED_EXPLICITLY),
|
||||
new Repetition(day(18), CHECKED_EXPLICITLY),
|
||||
new Repetition(day(15), CHECKED_EXPLICITLY),
|
||||
new Repetition(day(8), CHECKED_EXPLICITLY),
|
||||
};
|
||||
new Repetition(day(23), CHECKED_EXPLICITLY),
|
||||
new Repetition(day(22), CHECKED_EXPLICITLY),
|
||||
new Repetition(day(18), CHECKED_EXPLICITLY),
|
||||
new Repetition(day(15), CHECKED_EXPLICITLY),
|
||||
new Repetition(day(8), CHECKED_EXPLICITLY),
|
||||
};
|
||||
|
||||
ArrayList<CheckmarkList.Interval> expected = new ArrayList<>();
|
||||
expected.add(new CheckmarkList.Interval(day(23), day(22), day(17)));
|
||||
@@ -173,7 +172,7 @@ public class CheckmarkListTest extends BaseUnitTest
|
||||
|
||||
ArrayList<CheckmarkList.Interval> actual;
|
||||
actual =
|
||||
CheckmarkList.buildIntervals(Frequency.TWO_TIMES_PER_WEEK, reps);
|
||||
CheckmarkList.buildIntervals(Frequency.TWO_TIMES_PER_WEEK, reps);
|
||||
assertThat(actual, equalTo(expected));
|
||||
}
|
||||
|
||||
@@ -183,13 +182,13 @@ public class CheckmarkListTest extends BaseUnitTest
|
||||
travelInTime(-3);
|
||||
|
||||
int[] expectedValues = {
|
||||
CHECKED_EXPLICITLY,
|
||||
CHECKED_EXPLICITLY,
|
||||
CHECKED_EXPLICITLY,
|
||||
CHECKED_IMPLICITLY,
|
||||
CHECKED_IMPLICITLY,
|
||||
CHECKED_EXPLICITLY,
|
||||
CHECKED_EXPLICITLY
|
||||
CHECKED_EXPLICITLY,
|
||||
CHECKED_EXPLICITLY,
|
||||
CHECKED_EXPLICITLY,
|
||||
CHECKED_IMPLICITLY,
|
||||
CHECKED_IMPLICITLY,
|
||||
CHECKED_EXPLICITLY,
|
||||
CHECKED_EXPLICITLY
|
||||
};
|
||||
|
||||
int[] actualValues = nonDailyHabit.getCheckmarks().getAllValues();
|
||||
@@ -203,19 +202,19 @@ public class CheckmarkListTest extends BaseUnitTest
|
||||
travelInTime(3);
|
||||
|
||||
int[] expectedValues = {
|
||||
UNCHECKED,
|
||||
UNCHECKED,
|
||||
UNCHECKED,
|
||||
CHECKED_EXPLICITLY,
|
||||
UNCHECKED,
|
||||
CHECKED_IMPLICITLY,
|
||||
CHECKED_EXPLICITLY,
|
||||
CHECKED_EXPLICITLY,
|
||||
CHECKED_EXPLICITLY,
|
||||
CHECKED_IMPLICITLY,
|
||||
CHECKED_IMPLICITLY,
|
||||
CHECKED_EXPLICITLY,
|
||||
CHECKED_EXPLICITLY
|
||||
UNCHECKED,
|
||||
UNCHECKED,
|
||||
UNCHECKED,
|
||||
CHECKED_EXPLICITLY,
|
||||
UNCHECKED,
|
||||
CHECKED_IMPLICITLY,
|
||||
CHECKED_EXPLICITLY,
|
||||
CHECKED_EXPLICITLY,
|
||||
CHECKED_EXPLICITLY,
|
||||
CHECKED_IMPLICITLY,
|
||||
CHECKED_IMPLICITLY,
|
||||
CHECKED_EXPLICITLY,
|
||||
CHECKED_EXPLICITLY
|
||||
};
|
||||
|
||||
int[] actualValues = nonDailyHabit.getCheckmarks().getAllValues();
|
||||
@@ -236,16 +235,16 @@ public class CheckmarkListTest extends BaseUnitTest
|
||||
public void test_getAllValues_withNonDailyHabit()
|
||||
{
|
||||
int[] expectedValues = {
|
||||
CHECKED_EXPLICITLY,
|
||||
UNCHECKED,
|
||||
CHECKED_IMPLICITLY,
|
||||
CHECKED_EXPLICITLY,
|
||||
CHECKED_EXPLICITLY,
|
||||
CHECKED_EXPLICITLY,
|
||||
CHECKED_IMPLICITLY,
|
||||
CHECKED_IMPLICITLY,
|
||||
CHECKED_EXPLICITLY,
|
||||
CHECKED_EXPLICITLY
|
||||
CHECKED_EXPLICITLY,
|
||||
UNCHECKED,
|
||||
CHECKED_IMPLICITLY,
|
||||
CHECKED_EXPLICITLY,
|
||||
CHECKED_EXPLICITLY,
|
||||
CHECKED_EXPLICITLY,
|
||||
CHECKED_IMPLICITLY,
|
||||
CHECKED_IMPLICITLY,
|
||||
CHECKED_EXPLICITLY,
|
||||
CHECKED_EXPLICITLY
|
||||
};
|
||||
|
||||
int[] actualValues = nonDailyHabit.getCheckmarks().getAllValues();
|
||||
@@ -259,9 +258,9 @@ public class CheckmarkListTest extends BaseUnitTest
|
||||
CheckmarkList checkmarks = numericalHabit.getCheckmarks();
|
||||
|
||||
List<Checkmark> expected =
|
||||
Arrays.asList(new Checkmark(day(1), 200), new Checkmark(day(2), 0),
|
||||
new Checkmark(day(3), 300), new Checkmark(day(4), 0),
|
||||
new Checkmark(day(5), 400));
|
||||
Arrays.asList(new Checkmark(day(1), 200), new Checkmark(day(2), 0),
|
||||
new Checkmark(day(3), 300), new Checkmark(day(4), 0),
|
||||
new Checkmark(day(5), 400));
|
||||
|
||||
List<Checkmark> actual = checkmarks.getByInterval(day(5), day(1));
|
||||
assertThat(actual, equalTo(expected));
|
||||
@@ -286,8 +285,8 @@ public class CheckmarkListTest extends BaseUnitTest
|
||||
public void test_getValues_withInvalidInterval()
|
||||
{
|
||||
int values[] = nonDailyHabit
|
||||
.getCheckmarks()
|
||||
.getValues(new Timestamp(0L).plus(100), new Timestamp(0L));
|
||||
.getCheckmarks()
|
||||
.getValues(new Timestamp(0L).plus(100), new Timestamp(0L));
|
||||
assertThat(values, equalTo(new int[0]));
|
||||
}
|
||||
|
||||
@@ -298,17 +297,17 @@ public class CheckmarkListTest extends BaseUnitTest
|
||||
Timestamp to = today.minus(5);
|
||||
|
||||
int[] expectedValues = {
|
||||
CHECKED_EXPLICITLY,
|
||||
CHECKED_IMPLICITLY,
|
||||
CHECKED_IMPLICITLY,
|
||||
CHECKED_EXPLICITLY,
|
||||
CHECKED_EXPLICITLY,
|
||||
UNCHECKED,
|
||||
UNCHECKED,
|
||||
UNCHECKED,
|
||||
UNCHECKED,
|
||||
UNCHECKED,
|
||||
UNCHECKED
|
||||
CHECKED_EXPLICITLY,
|
||||
CHECKED_IMPLICITLY,
|
||||
CHECKED_IMPLICITLY,
|
||||
CHECKED_EXPLICITLY,
|
||||
CHECKED_EXPLICITLY,
|
||||
UNCHECKED,
|
||||
UNCHECKED,
|
||||
UNCHECKED,
|
||||
UNCHECKED,
|
||||
UNCHECKED,
|
||||
UNCHECKED
|
||||
};
|
||||
|
||||
int[] actualValues = nonDailyHabit.getCheckmarks().getValues(from, to);
|
||||
@@ -353,9 +352,9 @@ public class CheckmarkListTest extends BaseUnitTest
|
||||
public void test_writeCSV() throws IOException
|
||||
{
|
||||
String expectedCSV = "2015-01-25,2\n2015-01-24,0\n2015-01-23,1\n" +
|
||||
"2015-01-22,2\n2015-01-21,2\n2015-01-20,2\n" +
|
||||
"2015-01-19,1\n2015-01-18,1\n2015-01-17,2\n" +
|
||||
"2015-01-16,2\n";
|
||||
"2015-01-22,2\n2015-01-21,2\n2015-01-20,2\n" +
|
||||
"2015-01-19,1\n2015-01-18,1\n2015-01-17,2\n" +
|
||||
"2015-01-16,2\n";
|
||||
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
@@ -372,7 +371,7 @@ public class CheckmarkListTest extends BaseUnitTest
|
||||
private void travelInTime(int days)
|
||||
{
|
||||
DateUtils.setFixedLocalTime(
|
||||
FIXED_LOCAL_TIME + days * Timestamp.DAY_LENGTH);
|
||||
FIXED_LOCAL_TIME + days * Timestamp.DAY_LENGTH);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -381,12 +380,12 @@ public class CheckmarkListTest extends BaseUnitTest
|
||||
Timestamp t = Timestamp.ZERO.plus(100);
|
||||
Checkmark checkmark = new Checkmark(t, 2);
|
||||
assertThat(checkmark.toString(),
|
||||
equalTo("{timestamp: 1970-04-11, value: 2}"));
|
||||
equalTo("{timestamp: 1970-04-11, value: 2}"));
|
||||
|
||||
CheckmarkList.Interval interval =
|
||||
new CheckmarkList.Interval(t, t.plus(1), t.plus(2));
|
||||
new CheckmarkList.Interval(t, t.plus(1), t.plus(2));
|
||||
assertThat(interval.toString(), equalTo(
|
||||
"{begin: 1970-04-11, center: 1970-04-12, end: 1970-04-13}"));
|
||||
"{begin: 1970-04-11, center: 1970-04-12, end: 1970-04-13}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -422,4 +421,44 @@ public class CheckmarkListTest extends BaseUnitTest
|
||||
assertThat(byYear.get(1), equalTo(new Checkmark(timestamp(2014, JANUARY, 1), 8227)));
|
||||
assertThat(byYear.get(2), equalTo(new Checkmark(timestamp(2013, JANUARY, 1), 16172)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetTodayValue() throws Exception
|
||||
{
|
||||
Habit habit = fixtures.createLongNumericalHabit(timestamp(2014, JUNE, 1));
|
||||
CheckmarkList checkmarks = habit.getCheckmarks();
|
||||
|
||||
DateUtils.setFixedLocalTime(unixTime(2050, MAY, 1));
|
||||
assertThat(checkmarks.getTodayValue(), equalTo(0));
|
||||
assertThat(checkmarks.getThisWeekValue(SATURDAY), equalTo(0));
|
||||
assertThat(checkmarks.getThisMonthValue(), equalTo(0));
|
||||
assertThat(checkmarks.getThisQuarterValue(), equalTo(0));
|
||||
assertThat(checkmarks.getThisYearValue(), equalTo(0));
|
||||
|
||||
DateUtils.setFixedLocalTime(unixTime(2014, JUNE, 6));
|
||||
assertThat(checkmarks.getTodayValue(), equalTo(0));
|
||||
assertThat(checkmarks.getThisWeekValue(SATURDAY), equalTo(230));
|
||||
assertThat(checkmarks.getThisWeekValue(SUNDAY), equalTo(230));
|
||||
assertThat(checkmarks.getThisWeekValue(MONDAY), equalTo(0));
|
||||
assertThat(checkmarks.getThisMonthValue(), equalTo(230));
|
||||
assertThat(checkmarks.getThisQuarterValue(), equalTo(3263));
|
||||
assertThat(checkmarks.getThisYearValue(), equalTo(8227));
|
||||
|
||||
DateUtils.setFixedLocalTime(unixTime(2014, JUNE, 1));
|
||||
assertThat(checkmarks.getTodayValue(), equalTo(230));
|
||||
assertThat(checkmarks.getThisWeekValue(SATURDAY), equalTo(230));
|
||||
assertThat(checkmarks.getThisWeekValue(SUNDAY), equalTo(230));
|
||||
assertThat(checkmarks.getThisMonthValue(), equalTo(230));
|
||||
|
||||
DateUtils.setFixedLocalTime(unixTime(2014, MAY, 16));
|
||||
assertThat(checkmarks.getTodayValue(), equalTo(0));
|
||||
assertThat(checkmarks.getThisWeekValue(SATURDAY), equalTo(419));
|
||||
assertThat(checkmarks.getThisWeekValue(THURSDAY), equalTo(134));
|
||||
assertThat(checkmarks.getThisMonthValue(), equalTo(1006));
|
||||
|
||||
DateUtils.setFixedLocalTime(unixTime(2000, MAY, 1));
|
||||
assertThat(checkmarks.getTodayValue(), equalTo(0));
|
||||
assertThat(checkmarks.getThisWeekValue(SATURDAY), equalTo(0));
|
||||
assertThat(checkmarks.getThisMonthValue(), equalTo(0));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user