Skip to content

Instantly share code, notes, and snippets.

@kalgon
kalgon / XmlHash.java
Created February 20, 2024 13:38
Canonicalizing and Hashing XML in Java
import org.apache.xml.security.Init;
import org.apache.xml.security.c14n.Canonicalizer;
import org.apache.xml.security.exceptions.XMLSecurityException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.ByteArrayOutputStream;
@kalgon
kalgon / diff.ts
Last active February 3, 2024 22:25
diff
type Model<T> = T extends number | string | boolean ? typeof Object
: T extends Array<infer E> ? [Model<E>]
: { [P in keyof T]?: Model<T[P]> };
type Status = 'modified' | 'unchanged' | 'removed' | 'added';
type Diff<T> = Exclude<T, undefined | null> extends number | string | boolean ? { status: Status, left: T, right: T }
: Exclude<T, undefined | null> extends Array<infer E> ? { status: Status, elements: Array<Diff<E>> }
: { status: Status, props: { [P in keyof T]: Diff<T[P]> }};
@kalgon
kalgon / ancestors.sql
Last active December 21, 2022 10:39
Recursive Common Table Expression
with
people (id, name, father_id, mother_id) as (
select 1, 'Abraham Simpson', null, null from dual
union
select 2, 'Mona Olsen', null, null from dual
union
select 3, 'Clancy Bouvier', null, null from dual
union
select 4, 'Jackie Gurney', null, null from dual
union
@kalgon
kalgon / callback.html
Created June 8, 2022 13:54
OpenID Connect
<script src="https://cdnjs.cloudflare.com/ajax/libs/oidc-client/1.11.5/oidc-client.js"></script>
<script>new Oidc.UserManager().signinPopupCallback()</script>
@kalgon
kalgon / Either.java
Created September 23, 2020 10:10
Either
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
public final class Either<Left, Right> {
public static <L, R> Either<L, R> ofLeft(L left) {
return new Either<>(Objects.requireNonNull(left), null);
}
@kalgon
kalgon / Display.java
Last active November 29, 2022 08:00
Tree view
package com.github.kalgon;
import java.util.stream.IntStream;
import static com.github.kalgon.TreeView.*;
import static java.util.stream.Collectors.joining;
public final class Display {
public static void none(Object commit, int[] line) {
@kalgon
kalgon / FragmentablePageFactory.java
Last active June 19, 2020 11:56
Selenium PageFactory which supports fragments (nested classes)
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.pagefactory.DefaultElementLocatorFactory;
import org.openqa.selenium.support.pagefactory.DefaultFieldDecorator;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
@kalgon
kalgon / JavaBeanTypeFactory.java
Last active July 13, 2022 07:38
Generate observable JavaBean types from interface with ByteBuddy
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.modifier.FieldManifestation;
import net.bytebuddy.description.modifier.Visibility;
import net.bytebuddy.dynamic.DynamicType.Builder;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.dynamic.scaffold.subclass.ConstructorStrategy;
import net.bytebuddy.implementation.FieldAccessor;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.implementation.MethodDelegation;
@kalgon
kalgon / StreamSeeker.java
Created March 20, 2018 09:24
Seek for an array of bytes[]/chars[] in an OutputStream/Reader in java
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
public class StreamSeeker {
public static int seek(InputStream stream, byte[] bytes) throws IOException {
boolean[] checks = new boolean[bytes.length];
for (int s = bytes.length, z = 0, i = 0, x; (x = stream.read()) != -1; z++, i = i == s - 1 ? 0 : i + 1) {
@kalgon
kalgon / UUID2Base64.java
Created March 19, 2018 10:02
Convert UUID to Base64
import java.nio.ByteBuffer;
import java.util.Base64;
import java.util.UUID;
public enum UUID2Base64 {
BASIC(Base64.getEncoder(), Base64.getDecoder()),
MIME(Base64.getMimeEncoder(), Base64.getMimeDecoder()),
URL(Base64.getUrlEncoder(), Base64.getUrlDecoder());