mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2026-04-14 19:01:22 +00:00
Improve code style to be more consistent
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
package org.schabi.newpipe.util;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.collection.LruCache;
|
||||
import android.util.Log;
|
||||
|
||||
import org.schabi.newpipe.MainActivity;
|
||||
|
||||
@@ -14,53 +15,58 @@ import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
import java.util.UUID;
|
||||
|
||||
public class SerializedCache {
|
||||
public final class SerializedCache {
|
||||
private static final boolean DEBUG = MainActivity.DEBUG;
|
||||
private final String TAG = getClass().getSimpleName();
|
||||
|
||||
private static final SerializedCache instance = new SerializedCache();
|
||||
private static final SerializedCache INSTANCE = new SerializedCache();
|
||||
private static final int MAX_ITEMS_ON_CACHE = 5;
|
||||
|
||||
private static final LruCache<String, CacheData> lruCache =
|
||||
private static final LruCache<String, CacheData> LRU_CACHE =
|
||||
new LruCache<>(MAX_ITEMS_ON_CACHE);
|
||||
private static final String TAG = "SerializedCache";
|
||||
|
||||
private SerializedCache() {
|
||||
//no instance
|
||||
}
|
||||
|
||||
public static SerializedCache getInstance() {
|
||||
return instance;
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public <T> T take(@NonNull final String key, @NonNull final Class<T> type) {
|
||||
if (DEBUG) Log.d(TAG, "take() called with: key = [" + key + "]");
|
||||
synchronized (lruCache) {
|
||||
return lruCache.get(key) != null ? getItem(lruCache.remove(key), type) : null;
|
||||
if (DEBUG) {
|
||||
Log.d(TAG, "take() called with: key = [" + key + "]");
|
||||
}
|
||||
synchronized (LRU_CACHE) {
|
||||
return LRU_CACHE.get(key) != null ? getItem(LRU_CACHE.remove(key), type) : null;
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public <T> T get(@NonNull final String key, @NonNull final Class<T> type) {
|
||||
if (DEBUG) Log.d(TAG, "get() called with: key = [" + key + "]");
|
||||
synchronized (lruCache) {
|
||||
final CacheData data = lruCache.get(key);
|
||||
if (DEBUG) {
|
||||
Log.d(TAG, "get() called with: key = [" + key + "]");
|
||||
}
|
||||
synchronized (LRU_CACHE) {
|
||||
final CacheData data = LRU_CACHE.get(key);
|
||||
return data != null ? getItem(data, type) : null;
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public <T extends Serializable> String put(@NonNull T item, @NonNull final Class<T> type) {
|
||||
public <T extends Serializable> String put(@NonNull final T item,
|
||||
@NonNull final Class<T> type) {
|
||||
final String key = UUID.randomUUID().toString();
|
||||
return put(key, item, type) ? key : null;
|
||||
}
|
||||
|
||||
public <T extends Serializable> boolean put(@NonNull final String key, @NonNull T item,
|
||||
public <T extends Serializable> boolean put(@NonNull final String key, @NonNull final T item,
|
||||
@NonNull final Class<T> type) {
|
||||
if (DEBUG) Log.d(TAG, "put() called with: key = [" + key + "], item = [" + item + "]");
|
||||
synchronized (lruCache) {
|
||||
if (DEBUG) {
|
||||
Log.d(TAG, "put() called with: key = [" + key + "], item = [" + item + "]");
|
||||
}
|
||||
synchronized (LRU_CACHE) {
|
||||
try {
|
||||
lruCache.put(key, new CacheData<>(clone(item, type), type));
|
||||
LRU_CACHE.put(key, new CacheData<>(clone(item, type), type));
|
||||
return true;
|
||||
} catch (final Exception error) {
|
||||
Log.e(TAG, "Serialization failed for: ", error);
|
||||
@@ -70,15 +76,17 @@ public class SerializedCache {
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
if (DEBUG) Log.d(TAG, "clear() called");
|
||||
synchronized (lruCache) {
|
||||
lruCache.evictAll();
|
||||
if (DEBUG) {
|
||||
Log.d(TAG, "clear() called");
|
||||
}
|
||||
synchronized (LRU_CACHE) {
|
||||
LRU_CACHE.evictAll();
|
||||
}
|
||||
}
|
||||
|
||||
public long size() {
|
||||
synchronized (lruCache) {
|
||||
return lruCache.size();
|
||||
synchronized (LRU_CACHE) {
|
||||
return LRU_CACHE.size();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,10 +96,10 @@ public class SerializedCache {
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private <T extends Serializable> T clone(@NonNull T item,
|
||||
private <T extends Serializable> T clone(@NonNull final T item,
|
||||
@NonNull final Class<T> type) throws Exception {
|
||||
final ByteArrayOutputStream bytesOutput = new ByteArrayOutputStream();
|
||||
try (final ObjectOutputStream objectOutput = new ObjectOutputStream(bytesOutput)) {
|
||||
try (ObjectOutputStream objectOutput = new ObjectOutputStream(bytesOutput)) {
|
||||
objectOutput.writeObject(item);
|
||||
objectOutput.flush();
|
||||
}
|
||||
@@ -100,11 +108,11 @@ public class SerializedCache {
|
||||
return type.cast(clone);
|
||||
}
|
||||
|
||||
final private static class CacheData<T> {
|
||||
private static final class CacheData<T> {
|
||||
private final T item;
|
||||
private final Class<T> type;
|
||||
|
||||
private CacheData(@NonNull final T item, @NonNull Class<T> type) {
|
||||
private CacheData(@NonNull final T item, @NonNull final Class<T> type) {
|
||||
this.item = item;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user