mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Make model classes thread-safe
This commit is contained in:
@@ -77,6 +77,7 @@ dependencies {
|
|||||||
compile 'com.opencsv:opencsv:3.7'
|
compile 'com.opencsv:opencsv:3.7'
|
||||||
compile 'org.apmem.tools:layouts:1.10@aar'
|
compile 'org.apmem.tools:layouts:1.10@aar'
|
||||||
compile 'org.jetbrains:annotations-java5:15.0'
|
compile 'org.jetbrains:annotations-java5:15.0'
|
||||||
|
compile 'com.google.code.findbugs:jsr305:2.0.1'
|
||||||
|
|
||||||
provided 'javax.annotation:jsr250-api:1.0'
|
provided 'javax.annotation:jsr250-api:1.0'
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ package org.isoron.uhabits.models;
|
|||||||
|
|
||||||
import org.apache.commons.lang3.builder.*;
|
import org.apache.commons.lang3.builder.*;
|
||||||
|
|
||||||
|
import javax.annotation.concurrent.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A Checkmark represents the completion status of the habit for a given day.
|
* A Checkmark represents the completion status of the habit for a given day.
|
||||||
* <p>
|
* <p>
|
||||||
@@ -30,6 +32,7 @@ import org.apache.commons.lang3.builder.*;
|
|||||||
* <p>
|
* <p>
|
||||||
* Checkmarks are computed automatically from the list of repetitions.
|
* Checkmarks are computed automatically from the list of repetitions.
|
||||||
*/
|
*/
|
||||||
|
@ThreadSafe
|
||||||
public final class Checkmark
|
public final class Checkmark
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -27,18 +27,22 @@ import java.io.*;
|
|||||||
import java.text.*;
|
import java.text.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
import javax.annotation.concurrent.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The collection of {@link Checkmark}s belonging to a habit.
|
* The collection of {@link Checkmark}s belonging to a habit.
|
||||||
*/
|
*/
|
||||||
|
@ThreadSafe
|
||||||
public abstract class CheckmarkList
|
public abstract class CheckmarkList
|
||||||
{
|
{
|
||||||
protected Habit habit;
|
protected final Habit habit;
|
||||||
|
|
||||||
public ModelObservable observable = new ModelObservable();
|
public final ModelObservable observable;
|
||||||
|
|
||||||
public CheckmarkList(Habit habit)
|
public CheckmarkList(Habit habit)
|
||||||
{
|
{
|
||||||
this.habit = habit;
|
this.habit = habit;
|
||||||
|
this.observable = new ModelObservable();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -64,7 +68,7 @@ public abstract class CheckmarkList
|
|||||||
* @return values for the checkmarks in the interval
|
* @return values for the checkmarks in the interval
|
||||||
*/
|
*/
|
||||||
@NonNull
|
@NonNull
|
||||||
public final int[] getAllValues()
|
public synchronized final int[] getAllValues()
|
||||||
{
|
{
|
||||||
Repetition oldestRep = habit.getRepetitions().getOldest();
|
Repetition oldestRep = habit.getRepetitions().getOldest();
|
||||||
if (oldestRep == null) return new int[0];
|
if (oldestRep == null) return new int[0];
|
||||||
@@ -97,7 +101,7 @@ public abstract class CheckmarkList
|
|||||||
* @return checkmark for today
|
* @return checkmark for today
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public final Checkmark getToday()
|
public synchronized final Checkmark getToday()
|
||||||
{
|
{
|
||||||
computeAll();
|
computeAll();
|
||||||
return getNewestComputed();
|
return getNewestComputed();
|
||||||
@@ -108,7 +112,7 @@ public abstract class CheckmarkList
|
|||||||
*
|
*
|
||||||
* @return value of today's checkmark
|
* @return value of today's checkmark
|
||||||
*/
|
*/
|
||||||
public final int getTodayValue()
|
public synchronized final int getTodayValue()
|
||||||
{
|
{
|
||||||
Checkmark today = getToday();
|
Checkmark today = getToday();
|
||||||
if (today != null) return today.getValue();
|
if (today != null) return today.getValue();
|
||||||
@@ -159,9 +163,14 @@ public abstract class CheckmarkList
|
|||||||
*/
|
*/
|
||||||
public final void writeCSV(Writer out) throws IOException
|
public final void writeCSV(Writer out) throws IOException
|
||||||
{
|
{
|
||||||
computeAll();
|
int values[];
|
||||||
|
|
||||||
|
synchronized (this)
|
||||||
|
{
|
||||||
|
computeAll();
|
||||||
|
values = getAllValues();
|
||||||
|
}
|
||||||
|
|
||||||
int values[] = getAllValues();
|
|
||||||
long timestamp = DateUtils.getStartOfToday();
|
long timestamp = DateUtils.getStartOfToday();
|
||||||
SimpleDateFormat dateFormat = DateFormats.getCSVDateFormat();
|
SimpleDateFormat dateFormat = DateFormats.getCSVDateFormat();
|
||||||
|
|
||||||
@@ -271,7 +280,7 @@ public abstract class CheckmarkList
|
|||||||
* repetition of the habit until today. Days that already have a
|
* repetition of the habit until today. Days that already have a
|
||||||
* corresponding checkmark are skipped.
|
* corresponding checkmark are skipped.
|
||||||
*/
|
*/
|
||||||
protected final void computeAll()
|
private synchronized void computeAll()
|
||||||
{
|
{
|
||||||
Repetition oldest = habit.getRepetitions().getOldest();
|
Repetition oldest = habit.getRepetitions().getOldest();
|
||||||
if (oldest == null) return;
|
if (oldest == null) return;
|
||||||
|
|||||||
@@ -21,9 +21,12 @@ package org.isoron.uhabits.models;
|
|||||||
|
|
||||||
import org.apache.commons.lang3.builder.*;
|
import org.apache.commons.lang3.builder.*;
|
||||||
|
|
||||||
|
import javax.annotation.concurrent.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents how often is the habit repeated.
|
* Represents how often is the habit repeated.
|
||||||
*/
|
*/
|
||||||
|
@ThreadSafe
|
||||||
public class Frequency
|
public class Frequency
|
||||||
{
|
{
|
||||||
public static final Frequency DAILY = new Frequency(1, 1);
|
public static final Frequency DAILY = new Frequency(1, 1);
|
||||||
|
|||||||
@@ -26,11 +26,13 @@ import org.apache.commons.lang3.builder.*;
|
|||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
import javax.annotation.concurrent.*;
|
||||||
import javax.inject.*;
|
import javax.inject.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The thing that the user wants to track.
|
* The thing that the user wants to track.
|
||||||
*/
|
*/
|
||||||
|
@ThreadSafe
|
||||||
public class Habit
|
public class Habit
|
||||||
{
|
{
|
||||||
public static final String HABIT_URI_FORMAT =
|
public static final String HABIT_URI_FORMAT =
|
||||||
@@ -54,22 +56,23 @@ public class Habit
|
|||||||
@NonNull
|
@NonNull
|
||||||
private boolean archived;
|
private boolean archived;
|
||||||
|
|
||||||
@NonNull
|
|
||||||
private StreakList streaks;
|
|
||||||
|
|
||||||
@NonNull
|
|
||||||
private ScoreList scores;
|
|
||||||
|
|
||||||
@NonNull
|
|
||||||
private RepetitionList repetitions;
|
|
||||||
|
|
||||||
@NonNull
|
|
||||||
private CheckmarkList checkmarks;
|
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private Reminder reminder;
|
private Reminder reminder;
|
||||||
|
|
||||||
private ModelObservable observable = new ModelObservable();
|
@NonNull
|
||||||
|
private final StreakList streaks;
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private final ScoreList scores;
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private final RepetitionList repetitions;
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private final CheckmarkList checkmarks;
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private final ModelObservable observable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a habit with default attributes.
|
* Constructs a habit with default attributes.
|
||||||
@@ -88,12 +91,13 @@ public class Habit
|
|||||||
streaks = factory.buildStreakList(this);
|
streaks = factory.buildStreakList(this);
|
||||||
scores = factory.buildScoreList(this);
|
scores = factory.buildScoreList(this);
|
||||||
repetitions = factory.buildRepetitionList(this);
|
repetitions = factory.buildRepetitionList(this);
|
||||||
|
observable = new ModelObservable();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clears the reminder for a habit.
|
* Clears the reminder for a habit.
|
||||||
*/
|
*/
|
||||||
public void clearReminder()
|
public synchronized void clearReminder()
|
||||||
{
|
{
|
||||||
reminder = null;
|
reminder = null;
|
||||||
observable.notifyListeners();
|
observable.notifyListeners();
|
||||||
@@ -104,7 +108,7 @@ public class Habit
|
|||||||
*
|
*
|
||||||
* @param model the model whose attributes should be copied from
|
* @param model the model whose attributes should be copied from
|
||||||
*/
|
*/
|
||||||
public void copyFrom(@NonNull Habit model)
|
public synchronized void copyFrom(@NonNull Habit model)
|
||||||
{
|
{
|
||||||
this.name = model.getName();
|
this.name = model.getName();
|
||||||
this.description = model.getDescription();
|
this.description = model.getDescription();
|
||||||
@@ -119,7 +123,7 @@ public class Habit
|
|||||||
* List of checkmarks belonging to this habit.
|
* List of checkmarks belonging to this habit.
|
||||||
*/
|
*/
|
||||||
@NonNull
|
@NonNull
|
||||||
public CheckmarkList getCheckmarks()
|
public synchronized CheckmarkList getCheckmarks()
|
||||||
{
|
{
|
||||||
return checkmarks;
|
return checkmarks;
|
||||||
}
|
}
|
||||||
@@ -133,56 +137,56 @@ public class Habit
|
|||||||
* habit.color).
|
* habit.color).
|
||||||
*/
|
*/
|
||||||
@NonNull
|
@NonNull
|
||||||
public Integer getColor()
|
public synchronized Integer getColor()
|
||||||
{
|
{
|
||||||
return color;
|
return color;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setColor(@NonNull Integer color)
|
public synchronized void setColor(@NonNull Integer color)
|
||||||
{
|
{
|
||||||
this.color = color;
|
this.color = color;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
public String getDescription()
|
public synchronized String getDescription()
|
||||||
{
|
{
|
||||||
return description;
|
return description;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDescription(@NonNull String description)
|
public synchronized void setDescription(@NonNull String description)
|
||||||
{
|
{
|
||||||
this.description = description;
|
this.description = description;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
public Frequency getFrequency()
|
public synchronized Frequency getFrequency()
|
||||||
{
|
{
|
||||||
return frequency;
|
return frequency;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFrequency(@NonNull Frequency frequency)
|
public synchronized void setFrequency(@NonNull Frequency frequency)
|
||||||
{
|
{
|
||||||
this.frequency = frequency;
|
this.frequency = frequency;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public Long getId()
|
public synchronized Long getId()
|
||||||
{
|
{
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setId(@Nullable Long id)
|
public synchronized void setId(@Nullable Long id)
|
||||||
{
|
{
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
public String getName()
|
public synchronized String getName()
|
||||||
{
|
{
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setName(@NonNull String name)
|
public synchronized void setName(@NonNull String name)
|
||||||
{
|
{
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
@@ -203,13 +207,13 @@ public class Habit
|
|||||||
* @throws IllegalStateException if habit has no reminder
|
* @throws IllegalStateException if habit has no reminder
|
||||||
*/
|
*/
|
||||||
@NonNull
|
@NonNull
|
||||||
public Reminder getReminder()
|
public synchronized Reminder getReminder()
|
||||||
{
|
{
|
||||||
if (reminder == null) throw new IllegalStateException();
|
if (reminder == null) throw new IllegalStateException();
|
||||||
return reminder;
|
return reminder;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setReminder(@Nullable Reminder reminder)
|
public synchronized void setReminder(@Nullable Reminder reminder)
|
||||||
{
|
{
|
||||||
this.reminder = reminder;
|
this.reminder = reminder;
|
||||||
}
|
}
|
||||||
@@ -248,17 +252,17 @@ public class Habit
|
|||||||
*
|
*
|
||||||
* @return true if habit has reminder, false otherwise
|
* @return true if habit has reminder, false otherwise
|
||||||
*/
|
*/
|
||||||
public boolean hasReminder()
|
public synchronized boolean hasReminder()
|
||||||
{
|
{
|
||||||
return reminder != null;
|
return reminder != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isArchived()
|
public synchronized boolean isArchived()
|
||||||
{
|
{
|
||||||
return archived;
|
return archived;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setArchived(boolean archived)
|
public synchronized void setArchived(boolean archived)
|
||||||
{
|
{
|
||||||
this.archived = archived;
|
this.archived = archived;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,12 +28,15 @@ import org.isoron.uhabits.utils.*;
|
|||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
import javax.annotation.concurrent.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An ordered collection of {@link Habit}s.
|
* An ordered collection of {@link Habit}s.
|
||||||
*/
|
*/
|
||||||
|
@ThreadSafe
|
||||||
public abstract class HabitList implements Iterable<Habit>
|
public abstract class HabitList implements Iterable<Habit>
|
||||||
{
|
{
|
||||||
private ModelObservable observable;
|
private final ModelObservable observable;
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
protected final HabitMatcher filter;
|
protected final HabitMatcher filter;
|
||||||
|
|||||||
@@ -21,10 +21,13 @@ package org.isoron.uhabits.models;
|
|||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
import javax.annotation.concurrent.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A ModelObservable allows objects to subscribe themselves to it and receive
|
* A ModelObservable allows objects to subscribe themselves to it and receive
|
||||||
* notifications whenever the model is changed.
|
* notifications whenever the model is changed.
|
||||||
*/
|
*/
|
||||||
|
@ThreadSafe
|
||||||
public class ModelObservable
|
public class ModelObservable
|
||||||
{
|
{
|
||||||
private List<Listener> listeners;
|
private List<Listener> listeners;
|
||||||
@@ -43,7 +46,7 @@ public class ModelObservable
|
|||||||
*
|
*
|
||||||
* @param l the listener to be added.
|
* @param l the listener to be added.
|
||||||
*/
|
*/
|
||||||
public void addListener(Listener l)
|
public synchronized void addListener(Listener l)
|
||||||
{
|
{
|
||||||
listeners.add(l);
|
listeners.add(l);
|
||||||
}
|
}
|
||||||
@@ -53,7 +56,7 @@ public class ModelObservable
|
|||||||
* <p>
|
* <p>
|
||||||
* Only models should call this method.
|
* Only models should call this method.
|
||||||
*/
|
*/
|
||||||
public void notifyListeners()
|
public synchronized void notifyListeners()
|
||||||
{
|
{
|
||||||
for (Listener l : listeners) l.onModelChange();
|
for (Listener l : listeners) l.onModelChange();
|
||||||
}
|
}
|
||||||
@@ -66,7 +69,7 @@ public class ModelObservable
|
|||||||
*
|
*
|
||||||
* @param l the listener to be removed
|
* @param l the listener to be removed
|
||||||
*/
|
*/
|
||||||
public void removeListener(Listener l)
|
public synchronized void removeListener(Listener l)
|
||||||
{
|
{
|
||||||
listeners.remove(l);
|
listeners.remove(l);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ public class AndroidTaskRunner implements TaskRunner
|
|||||||
public void execute(Task task)
|
public void execute(Task task)
|
||||||
{
|
{
|
||||||
task.onAttached(this);
|
task.onAttached(this);
|
||||||
new CustomAsyncTask(task).execute();
|
new CustomAsyncTask(task).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user