mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Use more simple toString style
This commit is contained in:
@@ -23,6 +23,8 @@ import org.apache.commons.lang3.builder.*;
|
||||
|
||||
import javax.annotation.concurrent.*;
|
||||
|
||||
import static org.isoron.uhabits.core.utils.StringUtils.defaultToStringStyle;
|
||||
|
||||
/**
|
||||
* A Checkmark represents the completion status of the habit for a given day.
|
||||
* <p>
|
||||
@@ -108,7 +110,7 @@ public final class Checkmark
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return new ToStringBuilder(this)
|
||||
return new ToStringBuilder(this, defaultToStringStyle())
|
||||
.append("timestamp", timestamp)
|
||||
.append("value", value)
|
||||
.toString();
|
||||
|
||||
@@ -31,6 +31,7 @@ import java.util.*;
|
||||
import javax.annotation.concurrent.*;
|
||||
|
||||
import static org.isoron.uhabits.core.models.Checkmark.*;
|
||||
import static org.isoron.uhabits.core.utils.StringUtils.defaultToStringStyle;
|
||||
|
||||
/**
|
||||
* The collection of {@link Checkmark}s belonging to a habit.
|
||||
@@ -396,7 +397,7 @@ public abstract class CheckmarkList
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return new ToStringBuilder(this)
|
||||
return new ToStringBuilder(this, defaultToStringStyle())
|
||||
.append("begin", begin)
|
||||
.append("center", center)
|
||||
.append("end", end)
|
||||
|
||||
@@ -23,6 +23,8 @@ import org.apache.commons.lang3.builder.*;
|
||||
|
||||
import javax.annotation.concurrent.*;
|
||||
|
||||
import static org.isoron.uhabits.core.utils.StringUtils.defaultToStringStyle;
|
||||
|
||||
/**
|
||||
* Represents how often is the habit repeated.
|
||||
*/
|
||||
@@ -92,7 +94,7 @@ public class Frequency
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return new ToStringBuilder(this)
|
||||
return new ToStringBuilder(this, defaultToStringStyle())
|
||||
.append("numerator", numerator)
|
||||
.append("denominator", denominator)
|
||||
.toString();
|
||||
|
||||
@@ -29,6 +29,7 @@ import javax.annotation.concurrent.*;
|
||||
import javax.inject.*;
|
||||
|
||||
import static org.isoron.uhabits.core.models.Checkmark.*;
|
||||
import static org.isoron.uhabits.core.utils.StringUtils.defaultToStringStyle;
|
||||
|
||||
/**
|
||||
* The thing that the user wants to track.
|
||||
@@ -414,7 +415,7 @@ public class Habit
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return new ToStringBuilder(this)
|
||||
return new ToStringBuilder(this, defaultToStringStyle())
|
||||
.append("name", name)
|
||||
.append("description", description)
|
||||
.append("frequency", frequency)
|
||||
@@ -475,7 +476,7 @@ public class Habit
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return new ToStringBuilder(this)
|
||||
return new ToStringBuilder(this, defaultToStringStyle())
|
||||
.append("id", id)
|
||||
.append("data", data)
|
||||
.toString();
|
||||
|
||||
@@ -23,6 +23,8 @@ import android.support.annotation.*;
|
||||
|
||||
import org.apache.commons.lang3.builder.*;
|
||||
|
||||
import static org.isoron.uhabits.core.utils.StringUtils.defaultToStringStyle;
|
||||
|
||||
public final class Reminder
|
||||
{
|
||||
private final int hour;
|
||||
@@ -83,7 +85,7 @@ public final class Reminder
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return new ToStringBuilder(this)
|
||||
return new ToStringBuilder(this, defaultToStringStyle())
|
||||
.append("hour", hour)
|
||||
.append("minute", minute)
|
||||
.append("days", days)
|
||||
|
||||
@@ -21,6 +21,8 @@ package org.isoron.uhabits.core.models;
|
||||
|
||||
import org.apache.commons.lang3.builder.*;
|
||||
|
||||
import static org.isoron.uhabits.core.utils.StringUtils.defaultToStringStyle;
|
||||
|
||||
/**
|
||||
* Represents a record that the user has performed a certain habit at a certain
|
||||
* date.
|
||||
@@ -89,7 +91,7 @@ public final class Repetition
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return new ToStringBuilder(this)
|
||||
return new ToStringBuilder(this, defaultToStringStyle())
|
||||
.append("timestamp", timestamp)
|
||||
.append("value", value)
|
||||
.toString();
|
||||
|
||||
@@ -22,6 +22,7 @@ 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.
|
||||
@@ -88,7 +89,7 @@ public final class Score
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return new ToStringBuilder(this)
|
||||
return new ToStringBuilder(this, defaultToStringStyle())
|
||||
.append("timestamp", timestamp)
|
||||
.append("value", value)
|
||||
.toString();
|
||||
|
||||
@@ -21,6 +21,8 @@ package org.isoron.uhabits.core.models;
|
||||
|
||||
import org.apache.commons.lang3.builder.*;
|
||||
|
||||
import static org.isoron.uhabits.core.utils.StringUtils.defaultToStringStyle;
|
||||
|
||||
public final class Streak
|
||||
{
|
||||
private final Timestamp start;
|
||||
@@ -64,7 +66,7 @@ public final class Streak
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return new ToStringBuilder(this)
|
||||
return new ToStringBuilder(this, defaultToStringStyle())
|
||||
.append("start", start)
|
||||
.append("end", end)
|
||||
.toString();
|
||||
|
||||
@@ -24,6 +24,7 @@ import org.apache.commons.lang3.builder.*;
|
||||
import java.util.*;
|
||||
|
||||
import static java.util.Calendar.DAY_OF_WEEK;
|
||||
import static org.isoron.uhabits.core.utils.StringUtils.defaultToStringStyle;
|
||||
|
||||
public final class Timestamp
|
||||
{
|
||||
@@ -137,7 +138,7 @@ public final class Timestamp
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return new ToStringBuilder(this)
|
||||
return new ToStringBuilder(this, defaultToStringStyle())
|
||||
.append("unixTime", unixTime)
|
||||
.toString();
|
||||
}
|
||||
|
||||
@@ -23,6 +23,8 @@ import org.apache.commons.lang3.builder.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static org.isoron.uhabits.core.utils.StringUtils.defaultToStringStyle;
|
||||
|
||||
public final class WeekdayList
|
||||
{
|
||||
public static final WeekdayList EVERY_DAY = new WeekdayList(127);
|
||||
@@ -92,7 +94,7 @@ public final class WeekdayList
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return new ToStringBuilder(this)
|
||||
return new ToStringBuilder(this, defaultToStringStyle())
|
||||
.append("weekdays", weekdays)
|
||||
.toString();
|
||||
}
|
||||
|
||||
@@ -19,18 +19,35 @@
|
||||
|
||||
package org.isoron.uhabits.core.utils;
|
||||
|
||||
import org.apache.commons.lang3.builder.*;
|
||||
|
||||
import java.math.*;
|
||||
import java.util.*;
|
||||
|
||||
public class StringUtils
|
||||
{
|
||||
private static StandardToStringStyle toStringStyle = null;
|
||||
|
||||
public static String getRandomId()
|
||||
{
|
||||
return new BigInteger(260, new Random()).toString(32).substring(0, 32);
|
||||
}
|
||||
|
||||
public static String removePointers(String original)
|
||||
public static ToStringStyle defaultToStringStyle()
|
||||
{
|
||||
return original.replaceAll("@[0-9a-f]*", "@00000000");
|
||||
if (toStringStyle == null)
|
||||
{
|
||||
toStringStyle = new StandardToStringStyle();
|
||||
toStringStyle.setFieldSeparator(", ");
|
||||
toStringStyle.setUseClassName(false);
|
||||
toStringStyle.setUseIdentityHashCode(false);
|
||||
toStringStyle.setContentStart("{");
|
||||
toStringStyle.setContentEnd("}");
|
||||
toStringStyle.setFieldNameValueSeparator(": ");
|
||||
toStringStyle.setArrayStart("[");
|
||||
toStringStyle.setArrayEnd("]");
|
||||
}
|
||||
|
||||
return toStringStyle;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,6 @@ import nl.jqno.equalsverifier.*;
|
||||
import static org.hamcrest.MatcherAssert.*;
|
||||
import static org.hamcrest.core.IsEqual.*;
|
||||
import static org.isoron.uhabits.core.models.Checkmark.*;
|
||||
import static org.isoron.uhabits.core.utils.StringUtils.removePointers;
|
||||
|
||||
public class CheckmarkListTest extends BaseUnitTest
|
||||
{
|
||||
@@ -360,21 +359,14 @@ public class CheckmarkListTest extends BaseUnitTest
|
||||
{
|
||||
Timestamp t = Timestamp.ZERO.plus(100);
|
||||
Checkmark checkmark = new Checkmark(t, 2);
|
||||
String s = removePointers(checkmark.toString());
|
||||
assertThat(s, equalTo(
|
||||
"org.isoron.uhabits.core.models.Checkmark@00000000[" +
|
||||
"timestamp=org.isoron.uhabits.core.models.Timestamp@00000000[unixTime=8640000000]," +
|
||||
"value=2]"));
|
||||
|
||||
assertThat(checkmark.toString(),
|
||||
equalTo("{timestamp: {unixTime: 8640000000}, value: 2}"));
|
||||
|
||||
CheckmarkList.Interval interval =
|
||||
new CheckmarkList.Interval(t, t.plus(1), t.plus(2));
|
||||
s = removePointers(interval.toString());
|
||||
assertThat(s, equalTo(
|
||||
"org.isoron.uhabits.core.models.CheckmarkList$Interval@00000000[" +
|
||||
"begin=org.isoron.uhabits.core.models.Timestamp@00000000[unixTime=8640000000]," +
|
||||
"center=org.isoron.uhabits.core.models.Timestamp@00000000[unixTime=8726400000]," +
|
||||
"end=org.isoron.uhabits.core.models.Timestamp@00000000[unixTime=8812800000]]"));
|
||||
assertThat(interval.toString(), equalTo(
|
||||
"{begin: {unixTime: 8640000000}, center: {unixTime: 8726400000}," +
|
||||
" end: {unixTime: 8812800000}}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -27,7 +27,6 @@ import nl.jqno.equalsverifier.*;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.isoron.uhabits.core.utils.DateUtils.*;
|
||||
import static org.isoron.uhabits.core.utils.StringUtils.removePointers;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class HabitTest extends BaseUnitTest
|
||||
@@ -150,22 +149,14 @@ public class HabitTest extends BaseUnitTest
|
||||
{
|
||||
Habit h = modelFactory.buildHabit();
|
||||
h.setReminder(new Reminder(22, 30, WeekdayList.EVERY_DAY));
|
||||
String expected = "{id: <null>, data: {name: , description: ," +
|
||||
" frequency: {numerator: 3, denominator: 7}," +
|
||||
" color: 8, archived: false, targetType: 0," +
|
||||
" targetValue: 100.0, type: 0, unit: ," +
|
||||
" reminder: {hour: 22, minute: 30," +
|
||||
" days: {weekdays: [true,true,true,true,true,true,true]}}," +
|
||||
" position: 0}}";
|
||||
|
||||
String s = removePointers(h.toString());
|
||||
|
||||
String expected =
|
||||
"org.isoron.uhabits.core.models.Habit@00000000[" +
|
||||
"id=<null>," +
|
||||
"data=org.isoron.uhabits.core.models.Habit$HabitData@00000000[" +
|
||||
"name=,description=," +
|
||||
"frequency=org.isoron.uhabits.core.models.Frequency@00000000[numerator=3,denominator=7]," +
|
||||
"color=8,archived=false,targetType=0,targetValue=100.0,type=0,unit=," +
|
||||
"reminder=org.isoron.uhabits.core.models.Reminder@00000000[" +
|
||||
"hour=22,minute=30," +
|
||||
"days=org.isoron.uhabits.core.models.WeekdayList@00000000[" +
|
||||
"weekdays={true,true,true,true,true,true,true}]]," +
|
||||
"position=0]]";
|
||||
|
||||
assertThat(s, equalTo(expected));
|
||||
assertThat(h.toString(), equalTo(expected));
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,6 @@ 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.utils.StringUtils.removePointers;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.*;
|
||||
@@ -192,10 +191,6 @@ public class RepetitionListTest extends BaseUnitTest
|
||||
public void testToString() throws Exception
|
||||
{
|
||||
Repetition rep = new Repetition(Timestamp.ZERO.plus(100), 20);
|
||||
String s = removePointers(rep.toString());
|
||||
assertThat(s, equalTo(
|
||||
"org.isoron.uhabits.core.models.Repetition@00000000[" +
|
||||
"timestamp=org.isoron.uhabits.core.models.Timestamp@00000000[unixTime=8640000000]," +
|
||||
"value=20]"));
|
||||
assertThat(rep.toString(), equalTo("{timestamp: {unixTime: 8640000000}, value: 20}"));
|
||||
}
|
||||
}
|
||||
@@ -22,11 +22,10 @@ package org.isoron.uhabits.core.models;
|
||||
import org.isoron.uhabits.core.*;
|
||||
import org.junit.*;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
import static org.hamcrest.number.IsCloseTo.closeTo;
|
||||
import static org.isoron.uhabits.core.models.Score.compute;
|
||||
import static org.isoron.uhabits.core.utils.StringUtils.removePointers;
|
||||
import static org.hamcrest.MatcherAssert.*;
|
||||
import static org.hamcrest.core.IsEqual.*;
|
||||
import static org.hamcrest.number.IsCloseTo.*;
|
||||
import static org.isoron.uhabits.core.models.Score.*;
|
||||
|
||||
|
||||
public class ScoreTest extends BaseUnitTest
|
||||
@@ -75,10 +74,6 @@ public class ScoreTest extends BaseUnitTest
|
||||
public void testToString() throws Exception
|
||||
{
|
||||
Score score = new Score(Timestamp.ZERO.plus(100), 150.0);
|
||||
String string = removePointers(score.toString());
|
||||
assertThat(string, equalTo(
|
||||
"org.isoron.uhabits.core.models.Score@00000000[" +
|
||||
"timestamp=org.isoron.uhabits.core.models.Timestamp@00000000[unixTime=8640000000]," +
|
||||
"value=150.0]"));
|
||||
assertThat(score.toString(), equalTo( "{timestamp: {unixTime: 8640000000}, value: 150.0}"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,6 @@ import java.util.*;
|
||||
import static junit.framework.TestCase.*;
|
||||
import static org.hamcrest.MatcherAssert.*;
|
||||
import static org.hamcrest.core.IsEqual.*;
|
||||
import static org.isoron.uhabits.core.utils.StringUtils.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
public class StreakListTest extends BaseUnitTest
|
||||
@@ -124,10 +123,7 @@ public class StreakListTest extends BaseUnitTest
|
||||
{
|
||||
Timestamp time = Timestamp.ZERO.plus(100);
|
||||
Streak streak = new Streak(time, time.plus(10));
|
||||
String string = removePointers(streak.toString());
|
||||
assertThat(string, equalTo(
|
||||
"org.isoron.uhabits.core.models.Streak@00000000[" +
|
||||
"start=org.isoron.uhabits.core.models.Timestamp@00000000[unixTime=8640000000]," +
|
||||
"end=org.isoron.uhabits.core.models.Timestamp@00000000[unixTime=9504000000]]"));
|
||||
assertThat(streak.toString(), equalTo(
|
||||
"{start: {unixTime: 8640000000}, end: {unixTime: 9504000000}}"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user