Skip to content

Instantly share code, notes, and snippets.

View mskoroglu's full-sized avatar
👨‍💻

Mustafa Köroğlu mskoroglu

👨‍💻
View GitHub Profile
@mskoroglu
mskoroglu / VirtualFuture.java
Created February 1, 2024 07:58
Virtual Threads with async/await in Java
package com.mustafakoroglu.gist.virtualfuture;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.function.Supplier;
import java.util.stream.StreamSupport;
@mskoroglu
mskoroglu / UseCase.java
Last active June 3, 2024 08:26
Generic Use Case Interface with Functional Extensions in Java
/**
* A functional interface for use cases with an input of type {@code Q} and an output of type {@code S}.
* It extends {@code java.util.function.Function} to provide functional interface compatibility.
*
* @param <Q> the type of the request
* @param <S> the type of the response
*/
@FunctionalInterface
public interface UseCase<Q, S> extends java.util.function.Function<Q, S> {
/**
@mskoroglu
mskoroglu / EventHandler.java
Created October 29, 2023 10:57
An example of reified generic types in Java
public abstract class EventHandler<E extends Event> {
@SafeVarargs
protected EventHandler(final E... reifiedTypeArray) {
if (reifiedTypeArray.length > 0) {
throw new IllegalArgumentException();
}
final var reifiedType = reifiedTypeArray.getClass().getComponentType();
EventBus.registerHandler((Class<? extends Event>) reifiedType, this);
}
@mskoroglu
mskoroglu / Table.kt
Created April 20, 2022 08:23
Kotlin Table DSL
fun table(block: Table.() -> Unit): Table {
val table = Table()
table.block()
return table
}
class Table : Iterable<Row>, WithMetadataFilter<Row> {
private val rows = hashSetOf<Row>()
fun row(index: Int? = null, block: Row.() -> Unit): Row {
@mskoroglu
mskoroglu / pick.ts
Last active April 20, 2022 08:26
TypeScript type-safe pick function
/**
* Usage
* ```ts
* const person = { name: "John", surname: "Doe", age: 35 };
* console.log(pick(person, "surname", "age"));
* ```
*
* Result
* ```json
* { surname: 'Doe', age: 35 }
@mskoroglu
mskoroglu / Optional.ts
Created January 5, 2022 11:52
java.util.Optional implementation in TypeScript
export default class Optional<T> {
private static readonly EMPTY = new Optional(null);
private readonly value: T | null;
private constructor(value: T | null) {
this.value = value;
}
public static empty<T>(): Optional<T> {
@mskoroglu
mskoroglu / docker-compose.yml
Created October 26, 2021 14:28
Bitnami PostgreSQL Images - Pgpool-II & repmgr
version: '3'
volumes:
pg_0_data:
driver: local
pg_1_data:
driver: local
services:
pg-0:
@mskoroglu
mskoroglu / database.ts
Created December 29, 2020 07:48
Next.js with TypeORM - Database connection
import "reflect-metadata";
import {
Connection,
createConnection,
EntityTarget,
getConnectionManager,
Repository,
} from "typeorm";
import { Post } from "./posts/Post";
@mskoroglu
mskoroglu / LocaleSortingExtensions.kt
Last active July 6, 2022 09:34
Kotlin extension functions for sorting by locale(lang, country)
// Example: cities.sortedWithLocaleBy(Locale("tr")) { it.name }
inline fun <T, R : Comparable<R>> Iterable<T>.sortedWithLocaleBy(
locale: java.util.Locale,
crossinline selector: (T) -> R?
) = sortedWith(compareBy(java.text.Collator.getInstance(locale), selector))
// Example: cities.sortedWithLocaleByDescending(Locale("tr")) { it.name }
inline fun <T, R : Comparable<R>> Iterable<T>.sortedWithLocaleByDescending(
locale: java.util.Locale,
crossinline selector: (T) -> R?
import React from "react";
import { withState, multiState } from "./multi-state";
const Input = ({ state, ...props }) => (
<input
value={state.value}
onChange={e => state.setValue(e.target.value)}
{...props}
/>
);