diff --git a/ultrasonic/src/main/java/org/moire/ultrasonic/util/TimeLimitedCache.java b/ultrasonic/src/main/java/org/moire/ultrasonic/util/TimeLimitedCache.java deleted file mode 100644 index 1a39a440..00000000 --- a/ultrasonic/src/main/java/org/moire/ultrasonic/util/TimeLimitedCache.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - This file is part of Subsonic. - - Subsonic is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Subsonic is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Subsonic. If not, see . - - Copyright 2009 (C) Sindre Mehus - */ -package org.moire.ultrasonic.util; - -import java.lang.ref.SoftReference; -import java.util.concurrent.TimeUnit; - -/** - * @author Sindre Mehus - * @version $Id$ - */ -public class TimeLimitedCache -{ - - private SoftReference value; - private final long ttlMillis; - private long expires; - - public TimeLimitedCache(long ttl, TimeUnit timeUnit) - { - this.ttlMillis = TimeUnit.MILLISECONDS.convert(ttl, timeUnit); - } - - public T get() - { - return System.currentTimeMillis() < expires ? value.get() : null; - } - - public void set(T value) - { - set(value, ttlMillis, TimeUnit.MILLISECONDS); - } - - public void set(T value, long ttl, TimeUnit timeUnit) - { - this.value = new SoftReference(value); - expires = System.currentTimeMillis() + timeUnit.toMillis(ttl); - } - - public void clear() - { - expires = 0L; - value = null; - } -} diff --git a/ultrasonic/src/main/java/org/moire/ultrasonic/util/TimeLimitedCache.kt b/ultrasonic/src/main/java/org/moire/ultrasonic/util/TimeLimitedCache.kt new file mode 100644 index 00000000..09afdb41 --- /dev/null +++ b/ultrasonic/src/main/java/org/moire/ultrasonic/util/TimeLimitedCache.kt @@ -0,0 +1,31 @@ +/* + * TimeLimitedCache.kt + * Copyright (C) 2009-2021 Ultrasonic developers + * + * Distributed under terms of the GNU GPLv3 license. + */ +package org.moire.ultrasonic.util + +import java.lang.ref.SoftReference +import java.util.concurrent.TimeUnit + +class TimeLimitedCache(expiresAfter: Long = 60L, timeUnit: TimeUnit = TimeUnit.MINUTES) { + private var value: SoftReference? = null + private val expiresMillis: Long = TimeUnit.MILLISECONDS.convert(expiresAfter, timeUnit) + private var expires: Long = 0 + + fun get(): T? { + return if (System.currentTimeMillis() < expires) value!!.get() else null + } + + @JvmOverloads + fun set(value: T, ttl: Long = expiresMillis, timeUnit: TimeUnit = TimeUnit.MILLISECONDS) { + this.value = SoftReference(value) + expires = System.currentTimeMillis() + timeUnit.toMillis(ttl) + } + + fun clear() { + expires = 0L + value = null + } +}