Skip to content

Instantly share code, notes, and snippets.

View Miha-x64's full-sized avatar
🍵
へ‿(◉‿◉)‿ㄏ

Mike Miha-x64

🍵
へ‿(◉‿◉)‿ㄏ
View GitHub Profile
@Miha-x64
Miha-x64 / BadBackendResponseDeserializer.kt
Created March 31, 2017 18:44
Such deseriazers will help you in processing responses from back-ends which return 200 HTTP code all the time.
object class BadBackendResponseDeserializer : JsonDeserializer<Any?> {
// register all adapters you need
private val gson = GsonBuilder()
.registerTypeAdapter(Post::class.java, WallPostAdapter)
.registerTypeAdapter(Attachment::class.java, AttachmentAdapter)
.registerTypeAdapter(User::class.java, UserAdapter)
.registerTypeAdapter(Group::class.java, GroupAdapter)
.create()
@Miha-x64
Miha-x64 / RetrofitCallback.kt
Last active February 9, 2022 10:02
A wrapper around retrofit2.Callback
abstract class RetrofitCallback<T> : retrofit2.Callback<T> {
final override fun onResponse(call: Call<T>, response: Response<T>) =
if (response.isSuccessful) onSuccess(response.body())
else
onError(when (response.code()) {
401 -> Failure.Unauthorized()
404 -> Failure.NotFound()
409 -> Failure.Conflict()
500 -> Failure.InternalServerError()
@Miha-x64
Miha-x64 / EnumSetTypeAdapterFactory.kt
Last active February 9, 2022 10:01
TypeAdapter(Factory) for enums with no reflective @SerializedName lookup; TypeAdapterFactory whichs treats Set<E> as EnumSet<E>
package net.aquadc.common
import com.google.gson.Gson
import com.google.gson.TypeAdapter
import com.google.gson.TypeAdapterFactory
import com.google.gson.reflect.TypeToken
import java.lang.reflect.ParameterizedType
import java.util.*
/**
@Miha-x64
Miha-x64 / enums.kt
Last active February 9, 2022 10:00
Enum extensions for Kotlin, superseded by https://github.com/Miha-x64/Kotlin-MPP_Collection_utils
package net.aquadc.common
// Gist: https://gist.github.com/Miha-x64/5f626228b34175f827734596d6701008
import java.util.*
// maps
inline fun <reified K : Enum<K>, V> enumMapOf(): MutableMap<K, V> {
return EnumMap<K, V>(K::class.java)
}
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
public abstract class BundleKey<T> {
@NonNull final String key;
BundleKey(@NonNull String key) {
this.key = key;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.WorkerThread;
import java.io.File;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
package net.aquadc.util;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Iterator;
import java.util.function.Consumer;
import java.util.stream.Stream;
public final class Iterators {
private Iterators() {}
@Miha-x64
Miha-x64 / ArrayLists.java
Last active February 1, 2022 12:30
Utility-class for marking several ArrayList elements as removed and removing them all then.
package net.aquadc.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* For lists with O(n) random-deletion time (array-based lists, like {@link ArrayList}),
* it's faster to mark items as removed and them sweep them all together.
// Java(?) way
interface Fraction<E extends Exception> {
int numerator() throws E;
int denominator() throws E;
}
class FracStatic implements Fraction<RuntimeException> // don't force catching
class FracSum<E1, E2> implements Fraction<EitherException<E1, E2>> // impossible due to type erasure
// Kotlin, Scala, Rust way
@Miha-x64
Miha-x64 / CollectionWrap.java
Last active February 1, 2022 12:10
Absolutely normal and safe decorator class which can be easily broken with type-casting
import java.util.Collection;
import java.util.Comparator;
import java.util.Deque;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.NavigableSet;
import java.util.Queue;
import java.util.Set;
import java.util.SortedSet;