synchronize interval access

This commit is contained in:
AdrianLxM 2017-07-19 17:38:20 +02:00
parent cb08d1d76b
commit 76990b7983
4 changed files with 23 additions and 23 deletions

View file

@ -18,7 +18,7 @@ public abstract class Intervals<T extends Interval> {
LongSparseArray<T> rawData = new LongSparseArray<T>(); // oldest at index 0 LongSparseArray<T> rawData = new LongSparseArray<T>(); // oldest at index 0
public Intervals reset() { public synchronized Intervals reset() {
rawData = new LongSparseArray<T>(); rawData = new LongSparseArray<T>();
return this; return this;
} }
@ -29,7 +29,7 @@ public abstract class Intervals<T extends Interval> {
* The List must be sorted by `T.start()` in ascending order * The List must be sorted by `T.start()` in ascending order
* *
* */ * */
public void add(List<T> list) { public synchronized void add(List<T> list) {
for (T interval : list) { for (T interval : list) {
rawData.put(interval.start(), interval); rawData.put(interval.start(), interval);
} }
@ -38,21 +38,21 @@ public abstract class Intervals<T extends Interval> {
public List<T> getList() { public synchronized List<T> getList() {
List<T> list = new ArrayList<>(); List<T> list = new ArrayList<>();
for (int i = 0; i < rawData.size(); i++) for (int i = 0; i < rawData.size(); i++)
list.add(rawData.valueAt(i)); list.add(rawData.valueAt(i));
return list; return list;
} }
public List<T> getReversedList() { public synchronized List<T> getReversedList() {
List<T> list = new ArrayList<>(); List<T> list = new ArrayList<>();
for (int i = rawData.size() -1; i>=0; i--) for (int i = rawData.size() -1; i>=0; i--)
list.add(rawData.valueAt(i)); list.add(rawData.valueAt(i));
return list; return list;
} }
protected int binarySearch(long value) { protected synchronized int binarySearch(long value) {
int lo = 0; int lo = 0;
int hi = rawData.size() - 1; int hi = rawData.size() - 1;
@ -73,15 +73,15 @@ public abstract class Intervals<T extends Interval> {
public abstract T getValueByInterval(long time); public abstract T getValueByInterval(long time);
public int size() { public synchronized int size() {
return rawData.size(); return rawData.size();
} }
public T get(int index) { public synchronized T get(int index) {
return rawData.valueAt(index); return rawData.valueAt(index);
} }
public T getReversed(int index) { public synchronized T getReversed(int index) {
return rawData.valueAt(size() - 1 - index); return rawData.valueAt(size() - 1 - index);
} }

View file

@ -11,7 +11,7 @@ import info.nightscout.androidaps.interfaces.Interval;
public class NonOverlappingIntervals<T extends Interval> extends Intervals<T> { public class NonOverlappingIntervals<T extends Interval> extends Intervals<T> {
protected void merge() { protected synchronized void merge() {
for (int index = 0; index < rawData.size() - 1; index++) { for (int index = 0; index < rawData.size() - 1; index++) {
Interval i = rawData.valueAt(index); Interval i = rawData.valueAt(index);
long startOfNewer = rawData.valueAt(index + 1).start(); long startOfNewer = rawData.valueAt(index + 1).start();
@ -22,7 +22,7 @@ public class NonOverlappingIntervals<T extends Interval> extends Intervals<T> {
} }
@Nullable @Nullable
public T getValueByInterval(long time) { public synchronized T getValueByInterval(long time) {
int index = binarySearch(time); int index = binarySearch(time);
if (index >= 0) return rawData.valueAt(index); if (index >= 0) return rawData.valueAt(index);
return null; return null;

View file

@ -11,7 +11,7 @@ import info.nightscout.androidaps.interfaces.Interval;
public class OverlappingIntervals<T extends Interval> extends Intervals<T> { public class OverlappingIntervals<T extends Interval> extends Intervals<T> {
protected void merge() { protected synchronized void merge() {
boolean needToCut = false; boolean needToCut = false;
long cutTime = 0; long cutTime = 0;
@ -30,7 +30,7 @@ public class OverlappingIntervals<T extends Interval> extends Intervals<T> {
} }
@Nullable @Nullable
public T getValueByInterval(long time) { public synchronized T getValueByInterval(long time) {
for (int index = rawData.size()-1; index > 0; index--) { //begin with newest for (int index = rawData.size()-1; index > 0; index--) { //begin with newest
T cur = rawData.valueAt(index); T cur = rawData.valueAt(index);
if (cur.match(time)){ if (cur.match(time)){

View file

@ -19,24 +19,24 @@ public class ProfileIntervals<T extends Interval> {
private LongSparseArray<T> rawData = new LongSparseArray<>(); // oldest at index 0 private LongSparseArray<T> rawData = new LongSparseArray<>(); // oldest at index 0
public ProfileIntervals reset() { public synchronized ProfileIntervals reset() {
rawData = new LongSparseArray<>(); rawData = new LongSparseArray<>();
return this; return this;
} }
public void add(T newInterval) { public synchronized void add(T newInterval) {
rawData.put(newInterval.start(), newInterval); rawData.put(newInterval.start(), newInterval);
merge(); merge();
} }
public void add(List<T> list) { public synchronized void add(List<T> list) {
for (T interval : list) { for (T interval : list) {
rawData.put(interval.start(), interval); rawData.put(interval.start(), interval);
} }
merge(); merge();
} }
private void merge() { private synchronized void merge() {
for (int index = 0; index < rawData.size() - 1; index++) { for (int index = 0; index < rawData.size() - 1; index++) {
Interval i = rawData.valueAt(index); Interval i = rawData.valueAt(index);
long startOfNewer = rawData.valueAt(index + 1).start(); long startOfNewer = rawData.valueAt(index + 1).start();
@ -47,27 +47,27 @@ public class ProfileIntervals<T extends Interval> {
} }
@Nullable @Nullable
public Interval getValueToTime(long time) { public synchronized Interval getValueToTime(long time) {
int index = binarySearch(time); int index = binarySearch(time);
if (index >= 0) return rawData.valueAt(index); if (index >= 0) return rawData.valueAt(index);
return null; return null;
} }
public List<T> getList() { public synchronized List<T> getList() {
List<T> list = new ArrayList<>(); List<T> list = new ArrayList<>();
for (int i = 0; i < rawData.size(); i++) for (int i = 0; i < rawData.size(); i++)
list.add(rawData.valueAt(i)); list.add(rawData.valueAt(i));
return list; return list;
} }
public List<T> getReversedList() { public synchronized List<T> getReversedList() {
List<T> list = new ArrayList<>(); List<T> list = new ArrayList<>();
for (int i = rawData.size() -1; i>=0; i--) for (int i = rawData.size() -1; i>=0; i--)
list.add(rawData.valueAt(i)); list.add(rawData.valueAt(i));
return list; return list;
} }
private int binarySearch(long value) { private synchronized int binarySearch(long value) {
if (rawData.size() == 0) if (rawData.size() == 0)
return -1; return -1;
int lo = 0; int lo = 0;
@ -95,15 +95,15 @@ public class ProfileIntervals<T extends Interval> {
return -1; // value not present return -1; // value not present
} }
public int size() { public synchronized int size() {
return rawData.size(); return rawData.size();
} }
public T get(int index) { public synchronized T get(int index) {
return rawData.valueAt(index); return rawData.valueAt(index);
} }
public T getReversed(int index) { public synchronized T getReversed(int index) {
return rawData.valueAt(size() - 1 - index); return rawData.valueAt(size() - 1 - index);
} }
} }