From e7627b91a81dbbc2c138556865db9e4ac9aaf648 Mon Sep 17 00:00:00 2001 From: Oleg Smirnov Date: Mon, 25 May 2020 22:00:25 +0300 Subject: [PATCH] add missed files --- .../java/com/vk/api/sdk/VKKeyValueStorage.kt | 74 +++++++++++++++++++ .../api/sdk/VKPreferencesKeyValueStorage.kt | 48 ++++++++++++ .../VKInternalServerErrorException.kt | 30 ++++++++ 3 files changed, 152 insertions(+) create mode 100644 vk-sdk-core/src/main/java/com/vk/api/sdk/VKKeyValueStorage.kt create mode 100644 vk-sdk-core/src/main/java/com/vk/api/sdk/VKPreferencesKeyValueStorage.kt create mode 100644 vk-sdk-core/src/main/java/com/vk/api/sdk/exceptions/VKInternalServerErrorException.kt diff --git a/vk-sdk-core/src/main/java/com/vk/api/sdk/VKKeyValueStorage.kt b/vk-sdk-core/src/main/java/com/vk/api/sdk/VKKeyValueStorage.kt new file mode 100644 index 0000000000..286935c92d --- /dev/null +++ b/vk-sdk-core/src/main/java/com/vk/api/sdk/VKKeyValueStorage.kt @@ -0,0 +1,74 @@ +/******************************************************************************* + * The MIT License (MIT) + * + * Copyright (c) 2019 vk.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + ******************************************************************************/ + +package com.vk.api.sdk + +import java.util.concurrent.ConcurrentHashMap + +interface VKKeyValueStorage { + fun put(key: String, value: String) + fun get(key: String): String? + fun remove(key: String) + + fun putOrRemove(key: String, value: String?) { + value?.let { put(key, value) } ?: remove(key) + } +} + +class VKCachedKeyValueStorage(private val storage: VKKeyValueStorage): VKKeyValueStorage { + private val cache: MutableMap = ConcurrentHashMap() + + override fun put(key: String, value: String) { + if (cache[key] != value) { + cache[key] = value + storage.put(key, value) + } + } + + override fun get(key: String): String? { + val value = cache[key] + return if (value !== NULL_STRING) { + return value ?: getFromStorage(key) + } else null + } + + override fun remove(key: String) { + if (cache[key] !== NULL_STRING) { + cache[key] = NULL_STRING + storage.remove(key) + } + } + + private fun getFromStorage(key: String): String? { + val value = storage.get(key) + cache[key] = value ?: NULL_STRING + return value + } + + companion object { + private val NULL_STRING = String() + } +} + +fun VKKeyValueStorage.cached(): VKKeyValueStorage = VKCachedKeyValueStorage(this) diff --git a/vk-sdk-core/src/main/java/com/vk/api/sdk/VKPreferencesKeyValueStorage.kt b/vk-sdk-core/src/main/java/com/vk/api/sdk/VKPreferencesKeyValueStorage.kt new file mode 100644 index 0000000000..77f4e8baee --- /dev/null +++ b/vk-sdk-core/src/main/java/com/vk/api/sdk/VKPreferencesKeyValueStorage.kt @@ -0,0 +1,48 @@ +/******************************************************************************* + * The MIT License (MIT) + * + * Copyright (c) 2019 vk.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + ******************************************************************************/ + +package com.vk.api.sdk + +import android.content.Context +import android.content.Context.MODE_PRIVATE + +class VKPreferencesKeyValueStorage(context: Context, prefsName: String = PREFERENCE_NAME) : VKKeyValueStorage { + private val prefs = context.getSharedPreferences(prefsName, MODE_PRIVATE) + + override fun put(key: String, value: String) { + prefs.edit().putString(key, value).apply() + } + + override fun get(key: String): String? { + return prefs.getString(key, null) + } + + override fun remove(key: String) { + prefs.edit().remove(key).apply() + } + + companion object { + private const val PREFERENCE_NAME = "com.vkontakte.android_pref_name" + } +} \ No newline at end of file diff --git a/vk-sdk-core/src/main/java/com/vk/api/sdk/exceptions/VKInternalServerErrorException.kt b/vk-sdk-core/src/main/java/com/vk/api/sdk/exceptions/VKInternalServerErrorException.kt new file mode 100644 index 0000000000..20e3db3535 --- /dev/null +++ b/vk-sdk-core/src/main/java/com/vk/api/sdk/exceptions/VKInternalServerErrorException.kt @@ -0,0 +1,30 @@ +/******************************************************************************* + * The MIT License (MIT) + * + * Copyright (c) 2019 vk.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + ******************************************************************************/ + +package com.vk.api.sdk.exceptions + +open class VKInternalServerErrorException( + httpStatus: Int, + detailMessage: String +) : Exception("Server returned httpStatusCode=$httpStatus with body: $detailMessage") \ No newline at end of file