Skip to content

Instantly share code, notes, and snippets.

View david-bakin's full-sized avatar

David Bakin david-bakin

View GitHub Profile
@david-bakin
david-bakin / ReplaceThrowableMessageFragment.java
Last active July 3, 2023 01:03
Rewriting the message of a Throwable
/** Sometimes you might want to rethrow the same exception but with a little extra in the message. This allows
* you to "clone" an exception but change the message. (C# exceptions have the capability to hold arbitrary
* information via the `Data` property (a `Dictionary`).)
*/
@NonNull
public static Throwable replaceMessageOf(@NonNull final Throwable ex, @NonNull final String msg) {
// First try using reflection to just stomp on the exception instance's message
if (false) {
final var rex = stompOnMessageOf(ex, msg);
@david-bakin
david-bakin / OneArrayElementPerLineNoWhitespacePrettyPrinter.java
Created July 1, 2023 02:29
Jackson pretty printer that prints objects compactly (no whitespace) and array elements one-per-line
/* Looks like this:
{"todo":[
{"isbn":"0064632105"},
{"isbn":"007001115X"},
{"isbn":"0070079749"},
...
*/
public static class OneArrayElementPerLineNoWhitespacePrettyPrinter extends DefaultPrettyPrinter {
public OneArrayElementPerLineNoWhitespacePrettyPrinter() {}
@david-bakin
david-bakin / JacksonInterfaceCustomDeserializerMCVE.java
Created June 27, 2023 17:17
JacksonPolymorphicTypeHandlingOnInterfaceWithCustomDeserializer
/*
* Copyright (C) 2023 Bakin's Bits
*
* 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:
*
@david-bakin
david-bakin / gist:3b18a507ba4e2c825b26c01a85c8f858
Last active January 28, 2023 21:41
Electrum 4.3.4 "Balance" does _not_ include amount in change addresses
Electrum's "History" page - showing transactions in and out - has a total at the bottom called "Balance", which,
obviously, shows how much BTC is in the wallet.
Except it doesn't. Because it doesn't include any amount that is in change addresses.
@david-bakin
david-bakin / Auto.h
Last active August 6, 2022 17:53
O'Dwyer's Auto macro - hygenic scope guard w/ zero overhead
#pragma once
# O'Dwyer's Auto macro - hygenic scope guard w/ zero overhead
# https://quuxplusone.github.io/blog/2018/08/11/the-auto-macro/
#
# "This scope guard has perfect codegen with zero overhead (at `-O2`) on all major compilers."
# "Because it requires no explicit captures, no novel variable name, no special treatment for `this`,
# it is highly suited for mechanically generated code."
# "It is portable all the way back to C++11."
@david-bakin
david-bakin / template-inheriting-from-its-own-specialization.cc
Created January 12, 2021 17:05
Odd example of a template inheriting from its own specialization
#include <iostream>
#include <string>
// Template which inherits from its own specialization, as I first saw in
// https://stackoverflow.com/a/28213747/751579, which surprised me
template <typename T>
struct Foo : public Foo<decltype(T::x)> {
T xxx;
};
@david-bakin
david-bakin / grab-my-kindle-books.js
Last active June 28, 2022 05:35
Grab list of all Kindle books purchased from the currently logged in Amazon account - saves as a JSON file.
// Grab list of all kindle books purchased from the currently logged in
// Amazon account. Lets you save this list as a JSON file.
// The following data should be run in the console while viewing the page https://read.amazon.com/
// Kindle booklist fetch derived from jkubecki/ExportKindle.js
// (https://gist.github.com/jkubecki/d61d3e953ed5c8379075b5ddd8a95f22)
// console.save grabbed from DevTools Snipppets
// (https://bgrins.github.io/devtools-snippets/#console-save)
@david-bakin
david-bakin / shanselman-profile.json
Last active May 5, 2020 23:14 — forked from shanselman/settings.json
Microsoft Terminal settings/profile from [Scott Hanselman](https://www.youtube.com/watch?v=j0PPcUUtHlw)
{
"$schema": "https://aka.ms/terminal-profiles-schema",
"globals": {
"alwaysShowTabs": true,
"defaultProfile": "{79285a8e-036c-446f-8a9c-78994e34cf85}",
"initialCols": 120,
"initialRows": 30,
"requestedTheme": "dark",
"keybindings": [
{
@david-bakin
david-bakin / UsingStreamCollectOrCollector.java
Last active August 10, 2023 12:21
How to use Java 8 Stream::collect() to find the longest sequential subsequence in a stream
package com.bakins_bits;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
@david-bakin
david-bakin / Result.java
Created November 11, 2015 05:03
Java Result class - union (sum) type of a success type and a failure type, with a "pattern matching" functional interface
package com.bakins_bits.types;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import com.google.common.base.Preconditions;