MidnightTime -> kt

This commit is contained in:
Milos Kozak 2021-12-13 17:49:06 +01:00
parent 6fa4c880fd
commit af228634cb
2 changed files with 64 additions and 66 deletions

View file

@ -1,66 +0,0 @@
package info.nightscout.androidaps.utils;
import android.util.LongSparseArray;
import java.util.Calendar;
public class MidnightTime {
static final LongSparseArray<Long> times = new LongSparseArray<>();
private static long hits = 0;
private static long misses = 0;
private static final int THRESHOLD = 100000;
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();
}
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();
}
public static long calc(long time) {
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;
if (times.size() > THRESHOLD) resetCache();
}
return m;
}
static void resetCache() {
hits = 0;
misses = 0;
times.clear();
}
public static String log() {
return "Hits: " + hits + " misses: " + misses + " stored: " + times.size();
}
}

View file

@ -0,0 +1,64 @@
package info.nightscout.androidaps.utils
import android.util.LongSparseArray
import java.util.*
object MidnightTime {
val times = LongSparseArray<Long>()
private var hits: Long = 0
private var misses: Long = 0
private const val THRESHOLD = 100000
fun calc(): Long {
val c = Calendar.getInstance()
c[Calendar.HOUR_OF_DAY] = 0
c[Calendar.MINUTE] = 0
c[Calendar.SECOND] = 0
c[Calendar.MILLISECOND] = 0
return c.timeInMillis
}
fun calcPlusMinutes(minutes: Int): Long {
val h = minutes / 60
val m = minutes % 60
val c = Calendar.getInstance()
c[Calendar.HOUR_OF_DAY] = h
c[Calendar.MINUTE] = m
c[Calendar.SECOND] = 0
c[Calendar.MILLISECOND] = 0
return c.timeInMillis
}
fun calc(time: Long): Long {
var m: Long?
synchronized(times) {
m = times[time]
if (m != null) {
++hits
return m!!
}
val c = Calendar.getInstance()
c.timeInMillis = time
c[Calendar.HOUR_OF_DAY] = 0
c[Calendar.MINUTE] = 0
c[Calendar.SECOND] = 0
c[Calendar.MILLISECOND] = 0
m = c.timeInMillis
times.append(time, m)
++misses
if (times.size() > THRESHOLD) resetCache()
}
return m!!
}
fun resetCache() {
hits = 0
misses = 0
times.clear()
}
fun log(): String =
"Hits: " + hits + " misses: " + misses + " stored: " + times.size()
}