diff --git a/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/Score.java b/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/Score.java deleted file mode 100644 index 533fffb0f..000000000 --- a/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/Score.java +++ /dev/null @@ -1,121 +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; - -import org.apache.commons.lang3.builder.*; - -import static java.lang.Math.*; -import static org.isoron.uhabits.core.utils.StringUtils.defaultToStringStyle; - -/** - * Represents how strong a habit is at a certain date. - */ -public final class Score -{ - /** - * Timestamp of the day to which this score applies. Time of day should be - * midnight (UTC). - */ - private final Timestamp timestamp; - - /** - * Value of the score. - */ - private final double value; - - public Score(Timestamp timestamp, double value) - { - this.timestamp = timestamp; - this.value = value; - } - - /** - * Given the frequency of the habit, the previous score, and the value of - * the current checkmark, computes the current score for the habit. - *

- * The frequency of the habit is the number of repetitions divided by the - * length of the interval. For example, a habit that should be repeated 3 - * times in 8 days has frequency 3.0 / 8.0 = 0.375. - * - * @param frequency the frequency of the habit - * @param previousScore the previous score of the habit - * @param checkmarkValue the value of the current checkmark - * @return the current score - */ - public static double compute(double frequency, - double previousScore, - double checkmarkValue) - { - double multiplier = pow(0.5, sqrt(frequency) / 13.0); - - double score = previousScore * multiplier; - score += checkmarkValue * (1 - multiplier); - - return score; - } - - public int compareNewer(Score other) - { - return getTimestamp().compareTo(other.getTimestamp()); - } - - public Timestamp getTimestamp() - { - return timestamp; - } - - public double getValue() - { - return value; - } - - @Override - public String toString() - { - return new ToStringBuilder(this, defaultToStringStyle()) - .append("timestamp", timestamp) - .append("value", value) - .toString(); - } - - @Override - public boolean equals(Object o) - { - if (this == o) return true; - - if (o == null || getClass() != o.getClass()) return false; - - Score score = (Score) o; - - return new EqualsBuilder() - .append(value, score.value) - .append(timestamp, score.timestamp) - .isEquals(); - } - - @Override - public int hashCode() - { - return new HashCodeBuilder(17, 37) - .append(timestamp) - .append(value) - .toHashCode(); - } -} diff --git a/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/Score.kt b/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/Score.kt new file mode 100644 index 000000000..23b9d2487 --- /dev/null +++ b/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/Score.kt @@ -0,0 +1,58 @@ +/* + * 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 + +import kotlin.math.* + +data class Score( + val timestamp: Timestamp, + val value: Double, +) { + + fun compareNewer(other: Score): Int { + return timestamp.compareTo(other.timestamp) + } + + companion object { + /** + * Given the frequency of the habit, the previous score, and the value of + * the current checkmark, computes the current score for the habit. + * + * The frequency of the habit is the number of repetitions divided by the + * length of the interval. For example, a habit that should be repeated 3 + * times in 8 days has frequency 3.0 / 8.0 = 0.375. + * + * @param frequency the frequency of the habit + * @param previousScore the previous score of the habit + * @param checkmarkValue the value of the current checkmark + * @return the current score + */ + @JvmStatic + fun compute( + frequency: Double, + previousScore: Double, + checkmarkValue: Double, + ): Double { + val multiplier = Math.pow(0.5, sqrt(frequency) / 13.0) + var score = previousScore * multiplier + score += checkmarkValue * (1 - multiplier) + return score + } + } +} \ No newline at end of file diff --git a/android/uhabits-core/src/test/java/org/isoron/uhabits/core/models/HabitTest.java b/android/uhabits-core/src/test/java/org/isoron/uhabits/core/models/HabitTest.java index 66ad32c87..9817ea2c3 100644 --- a/android/uhabits-core/src/test/java/org/isoron/uhabits/core/models/HabitTest.java +++ b/android/uhabits-core/src/test/java/org/isoron/uhabits/core/models/HabitTest.java @@ -133,13 +133,4 @@ public class HabitTest extends BaseUnitTest assertThat(h.getUriString(), equalTo("content://org.isoron.uhabits/habit/0")); } - - @Test - public void testEquals() throws Exception - { - EqualsVerifier.forClass(Score.class).verify(); - EqualsVerifier.forClass(Streak.class).verify(); - EqualsVerifier.forClass(Reminder.class).verify(); - EqualsVerifier.forClass(WeekdayList.class).verify(); - } } \ No newline at end of file diff --git a/android/uhabits-core/src/test/java/org/isoron/uhabits/core/models/ScoreTest.java b/android/uhabits-core/src/test/java/org/isoron/uhabits/core/models/ScoreTest.java index 545c44036..b4e32c308 100644 --- a/android/uhabits-core/src/test/java/org/isoron/uhabits/core/models/ScoreTest.java +++ b/android/uhabits-core/src/test/java/org/isoron/uhabits/core/models/ScoreTest.java @@ -68,12 +68,4 @@ public class ScoreTest extends BaseUnitTest assertThat(compute(freq, 0.5, check), closeTo(0.484842, E)); assertThat(compute(freq, 0.75, check), closeTo(0.727263, E)); } - - - @Test - public void testToString() throws Exception - { - Score score = new Score(Timestamp.ZERO.plus(100), 150.0); - assertThat(score.toString(), equalTo( "{timestamp: 1970-04-11, value: 150.0}")); - } }