Skip to content

Commit

Permalink
add missed files
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleg Smirnov committed May 25, 2020
1 parent 3a35cf3 commit e7627b9
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
74 changes: 74 additions & 0 deletions vk-sdk-core/src/main/java/com/vk/api/sdk/VKKeyValueStorage.kt
Original file line number Diff line number Diff line change
@@ -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<String, String> = 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)
Original file line number Diff line number Diff line change
@@ -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"
}
}
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit e7627b9

Please sign in to comment.