AndroidAPS/app/src/main/java/info/nightscout/androidaps/utils/MidnightTime.java

67 lines
1.8 KiB
Java
Raw Normal View History

2019-02-26 20:38:27 +01:00
package info.nightscout.androidaps.utils;
2018-10-28 12:21:35 +01:00
import android.util.LongSparseArray;
import java.util.Calendar;
public class MidnightTime {
2019-04-13 21:49:15 +02:00
static final LongSparseArray<Long> times = new LongSparseArray<>();
2018-10-28 12:21:35 +01:00
private static long hits = 0;
private static long misses = 0;
2019-04-13 21:49:15 +02:00
private static final int THRESHOLD = 100000;
2018-10-28 12:21:35 +01:00
public static long calc() {
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return c.getTimeInMillis();
}
2020-03-29 23:38:58 +02:00
public static long calcPlusMinutes(int minutes) {
int h = minutes / 60;
int m = minutes % 60;
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, h);
c.set(Calendar.MINUTE, m);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return c.getTimeInMillis();
}
2018-10-28 12:21:35 +01:00
public static long calc(long time) {
2019-03-05 23:12:35 +01:00
Long m;
synchronized (times) {
m = times.get(time);
if (m != null) {
++hits;
return m;
}
Calendar c = Calendar.getInstance();
c.setTimeInMillis(time);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
m = c.getTimeInMillis();
times.append(time, m);
++misses;
2019-04-13 21:49:15 +02:00
if (times.size() > THRESHOLD) resetCache();
2018-10-28 12:21:35 +01:00
}
return m;
}
2019-04-13 21:49:15 +02:00
static void resetCache() {
hits = 0;
misses = 0;
times.clear();
}
2018-10-28 12:21:35 +01:00
public static String log() {
return "Hits: " + hits + " misses: " + misses + " stored: " + times.size();
}
}